Keystroke logging, often referred to as keylogging or keyboard capturing, is the action of recording the keys struck on a keyboard, typically covertly, so that a person using the keyboard is unaware that their actions are being monitored. Data can then be retrieved by the person ope<rating the logging program.
#Using g++
C:\>g++ -o keylogger.exe keylogger.cpp
Or use an IDE
In terms of legality, a tool isn't illegal unless used unethically. The tool can be used to track someone's keystrokes when using your pc. It's very much safe to assume that a user will turn this on just to make sure that the one using your pc isn't doing anything SUS.
C++ is a general-purpose progrmming language created by Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language is a superset of the programming language C, although this language is really not that type safe just like it's older sibling, it still is a good programming langauge to use when making exploits, malwares etc.
I reffered the code to a keylogger tutorial i found on youtube, although i forgot the youtube channel. š¤¦ With that said, enjoy using the tool for education and more knowledge.