@@ -30,6 +30,9 @@ const int KEY_DOWN_EVENT_FIRED = 1; // key is held and already sent output
3030// other values stored in g_KeyDownTime are the original key down time as returned by GetTickCount64
3131ULONGLONG g_KeyDownTime[WIN_KEY_COUNT];
3232
33+ void ProcessLongPressKeyDown (const InputConfig* pKeyDef, const RemapEntryContainerListItem* pKeyListItem);
34+ void ProcessLongPressKeyUp (const InputConfig* pKeyDef);
35+
3336/*
3437Implementation of the win32 LowLevelKeyboardProc (see docs for information)
3538
@@ -87,50 +90,7 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
8790 && !bShift
8891 && pKeyDef->inputFlag .bLongPress )
8992 {
90- switch (g_KeyDownTime[pKeyDef->virtualKey ])
91- {
92- case KEY_DOWN_UNSET:
93- {
94- const ULONGLONG tickCount = GetTickCount64 ();
95- g_KeyDownTime[pKeyDef->virtualKey ] = tickCount;
96- #ifdef _DEBUG
97- LogDebugMessage (" Detected LONGPRESS Key Down: %d Tick: %d" , pKeyDef->virtualKey , tickCount);
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 ;
133- }
93+ ProcessLongPressKeyDown (pKeyDef, pKeyListItem);
13494 // block further processing with the inputs
13595 bSentInput = true ;
13696 break ;
@@ -161,15 +121,7 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
161121 && !bShift
162122 && pKeyDef->inputFlag .bLongPress )
163123 {
164- if (g_KeyDownTime[pKeyDef->virtualKey ] != KEY_DOWN_EVENT_FIRED &&
165- g_KeyDownTime[pKeyDef->virtualKey ] != KEY_DOWN_UNSET)
166- {
167- const ULONGLONG tickCount = GetTickCount64 ();
168- LogDebugMessage (" Detected LONGPRESS Key UP (too short): %d Tick: %d (Old Tick: %d)" , pKeyDef->virtualKey , tickCount, g_KeyDownTime[pKeyDef->virtualKey ]);
169- // when a longpress "fails" just send the normal keypress
170- SendInputKeypress (pKeyDef);
171- }
172- g_KeyDownTime[pKeyDef->virtualKey ] = KEY_DOWN_UNSET;
124+ ProcessLongPressKeyUp (pKeyDef);
173125 // block further processing with the inputs
174126 bSentInput = true ;
175127 break ;
@@ -183,3 +135,63 @@ LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
183135 }
184136 return bSentInput ? 1 : CallNextHookEx (NULL , nCode, wParam, lParam);
185137}
138+
139+ void ProcessLongPressKeyDown (const InputConfig* pKeyDef, const RemapEntryContainerListItem* pKeyListItem)
140+ {
141+ switch (g_KeyDownTime[pKeyDef->virtualKey ])
142+ {
143+ case KEY_DOWN_EVENT_FIRED:
144+ // nothing for now
145+ break ;
146+ case KEY_DOWN_UNSET:
147+ {
148+ const ULONGLONG tickCount = GetTickCount64 ();
149+ g_KeyDownTime[pKeyDef->virtualKey ] = tickCount;
150+ #ifdef _DEBUG
151+ LogDebugMessage (" Detected LONGPRESS Key Down: %d Tick: %d" , pKeyDef->virtualKey , tickCount);
152+ #endif
153+ }
154+ break ;
155+ default :
156+ {
157+ const ULONGLONG tickCount = GetTickCount64 ();
158+ const unsigned int longPressMinimum = max (LONG_PRESS_MIN, pKeyDef->parameter );
159+ #if false
160+ // windows has a built repeat rate limiter so the shortest time is 250ms
161+ const ULONGLONG diff = tickCount - g_KeyDownTime[pKeyDef->virtualKey ];
162+ LogDebugMessage (" diff: %d" , diff);
163+ #endif
164+ if (tickCount - g_KeyDownTime[pKeyDef->virtualKey ] >= longPressMinimum)
165+ {
166+ #ifdef _DEBUG
167+ char * pInputConfigDescription = GetInputConfigDescription (*pKeyDef);
168+ LogDebugMessage (" Detected LONGPRESS [Key Down: %s] [Outputs: %d][longPressMinimum: %d]" ,
169+ pInputConfigDescription,
170+ pKeyListItem->pEntryContainer ->pEntry ->outputCount ,
171+ longPressMinimum);
172+ free (pInputConfigDescription);
173+ #endif
174+ // If there is NOT an existing thread OR the existing thread is running a repeat another key press is allowed
175+ if (NULL == pKeyListItem->pEntryContainer ->pEntryState ->threadHandle || pKeyListItem->pEntryContainer ->pEntryState ->bRepeating )
176+ {
177+ pKeyListItem->pEntryContainer ->pEntryState ->threadHandle = CreateThread (NULL , 0 , SendInputThread, pKeyListItem->pEntryContainer , 0 , NULL );
178+ }
179+ g_KeyDownTime[pKeyDef->virtualKey ] = KEY_DOWN_EVENT_FIRED;
180+ }
181+ }
182+ break ;
183+ }
184+ }
185+
186+ void ProcessLongPressKeyUp (const InputConfig* pKeyDef)
187+ {
188+ if (g_KeyDownTime[pKeyDef->virtualKey ] != KEY_DOWN_EVENT_FIRED &&
189+ g_KeyDownTime[pKeyDef->virtualKey ] != KEY_DOWN_UNSET)
190+ {
191+ const ULONGLONG tickCount = GetTickCount64 ();
192+ LogDebugMessage (" Detected LONGPRESS Key UP (too short): %d Tick: %d (Old Tick: %d)" , pKeyDef->virtualKey , tickCount, g_KeyDownTime[pKeyDef->virtualKey ]);
193+ // when a longpress "fails" just send the normal keypress
194+ SendInputKeypress (pKeyDef);
195+ }
196+ g_KeyDownTime[pKeyDef->virtualKey ] = KEY_DOWN_UNSET;
197+ }
0 commit comments