While loop using a lot of CPU time

后端 未结 3 1615
無奈伤痛
無奈伤痛 2020-12-21 13:00

I am creating a keystroke logger for my personal interest, as well wanting to know how to capture and use them as functions (like key shortcuts).

I got the code to l

3条回答
  •  别那么骄傲
    2020-12-21 13:27

    On Windows, you need to insert a low level keyboard hook with the windows API. Then you will get a callback on your LowLevelKeyboardProc function, which you can then use to log. This will use pretty much 0% CPU and is guaranteed to catch every key.

    As for handling a global key press to quit the logger, you could look for it in LowLevelKeyboardProc since you will be intercepting every key, but a better solution is to use RegisterHotKey and then look for WM_HOTKEY events in the message loop. It is a better solution because

    1. It will be less of a CPU burden on the LowLevelKeyboardProc, which is important for low level hooks.
    2. It works for other applications that aren't monitoring every keystroke.

提交回复
热议问题