Как обнаружить нажатие одной клавиши пробела в низкоуровневой клавиатуре без таймера ⇐ C++
Как обнаружить нажатие одной клавиши пробела в низкоуровневой клавиатуре без таймера
I've broken my arm and am developing a one-handed keyboard application for Windows that mirrors the keyboard layout when the space bar is held down, allowing for one-handed typing. The mirroring function is operational using a timer, but this is not ideal - it's sluggish and leaves a space if a mirrored key is pressed quickly after holding space down.
Is there a better way to handle this use case? I've tried using flags from the lParam's KBDLLHOOKSTRUCT struct but I cannot find a way to differentiate single key presses from repeated. I've also tried ignoring input on space keydowns and sending an input when it's just a single press, but I run into issues of it not working, likely due to infinite loops.
Any help or pointers in the right direction would be greatly appreciated.
#include #include #include #include using Clock = std::chrono::high_resolution_clock; std::chrono::time_point spacePressedTime; bool spaceHeld = false; std::map keyMappings; bool SendMirroredKeyPress(UINT vkCode); void InitializeKeyMappings(); LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { auto kbdStruct = *((KBDLLHOOKSTRUCT*)lParam); if (kbdStruct.vkCode == VK_SPACE) { switch (wParam) { case WM_KEYDOWN: if (!spaceHeld) { // Check if this is the first event of space bar press spacePressedTime = Clock::now(); spaceHeld = true; // Prevent re-entry for this press } return 1; // Block this event to prevent default space input case WM_KEYUP: if (spaceHeld) { auto elapsed = std::chrono::duration_cast(Clock::now() - spacePressedTime).count(); if (elapsed < 200) { // Threshold for distinguishing tap from hold // It was a tap, simulate a space key press INPUT input[2] = {}; input[0].type = INPUT_KEYBOARD; input[0].ki.wVk = VK_SPACE; input[1].type = INPUT_KEYBOARD; input[1].ki.wVk = VK_SPACE; input[1].ki.dwFlags = KEYEVENTF_KEYUP; SendInput(2, input, sizeof(INPUT)); std::cout
Источник: https://stackoverflow.com/questions/780 ... thout-time
I've broken my arm and am developing a one-handed keyboard application for Windows that mirrors the keyboard layout when the space bar is held down, allowing for one-handed typing. The mirroring function is operational using a timer, but this is not ideal - it's sluggish and leaves a space if a mirrored key is pressed quickly after holding space down.
Is there a better way to handle this use case? I've tried using flags from the lParam's KBDLLHOOKSTRUCT struct but I cannot find a way to differentiate single key presses from repeated. I've also tried ignoring input on space keydowns and sending an input when it's just a single press, but I run into issues of it not working, likely due to infinite loops.
Any help or pointers in the right direction would be greatly appreciated.
#include #include #include #include using Clock = std::chrono::high_resolution_clock; std::chrono::time_point spacePressedTime; bool spaceHeld = false; std::map keyMappings; bool SendMirroredKeyPress(UINT vkCode); void InitializeKeyMappings(); LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { auto kbdStruct = *((KBDLLHOOKSTRUCT*)lParam); if (kbdStruct.vkCode == VK_SPACE) { switch (wParam) { case WM_KEYDOWN: if (!spaceHeld) { // Check if this is the first event of space bar press spacePressedTime = Clock::now(); spaceHeld = true; // Prevent re-entry for this press } return 1; // Block this event to prevent default space input case WM_KEYUP: if (spaceHeld) { auto elapsed = std::chrono::duration_cast(Clock::now() - spacePressedTime).count(); if (elapsed < 200) { // Threshold for distinguishing tap from hold // It was a tap, simulate a space key press INPUT input[2] = {}; input[0].type = INPUT_KEYBOARD; input[0].ki.wVk = VK_SPACE; input[1].type = INPUT_KEYBOARD; input[1].ki.wVk = VK_SPACE; input[1].ki.dwFlags = KEYEVENTF_KEYUP; SendInput(2, input, sizeof(INPUT)); std::cout
Источник: https://stackoverflow.com/questions/780 ... thout-time
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Код VS удаляет пробелы после клавиши табуляции и клавиши пробела (невозможно сделать отступ)
Anonymous » » в форуме Html - 0 Ответы
- 14 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Нажатие конечной клавиши на моей клавиатуре делает эмулятор сходить с ума
Anonymous » » в форуме Android - 0 Ответы
- 4 Просмотры
-
Последнее сообщение Anonymous
-