Skip to content

Commit 2a933d3

Browse files
committed
Refactor proc files into their own types
More log wrapping for debug. Overall needs more testing with mouse. Mouse is challenging if you have other apps that perform injections (mouse software for example). Will reorganize files and potentially rename things to cut down on confusion (inputs/outputs/blah).
1 parent 4a9d9d7 commit 2a933d3

18 files changed

+467
-249
lines changed

Format/MouseMapping.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// The MIT License (MIT)
3+
//
4+
// Copyright (c) 2023 Tim Stair
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
////////////////////////////////////////////////////////////////////////////////
24+
using System.Collections.Generic;
25+
using System.Windows.Forms;
26+
27+
namespace KeyCap.Format
28+
{
29+
public class MouseMapping
30+
{
31+
const byte VK_MBUTTON = 0x04;
32+
const byte VK_XBUTTON1 = 0x05;
33+
const byte VK_XBUTTON2 = 0x06;
34+
35+
private static Dictionary<MouseButtons, byte> s_mouseButtonsToKeyCode = new Dictionary<MouseButtons, byte>()
36+
{
37+
{MouseButtons.Middle, VK_MBUTTON },
38+
{MouseButtons.XButton1, VK_XBUTTON1 },
39+
{MouseButtons.XButton2, VK_XBUTTON2 },
40+
};
41+
42+
public static byte MapMouseButtonToKeyCode(MouseButtons eMouseButton)
43+
{
44+
if(s_mouseButtonsToKeyCode.TryGetValue(eMouseButton, out byte nKeyCode))
45+
{
46+
return nKeyCode;
47+
}
48+
49+
return 0;
50+
}
51+
}
52+
}

Forms/KeyCaptureConfig.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -401,19 +401,7 @@ private void btnMouseGeneric_Click(object sender, EventArgs e)
401401

402402
private void txtKeyIn_MouseDown(object sender, MouseEventArgs e)
403403
{
404-
byte keyCode = 0;
405-
switch (e.Button)
406-
{
407-
case MouseButtons.Middle:
408-
keyCode = 0x04; // VK_MBUTTON
409-
break;
410-
case MouseButtons.XButton1:
411-
keyCode = 0x05; // VK_XBUTTON1
412-
break;
413-
case MouseButtons.XButton2:
414-
keyCode = 0x06; // VK_XBUTTON2
415-
break;
416-
}
404+
byte keyCode = MouseMapping.MapMouseButtonToKeyCode(e.Button);
417405

418406
if (keyCode == 0)
419407
return; // unsupported

KeyCap.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<ItemGroup>
7171
<Compile Include="Format\BaseIOConfig.cs" />
7272
<Compile Include="Format\ConfigFileManager.cs" />
73+
<Compile Include="Format\MouseMapping.cs" />
7374
<Compile Include="Format\OutputConfig.cs" />
7475
<Compile Include="Format\InputConfig.cs" />
7576
<Compile Include="Format\RemapEntry.cs" />

KeyCapLib/KeyCapLib.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,27 @@
149149
</ItemDefinitionGroup>
150150
<ItemGroup>
151151
<ClInclude Include="configfile.h" />
152+
<ClInclude Include="inputproc.h" />
152153
<ClInclude Include="keyboardinput.h" />
153154
<ClInclude Include="keyboardproc.h" />
154155
<ClInclude Include="keycapture.h" />
155156
<ClInclude Include="keycapturestructs.h" />
156157
<ClInclude Include="keycaptureutil.h" />
157158
<ClInclude Include="mouseinput.h" />
159+
<ClInclude Include="mouseproc.h" />
158160
<ClInclude Include="sendinputthread.h" />
159161
<ClInclude Include="stdafx.h" />
160162
<ClInclude Include="targetver.h" />
161163
</ItemGroup>
162164
<ItemGroup>
163165
<ClCompile Include="configfile.cpp" />
166+
<ClCompile Include="inputproc.cpp" />
164167
<ClCompile Include="keyboardinput.cpp" />
165168
<ClCompile Include="keyboardproc.cpp" />
166169
<ClCompile Include="keycapture.cpp" />
167170
<ClCompile Include="keycaptureutil.cpp" />
168171
<ClCompile Include="mouseinput.cpp" />
172+
<ClCompile Include="mouseproc.cpp" />
169173
<ClCompile Include="sendinputthread.cpp" />
170174
</ItemGroup>
171175
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

KeyCapLib/KeyCapLib.vcxproj.filters

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<ClCompile Include="keycaptureutil.cpp" />
99
<ClCompile Include="mouseinput.cpp" />
1010
<ClCompile Include="sendinputthread.cpp" />
11+
<ClCompile Include="inputproc.cpp" />
12+
<ClCompile Include="mouseproc.cpp" />
1113
</ItemGroup>
1214
<ItemGroup>
1315
<ClInclude Include="stdafx.h" />
@@ -20,5 +22,7 @@
2022
<ClInclude Include="keycaptureutil.h" />
2123
<ClInclude Include="mouseinput.h" />
2224
<ClInclude Include="sendinputthread.h" />
25+
<ClInclude Include="inputproc.h" />
26+
<ClInclude Include="mouseproc.h" />
2327
</ItemGroup>
2428
</Project>

KeyCapLib/KeyCapture.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "keycapture.h"
2929
#include "keyboardproc.h"
30+
#include "mouseproc.h"
3031
#include "mouseinput.h"
3132
#include "configfile.h"
3233

@@ -42,10 +43,10 @@ extern "C"
4243
void InitiallizeEntryContainerListItem(RemapEntryContainerListItem* pKeyItem, RemapEntry* pEntry);
4344

4445
// sweet globals
45-
HHOOK g_hookKeyboard = NULL, g_hookMouse = NULL;
46+
HHOOK g_hookKeyboard = nullptr, g_hookMouse = nullptr;
4647
RemapEntryContainerListItem* g_KeyTranslationTable[WIN_KEY_COUNT];
47-
RemapEntry* g_KeyTranslationHead = NULL;
48-
void* g_KeyTranslationEnd = NULL; // pointer indicating the end of the input file data
48+
RemapEntry* g_KeyTranslationHead = nullptr;
49+
void* g_KeyTranslationEnd = nullptr; // pointer indicating the end of the input file data
4950

5051
/*
5152
Performs the load file operation and initiates the keyboard capture process
@@ -77,7 +78,7 @@ __declspec(dllexport) int LoadAndCaptureFromFile(HINSTANCE hInstance, char* sFil
7778

7879
// The translation table contains linked lists of all the output sets for a given key due to the flag keys (shift/alt/ctrl)
7980
// Example: input 'a' will be in the same list as 'shift+a' 'alt+a' 'ctrl+a' (or any combos like 'alt+shift+a')
80-
while(NULL != pEntry)
81+
while(nullptr != pEntry)
8182
{
8283
if(0 == pEntry->outputCount)
8384
{
@@ -91,25 +92,25 @@ __declspec(dllexport) int LoadAndCaptureFromFile(HINSTANCE hInstance, char* sFil
9192
free(pInputConfigDescription);
9293
#endif
9394
// if the entry doesn't exist yet for the given input vkey create a new one with a null next pointer
94-
if(NULL == g_KeyTranslationTable[pEntry->inputConfig.virtualKey])
95+
if(nullptr == g_KeyTranslationTable[pEntry->inputConfig.virtualKey])
9596
{
9697
g_KeyTranslationTable[pEntry->inputConfig.virtualKey] = (RemapEntryContainerListItem*)malloc(sizeof(RemapEntryContainerListItem));
9798
InitiallizeEntryContainerListItem(g_KeyTranslationTable[pEntry->inputConfig.virtualKey], pEntry);
98-
g_KeyTranslationTable[pEntry->inputConfig.virtualKey]->pNext = NULL;
99+
g_KeyTranslationTable[pEntry->inputConfig.virtualKey]->pNext = nullptr;
99100
}
100101

101102
// if the entry does exist create a new entry and append it to the existing linked list
102103
else
103104
{
104105
RemapEntryContainerListItem* pKeyItem = g_KeyTranslationTable[pEntry->inputConfig.virtualKey];
105-
while(NULL != pKeyItem->pNext)
106+
while(nullptr != pKeyItem->pNext)
106107
{
107108
pKeyItem = pKeyItem->pNext;
108109
}
109110
pKeyItem->pNext = (RemapEntryContainerListItem*)malloc(sizeof(RemapEntryContainerListItem));
110111
pKeyItem = pKeyItem->pNext;
111112
InitiallizeEntryContainerListItem(pKeyItem, pEntry);
112-
pKeyItem->pNext = NULL;
113+
pKeyItem->pNext = nullptr;
113114
}
114115
// jump to the next entry
115116
pEntry = (RemapEntry*)(&pEntry->outputCount + (sizeof(unsigned int) + (pEntry->outputCount * sizeof(OutputConfig))));
@@ -130,7 +131,7 @@ __declspec(dllexport) int LoadAndCaptureFromFile(HINSTANCE hInstance, char* sFil
130131
{
131132
// Note: This fails in VisualStudio if managed debugging is NOT enabled in the project(!)
132133
g_hookKeyboard = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, NULL);
133-
g_hookMouse = SetWindowsHookEx(WH_MOUSE_LL, LowLevelKeyboardProc, hInstance, NULL);
134+
g_hookMouse = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, hInstance, NULL);
134135
if(g_hookKeyboard && g_hookMouse)
135136
{
136137
return HOOK_CREATION_SUCCESS;
@@ -155,14 +156,14 @@ void ShutdownInputThreads(bool forceShutdown)
155156
// signal shutdown for all entries with a thread handle
156157
for (int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
157158
{
158-
if (NULL != g_KeyTranslationTable[nIdx])
159+
if (nullptr != g_KeyTranslationTable[nIdx])
159160
{
160161
RemapEntryContainerListItem* pListItem = g_KeyTranslationTable[nIdx];
161-
RemapEntryContainerListItem* pNextItem = NULL;
162-
while (NULL != pListItem)
162+
RemapEntryContainerListItem* pNextItem = nullptr;
163+
while (nullptr != pListItem)
163164
{
164165
pNextItem = pListItem->pNext;
165-
if (NULL != pListItem->pEntryContainer->pEntryState->threadHandle)
166+
if (nullptr != pListItem->pEntryContainer->pEntryState->threadHandle)
166167
{
167168
pListItem->pEntryContainer->pEntryState->bShutdown = true;
168169
}
@@ -177,14 +178,14 @@ void ShutdownInputThreads(bool forceShutdown)
177178
// monitor and terminate threads for all entries with a thread handle
178179
for (int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
179180
{
180-
if (NULL != g_KeyTranslationTable[nIdx])
181+
if (nullptr != g_KeyTranslationTable[nIdx])
181182
{
182183
RemapEntryContainerListItem* pListItem = g_KeyTranslationTable[nIdx];
183-
RemapEntryContainerListItem* pNextItem = NULL;
184-
while (NULL != pListItem)
184+
RemapEntryContainerListItem* pNextItem = nullptr;
185+
while (nullptr != pListItem)
185186
{
186187
pNextItem = pListItem->pNext;
187-
if (NULL != pListItem->pEntryContainer->pEntryState->threadHandle)
188+
if (nullptr != pListItem->pEntryContainer->pEntryState->threadHandle)
188189
{
189190
DWORD dwExitCode = WAIT_TIMEOUT;
190191
for (int shutdownIteration = 0; shutdownIteration < THREAD_SHUTDOWN_MAX_ATTEMPTS && dwExitCode != WAIT_OBJECT_0; shutdownIteration++)
@@ -217,31 +218,31 @@ __declspec(dllexport) void ShutdownCapture()
217218
{
218219
LogDebugMessage("Unhooked keyboard");
219220
UnhookWindowsHookEx(g_hookKeyboard);
220-
g_hookKeyboard = NULL;
221+
g_hookKeyboard = nullptr;
221222
}
222223
if (g_hookMouse)
223224
{
224225
LogDebugMessage("Unhooked mouse");
225226
UnhookWindowsHookEx(g_hookMouse);
226-
g_hookMouse = NULL;
227+
g_hookMouse = nullptr;
227228
}
228229

229230
// memory clean up
230-
if(NULL != g_KeyTranslationHead)
231+
if(nullptr != g_KeyTranslationHead)
231232
{
232233
LogDebugMessage("Cleared Memory!");
233234
free(g_KeyTranslationHead);
234-
g_KeyTranslationHead = NULL;
235+
g_KeyTranslationHead = nullptr;
235236
}
236237

237238
// clean up "hash" table
238239
for(int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
239240
{
240-
if(NULL != g_KeyTranslationTable[nIdx])
241+
if(nullptr != g_KeyTranslationTable[nIdx])
241242
{
242243
RemapEntryContainerListItem* pListItem = g_KeyTranslationTable[nIdx];
243-
RemapEntryContainerListItem* pNextItem = NULL;
244-
while(NULL != pListItem)
244+
RemapEntryContainerListItem* pNextItem = nullptr;
245+
while(nullptr != pListItem)
245246
{
246247
pNextItem = pListItem->pNext;
247248
free(pListItem->pEntryContainer->pEntryState);
@@ -250,11 +251,11 @@ __declspec(dllexport) void ShutdownCapture()
250251
free(pListItem);
251252
pListItem = pNextItem;
252253
}
253-
g_KeyTranslationTable[nIdx] = NULL;
254+
g_KeyTranslationTable[nIdx] = nullptr;
254255
}
255256
}
256257

257-
g_KeyTranslationEnd = NULL;
258+
g_KeyTranslationEnd = nullptr;
258259

259260
LogDebugMessage("Capture Shutdown");
260261
}

KeyCapLib/KeyCaptureUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void ResetRemapEntryState(RemapEntryState* pRemapEntryState, BYTE bToggled)
4242
pRemapEntryState->bToggled = bToggled;
4343
pRemapEntryState->bRepeating = false;
4444
pRemapEntryState->bShutdown = false;
45-
pRemapEntryState->threadHandle = NULL;
45+
pRemapEntryState->threadHandle = nullptr;
4646
}
4747

4848
bool IsButtonDownRequired(RemapEntryState* pRemapEntryState, OutputConfig* pKeyDef)

KeyCapLib/KeyboardInput.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ void SendInputKeys(RemapEntryState* pRemapEntryState, OutputConfig* pKeyDef)
9898

9999
if (pKeyDef->outputFlag.bToggle && pKeyDef->virtualKey >= MAX_VKEY)
100100
{
101+
#if _DEBUG
101102
LogDebugMessage("---- ERROR Cannot have a vkey value over 255: %d", pKeyDef->virtualKey);
103+
#endif
102104
return;
103105
}
104106

@@ -121,19 +123,25 @@ void SendInputKeys(RemapEntryState* pRemapEntryState, OutputConfig* pKeyDef)
121123
}
122124
if (!bSendKeyDown && !bSendKeyUp)
123125
{
126+
#if _DEBUG
124127
LogDebugMessage("Nothing to send for this keyout. Misconfigured output.");
128+
#endif
125129
return;
126130
}
127131

132+
#if _DEBUG
128133
LogDebugMessage("[Sending Inputs]");
129134
for (int nTemp = 0; nTemp < nIndex; nTemp++)
130135
{
131136
// TODO: support Shift+W toggle (the toggle ends up requiring detection of the shift key)
132137
LogDebugMessage("%s: %d", GetKeyFlagsString(inputBuffer[nTemp].ki.dwFlags), inputBuffer[nTemp].ki.wVk);
133138
}
139+
#endif
134140

135141
UINT inputsSent = SendInput(nIndex, inputBuffer, sizeof(INPUT));
142+
#if _DEBUG
136143
LogDebugMessage("[Sent Inputs] (%d/%d)", inputsSent, nIndex);
144+
#endif
137145
}
138146

139147
/*

KeyCapLib/configfile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int LoadFile(char* sFile, RemapEntry** ppKeyTranslationHead, void** ppKeyTransla
5656
DWORD dwBytesRead = 0;
5757
unsigned int headerBuffer[2];
5858
// READ THE HEADER
59-
if (ReadFile(hFile, headerBuffer, sizeof(headerBuffer), &dwBytesRead, NULL))
59+
if (ReadFile(hFile, headerBuffer, sizeof(headerBuffer), &dwBytesRead, nullptr))
6060
{
6161
if (dwBytesRead == sizeof(headerBuffer))
6262
{
@@ -80,7 +80,7 @@ int LoadFile(char* sFile, RemapEntry** ppKeyTranslationHead, void** ppKeyTransla
8080

8181
// read the entire file into memory
8282
// NOTE: This assumes the file is less than DWORD max in size(!)
83-
if (ReadFile(hFile, *ppKeyTranslationHead, dwBytesRemaining, &dwBytesRead, NULL))
83+
if (ReadFile(hFile, *ppKeyTranslationHead, dwBytesRemaining, &dwBytesRead, nullptr))
8484
{
8585
if (dwBytesRead == dwBytesRemaining) // verify everything was read in...
8686
{

0 commit comments

Comments
 (0)