Skip to content

Commit ee6b3ae

Browse files
committed
Minor refactors in dll
Broke up the long LowLevelKeyboardProc (long press specifically)
1 parent 0291c0b commit ee6b3ae

File tree

5 files changed

+79
-70
lines changed

5 files changed

+79
-70
lines changed

KeyCapLib/KeyCaptureUtil.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424

2525
#include "keycapturestructs.h"
2626
#include "keycaptureutil.h"
27+
#include "keyboardinput.h"
28+
29+
UINT SendInputKeypress(const InputConfig* pKeyDef)
30+
{
31+
int nIndex = 0;
32+
INPUT inputBuffer[MAX_KEY_INPUT_PER_STROKE];
33+
memset(&inputBuffer, 0, sizeof(INPUT) * MAX_KEY_INPUT_PER_STROKE);
34+
AppendSingleKey(pKeyDef->virtualKey, &inputBuffer[nIndex++], 0);
35+
AppendSingleKey(pKeyDef->virtualKey, &inputBuffer[nIndex++], KEYEVENTF_KEYUP);
36+
LogDebugMessage("[Sending Input Keypress][virtualKey: %d]", pKeyDef->virtualKey);
37+
return SendInput(nIndex, inputBuffer, sizeof(INPUT));
38+
}
2739

2840
void ResetRemapEntryState(RemapEntryState* pRemapEntryState, BYTE bToggled)
2941
{

KeyCapLib/KeyCaptureUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ void ValidateStructs();
4040
char* GetBoolString(BYTE nValue);
4141
char* GetInputConfigDescription(InputConfig inputConfig);
4242
char* GetOutputConfigDescription(OutputConfig outputConfig);
43+
44+
UINT SendInputKeypress(const InputConfig* pKeyDef);
4345
#endif

KeyCapLib/keyboardproc.cpp

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3131
ULONGLONG g_KeyDownTime[WIN_KEY_COUNT];
3232

33+
void ProcessLongPressKeyDown(const InputConfig* pKeyDef, const RemapEntryContainerListItem* pKeyListItem);
34+
void ProcessLongPressKeyUp(const InputConfig* pKeyDef);
35+
3336
/*
3437
Implementation 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+
}

KeyCapLib/sendinputthread.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,4 @@ DWORD InitiateSendInput(RemapEntry* pRemapEntry, RemapEntryState* pRemapEntrySta
180180
free(pInputConfigDescription);
181181
#endif
182182
return 0;
183-
}
184-
185-
/*
186-
* Thread entry point for simple keypress (used for longpress configured keys to send the normal input key on key up)
187-
*/
188-
DWORD WINAPI SendInputKeypress(LPVOID lpParam)
189-
{
190-
InputConfig* inputConfig = static_cast<InputConfig*>(lpParam);
191-
int nIndex = 0;
192-
INPUT inputBuffer[MAX_KEY_INPUT_PER_STROKE];
193-
memset(&inputBuffer, 0, sizeof(INPUT) * MAX_KEY_INPUT_PER_STROKE);
194-
AppendSingleKey(inputConfig->virtualKey, &inputBuffer[nIndex++], 0);
195-
AppendSingleKey(inputConfig->virtualKey, &inputBuffer[nIndex++], KEYEVENTF_KEYUP);
196-
LogDebugMessage("[Sending Input Keypress][virtualKey: %d]", inputConfig->virtualKey);
197-
SendInput(nIndex, inputBuffer, sizeof(INPUT));
198-
return 0;
199183
}

KeyCapLib/sendinputthread.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@
2828
#include "stdafx.h"
2929

3030
DWORD WINAPI SendInputThread(LPVOID lpParam);
31-
DWORD WINAPI SendInputKeypress(LPVOID lpParam);
3231

3332
#endif

0 commit comments

Comments
 (0)