|
24 | 24 |
|
25 | 25 | extern RemapEntryContainerListItem* g_KeyTranslationTable[WIN_KEY_COUNT]; |
26 | 26 |
|
| 27 | +const int LONG_PRESS_MIN = 100; |
| 28 | +const int KEY_DOWN_UNSET = 0; // key not being held |
| 29 | +const int KEY_DOWN_EVENT_FIRED = 1; // key is held and already sent output |
| 30 | +// other values stored in g_KeyDownTime are the original key down time as returned by GetTickCount64 |
27 | 31 | ULONGLONG g_KeyDownTime[WIN_KEY_COUNT]; |
28 | 32 |
|
29 | 33 | /* |
@@ -83,13 +87,49 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) |
83 | 87 | && !bShift |
84 | 88 | && pKeyDef->inputFlag.bLongPress) |
85 | 89 | { |
86 | | - if (g_KeyDownTime[pKeyDef->virtualKey] == 0) // ignore all keydown events on this key after recording the initial key down time |
| 90 | + switch(g_KeyDownTime[pKeyDef->virtualKey]) |
| 91 | + { |
| 92 | + case KEY_DOWN_UNSET: |
87 | 93 | { |
88 | 94 | const ULONGLONG tickCount = GetTickCount64(); |
89 | 95 | g_KeyDownTime[pKeyDef->virtualKey] = tickCount; |
90 | 96 | #ifdef _DEBUG |
91 | 97 | LogDebugMessage("Detected LONGPRESS Key Down: %d Tick: %d", pKeyDef->virtualKey, tickCount); |
92 | 98 | #endif |
| 99 | + } |
| 100 | + break; |
| 101 | + |
| 102 | + case KEY_DOWN_EVENT_FIRED: |
| 103 | + // nothing for now |
| 104 | + break; |
| 105 | + default: |
| 106 | + { |
| 107 | + const ULONGLONG tickCount = GetTickCount64(); |
| 108 | + const unsigned int longPressMinimum = max(LONG_PRESS_MIN, pKeyDef->parameter); |
| 109 | +#if false |
| 110 | + // windows has a built repeat rate limiter so the shortest time is 250ms |
| 111 | + const ULONGLONG diff = tickCount - g_KeyDownTime[pKeyDef->virtualKey]; |
| 112 | + LogDebugMessage("diff: %d", diff); |
| 113 | +#endif |
| 114 | + if (tickCount - g_KeyDownTime[pKeyDef->virtualKey] >= longPressMinimum) |
| 115 | + { |
| 116 | +#ifdef _DEBUG |
| 117 | + char* pInputConfigDescription = GetInputConfigDescription(*pKeyDef); |
| 118 | + LogDebugMessage("Detected LONGPRESS [Key Down: %s] [Outputs: %d][longPressMinimum: %d]", |
| 119 | + pInputConfigDescription, |
| 120 | + pKeyListItem->pEntryContainer->pEntry->outputCount, |
| 121 | + longPressMinimum); |
| 122 | + free(pInputConfigDescription); |
| 123 | +#endif |
| 124 | + // If there is NOT an existing thread OR the existing thread is running a repeat another key press is allowed |
| 125 | + if (NULL == pKeyListItem->pEntryContainer->pEntryState->threadHandle || pKeyListItem->pEntryContainer->pEntryState->bRepeating) |
| 126 | + { |
| 127 | + pKeyListItem->pEntryContainer->pEntryState->threadHandle = CreateThread(NULL, 0, SendInputThread, pKeyListItem->pEntryContainer, 0, NULL); |
| 128 | + } |
| 129 | + g_KeyDownTime[pKeyDef->virtualKey] = KEY_DOWN_EVENT_FIRED; |
| 130 | + } |
| 131 | + } |
| 132 | + break; |
93 | 133 | } |
94 | 134 | // block further processing with the inputs |
95 | 135 | bSentInput = true; |
@@ -121,27 +161,14 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) |
121 | 161 | && !bShift |
122 | 162 | && pKeyDef->inputFlag.bLongPress) |
123 | 163 | { |
124 | | - ULONGLONG tickCount = GetTickCount64(); |
125 | | - if (tickCount - g_KeyDownTime[pKeyDef->virtualKey] > 1000) |
126 | | - { |
127 | | -#ifdef _DEBUG |
128 | | - char* pInputConfigDescription = GetInputConfigDescription(*pKeyDef); |
129 | | - LogDebugMessage("Detected LONGPRESS Key Up: %s Outputs: %d", pInputConfigDescription, pKeyListItem->pEntryContainer->pEntry->outputCount); |
130 | | - free(pInputConfigDescription); |
131 | | -#endif |
132 | | - // If there is NOT an existing thread OR the existing thread is running a repeat another key press is allowed |
133 | | - if (NULL == pKeyListItem->pEntryContainer->pEntryState->threadHandle || pKeyListItem->pEntryContainer->pEntryState->bRepeating) |
134 | | - { |
135 | | - pKeyListItem->pEntryContainer->pEntryState->threadHandle = CreateThread(NULL, 0, SendInputThread, pKeyListItem->pEntryContainer, 0, NULL); |
136 | | - } |
137 | | - } |
138 | | - else |
| 164 | + if (g_KeyDownTime[pKeyDef->virtualKey] != KEY_DOWN_EVENT_FIRED) |
139 | 165 | { |
| 166 | + const ULONGLONG tickCount = GetTickCount64(); |
140 | 167 | LogDebugMessage("Detected LONGPRESS Key UP (too short): %d Tick: %d (Old Tick: %d)", pKeyDef->virtualKey, tickCount, g_KeyDownTime[pKeyDef->virtualKey]); |
141 | 168 | // when a longpress "fails" just send the normal keypress |
142 | 169 | CreateThread(NULL, 0, SendInputKeypress, pKeyDef, 0, NULL); |
143 | 170 | } |
144 | | - g_KeyDownTime[pKeyDef->virtualKey] = 0; |
| 171 | + g_KeyDownTime[pKeyDef->virtualKey] = KEY_DOWN_UNSET; |
145 | 172 | // block further processing with the inputs |
146 | 173 | bSentInput = true; |
147 | 174 | break; |
|
0 commit comments