Detecting key press for hotkey?

How would I detect every key press without listing out every key code from start to finish and not limiting the usable keys to special keys?

I want to create a function which detects a keypress and get the keycode of said key.
Pseudo:
1
2
3
4
5
6
7
8
  Program Start
  keypress == user presses key
  var hotkey == keypress
  while ( true )
  {
   if(user presses hotkey)
      you presses the hotkey!
   }

I want to make a program that does what's above ^ with any pressable key eg: User presses K the hotkey can be K doesn't have to be restricted to F1-12 and other special keys.

I can not think of a way to do this other than making a switch statement with a case for all 70+ keys that would be over 200+ lines (estimating) of code which I'd rather not right out if I don't have to.
Never mind I've figured out how to do it. I thought Keyboard Hooks would be harder, but it turns out they're much simpler! The KBDLLHOOK* structure actually stores the vkCode of pressed keys.

Working Code:
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
int keys;
LRESULT CALLBACK selectHotkey(int nCode, WPARAM wParam, LPARAM lParam)
{
	KBDLLHOOKSTRUCT *pK = (KBDLLHOOKSTRUCT*)lParam;
	switch (wParam)
	{
	case WM_KEYDOWN:
	{
		switch (pK->vkCode)
		{
		default:
			printf("HotKey: %d", pK->vkCode);
			keys = pK->vkCode;
			break;
		}

	}
	//Wparam Default
	default:
		break;
	}
	return CallNextHookEx(NULL, nCode, wParam, lParam);
}


/* Keyboard Hook */
int KHhotkey(int opt) // KH = Keyboard Hook
{
	option = opt;
	HINSTANCE appInstance = GetModuleHandle(NULL);
	SetWindowsHookEx(WH_KEYBOARD_LL, selectHotkey, appInstance, 0);
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)>0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
Topic archived. No new replies allowed.