How to use (Get|Set)KeyboardState() to modify the keyboard state when using PostMessage()?

Viewed 70

I read that when the TranslateMessage() API is called, it 'checks' your current keyboard state, so if you want to really 'simulate' a key input, you need to also modify the thread's keyboard state.

I'm trying to understand what |= 0x80 means, is it 'the key is down'?

DWORD current_thread_id = GetCurrentThreadId();
DWORD thread_id = GetWindowThreadProcessId(hWnd, NULL);

AttachThreadInput(current_thread_id, thread_id , true);

BYTE KeybState[256] = { '\0' };

GetKeyboardState((PBYTE)&KeybState);

KeybState[...] |= 0x80;

SetKeyboardState((LPBYTE)&KeybState);

SetFocus(hWnd);

PostMessage(hWnd, WM_KEYDOWN, ..., ...);
PostMessage(hWnd, WM_KEYUP, ..., ...);

AttachThreadInput(current_thread_id, thread_id , false);

What value represents when the key is up, KeybState[...] = 0?

Suppose I'm trying to send #, I need to:

KeybState[VK_SHIFT] |= 0x80;
SetKeyboardState((LPBYTE)&KeybState);
PostMessage(hWnd, WM_KEYDOWN, VK_SHIFT, lParam);
PostMessage(hWnd, WM_KEYUP, 0x33, lParam); // 0x33 is the vk_code for the  3 key

Or, do I also need to use KeybState[0x33] |= 0x80;?

0 Answers
Related