Looks like your processor has 2 cores. Your program is using up 100% of one core which adds up to 50% in total.
If you want to lower the CPU usage you could call Sleep or std::sleep_for inside the loop but that might make your keylogger less responsive, increasing the risk that you miss short keypresses.
It has 1 core. My processor is Intel Pentium 4 CPU 2.80GHz
I have been using this keylogger for a few months. Maybe I have to rewrite it in other way. But I am not so skilled in C++ yet. For now I just want to modify it so that it won't load my processor anymore.
someone already said it, try sleep(1); in your loop. Be careful, from (a rather faulty) memory sleep(1) is 1 millisecond, Sleep(1) is one second (nonstandard windows function).
the better way is probably to use directinput (direct access libraries like directx that cut out a lot of the middleman junk between your program and the hardware) and make your logger event driven (so it only does any work when a key is pressed, something like using the windows messaging to trap on key press events).
Hooks might work also. There are a bunch of ways to do it. The one way to not do it is anything that just loops forever constantly checking for the events.
your code never takes a break. The program checks for a key, if it finds one it logs it, then it immediately checks again, ... there is no downtime. The CPU is dumb. The CPU runs the instructions in your code literally as fast as it can. If you do not tell it to take a breather between instructions, it just runs and runs as hard as it can go. Inserting the sleep allows the OS and hardware to swap your program out for a moment. To put in perspective, your hz of the cpu is how many low level instructions you can do at once on one core as a ball park (its not 100% accurate due to u/v pipes and other things, but its more or less true).
A modern machine runs at around 3 GHZ. That is 3000000000 instructions in a second. In a milisecond, it can do 3000000 instructions. So with a sleep(1) it can execute 3 million instructions before it comes back to your code. More likely, since sleep is imprecise, it will squeeze in a few more if there is something to do on the machine (if idle, it will spring back quickly). So it does not seem like much, but its a lot to the CPU.
Without the sleep, it probably is checking for a key press more than a million times a second. If you hold down a key on the keyboard, you still only get a few hundred per second....
Thank you for your explanations. I think it's better to rewrite the whole code from scratch. In another way. But that will be a big work for me as a novice in cpp.