Как обнаружить нажатие одной клавиши пробела в низкоуровневой клавиатуре без таймераC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Как обнаружить нажатие одной клавиши пробела в низкоуровневой клавиатуре без таймера

Сообщение Anonymous »


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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»