Question regarding system-wide Hotkeys and disabling keydown autorepeat

Hello,

I'm trying to write a program which will use hotkeys to capture user key events and turn them into key up/key down events.

Here is what I would like to happen:

Ideally, the program would remove the original user keypress (i.e. not send it to the current user application) and activate the hotkey instead.
The hotkey will then send a separate, corresponding keypress. There are two things that can happen with this keypress:
1. User is holding mouse4. Key down is sent. Program waits. Key up is only sent when Mouse4 is released.
2. User is NOT holding mouse4. Key down and key up are sent immediately.

Here is what actually happens:

Problem 1: The user's original keypress is sent, including key down and key up events.
Ideal Solution: Completely negate the user's original key press, allowing the hotkey function exclusive permission to send key up and down events as necessary.

Problem 2: The "KeyDown" event inside the hotkey function seems to be repeating as if the key was held down by the user.
Ideal Solution: I would like this key to simply be seen as "down" if the state is checked. The autorepeat feature inside Windows seems to prevent the single "down" state from happening.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <windows.h>
using namespace std;

void KeyDown(int id) {
	INPUT press;
	press.type = INPUT_KEYBOARD;
	press.ki.wScan = 0;
	press.ki.time = 0;
	press.ki.dwExtraInfo = 0;
	press.ki.wVk = id;
	press.ki.dwFlags = 0;
	SendInput(1, &press, sizeof(INPUT));
}

void KeyUp(int id) {
	INPUT rls;
	rls.type = INPUT_KEYBOARD;
	rls.ki.wScan = 0;
	rls.ki.time = 0;
	rls.ki.dwExtraInfo = 0;
	rls.ki.wVk = id;
	rls.ki.dwFlags = 2;
	SendInput(1, &rls, sizeof(INPUT));
}

int main() {
	RegisterHotKey(NULL, 1, 0, 0x51);
	MSG msg = { 0 };

	while (GetMessage(&msg, NULL, 0, 0) != 0) {
		if (msg.message == WM_HOTKEY) {
			switch (msg.wParam){
			case 1:
				UnregisterHotKey(NULL, 1);
				if (GetAsyncKeyState(VK_XBUTTON1)) {
					KeyDown(0x51);
					cout << "Sending Q Down Before Mouse4" << endl;
					while (GetAsyncKeyState(VK_XBUTTON1)) {
						Sleep(100);
						cout << "Sleeping while Mouse4 Held" << endl;
					}
					KeyUp(0x51);
					cout << "Sending Q Up After Mouse4" << endl;
				}
				else {
					KeyDown(0x51);
					cout << "Sending Fallback Q Down" << endl;
					KeyUp(0x51);
					cout << "Sending Fallback Q Up" << endl;
				}
				RegisterHotKey(NULL, 1, 0, 0x51);
				cout << "You pushed  hotkey 1" << endl;
				break;
			}
		}
	}
	UnregisterHotKey(NULL, 1);
}
Topic archived. No new replies allowed.