I'm looking to rewrite ctrl+c and ctrl+v on my Windows 10 machine to add some additional functionality.
I'm able to copy and paste correctly and have successfully created a keyboard hook to execute my code after these keys are pressed but I'm having an issue after I press ctrl while my program is running, ctrl continuously acts as if it's held down. Even after I terminate the program entirely, ctrl continues to act as if it's being held down until I logout of the computer entirely. What can I do to correct this?
Thanks!
Edit: after doing a bit of messing around, I can conclude any key gets stuck. Shift and caps lock get stuck as well.
#include <Windows.h>
#include <stdio.h>
#include <queue>
using namespace std;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
WM_COPY;
}
else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
OpenClipboard(NULL);
char* buffer;
buffer = (char*)GetClipboardData(CF_TEXT);
CloseClipboard();
cout << buffer;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
}
int main()
{
HHOOK keyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL);
MSG msg;
while (!GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(keyBoard);
}
Do not put return CallNextHookEx(NULL, nCode, wParam, lParam)
in if (wParam == WM_KEYDOWN)
.
Modify:
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
WM_COPY;
}
else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
OpenClipboard(NULL);
char* buffer;
buffer = (char*)GetClipboardData(CF_TEXT);
CloseClipboard();
cout << buffer;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}