I would like to point out there is an event-based method that performs better than GetAsncKeyState
which is via SetWindowsHookEx
with the WH_KEYBOARD_LL
hook type that can install a global monitor on keystrokes. Please refer to LowLevelKeyboardProc
for the callback function details. For an example (in C), you can refer to my previous thread on Windows Keylogging (shameless self-promotion ).
Iām assuming the reason you need this is because youāre building a console application. If you can get it to build with the Windows (GUI) subsystem, it removes the console prompt entirely, making this code redundant. Itās also good to note that if you do this, you may need the Windows Message Loop to keep the application alive. Itāll be needed to obtain messages for the keystrokes from SetWindowsHookEx
as well.
As for the clipboard monitor, there are a few ways to do it. MSDN documents one way through GetClipboardSequenceNumber
that uses a 32-bit āclipboard sequence numberā to track different clipboard contents. It also documents an event-based listener with AddClipboardFormatListener
with the WM_CLIPBOARDUPDATE
window message (requires a window). Here is some PoC code that demonstrates the latter: ClipboardMonitor.c.
EDIT: I just realised that your code might not account for right-click ā copy.
Looking forward to the rest of the series!