2222// SOFTWARE.
2323// //////////////////////////////////////////////////////////////////////////////
2424
25- // KeyCapture.cpp : Defines the entirety of the key capture... (probably should be broken up a bit)
25+ // KeyCapture.cpp : Defines the entry point for key capture and re-map
2626//
2727
2828#include " stdafx.h"
29- #include < stdlib.h>
30- #include < stdio.h>
31- #include < assert.h>
32- #include < winuser.h>
33-
34- // === consts and defines
35-
36- const int MAX_KEY_INPUT_PER_STROKE = 9 ; // control, alt (OR alt-up alt-down alt-up), shift, key, key-up, shift-up, alt-up, control-up
37-
38- const int HASH_TABLE_SIZE = 256 ;
39- const int MAX_VKEY = 256 ;
29+ #include " KeyCapture.h"
30+ #include " MouseInput.h"
31+ #include " KeyboardInput.h"
4032
4133// === enums
4234
@@ -49,51 +41,9 @@ enum HOOK_RESULT
4941 INPUT_BAD
5042};
5143
52- enum MOUSE_BUTTON
53- {
54- MOUSE_NONE = 0x00 ,
55- MOUSE_LEFT = 0x01 ,
56- MOUSE_RIGHT = 0x02 ,
57- MOUSE_MIDDLE = 0x03 ,
58- MOUSE_BUTTON_COUNT
59- };
60-
61- // === structs
62- struct KeyDefinition
63- {
64- #if 0
65- const int KEYCAP_SHIFT = 1 << 0;
66- const int KEYCAP_CONTROL = 1 << 1;
67- const int KEYCAP_ALT = 1 << 2;
68- const int KEYCAP_DONOTHING = 1 << 3;
69- const int KEYCAP_MOUSEOUT = 1 << 4;
70- const int KEYCAP_DELAY = 1 << 5;
71- const int KEYCAP_TOGGLE = 1 << 6;
72- #endif
73- unsigned char bShift : 1 ;
74- unsigned char bControl : 1 ;
75- unsigned char bAlt : 1 ;
76- unsigned char bDoNothing : 1 ;
77- unsigned char bMouseOut : 1 ;
78- unsigned char bDelay : 1 ;
79- unsigned char bToggle : 1 ;
80- unsigned char pad : 1 ;
81- unsigned char nVkKey : 8 ; // this stores mouse input too... TODO better name
82- };
83-
84- struct KeyTranslation
85- {
86- KeyDefinition kDef ;
87-
88- char nKDefOutput; // count of output key definitions
89- // KeyDefintion[nKDefOutput]
90- };
44+ // === consts and defines
9145
92- struct KeyTranslationListItem
93- {
94- KeyTranslation* pTrans;
95- KeyTranslationListItem* pNext;
96- };
46+ const int HASH_TABLE_SIZE = 256 ;
9747
9848// === prototypes
9949// avoid mangling the function names (necessary for export and usage in C#)
@@ -102,25 +52,15 @@ extern "C"
10252 __declspec (dllexport) int LoadAndCaptureFromFile(HINSTANCE hInstance, char * sFile );
10353 __declspec (dllexport) void ShutdownCapture();
10454}
55+
10556LRESULT CALLBACK LowLevelKeyboardProc (int nCode,WPARAM wParam,LPARAM lParam);
10657DWORD WINAPI SendInputThread ( LPVOID lpParam );
107- void SendInputMouse (KeyDefinition *pKeyDef);
10858void SendTriggerEndInputKeys (KeyDefinition* pTriggerDefinition);
109- void SendInputKeys (KeyDefinition* pKeyDef);
110- void AppendSingleKey (short keyScan, INPUT* inputChar, DWORD dwFlags);
111- void AppendSingleMouse (INPUT* inputChar, unsigned char nVkKey);
11259bool CheckKeyFlags (char nFlags, bool bAlt, bool bControl, bool bShift);
11360
11461// === sweet globals
115- unsigned char g_MouseDownMap[] = { 0 , MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_MIDDLEDOWN };
116- unsigned char g_MouseUpMap[] = { 0 , MOUSEEVENTF_LEFTUP, MOUSEEVENTF_RIGHTUP, MOUSEEVENTF_MIDDLEUP };
11762KeyTranslationListItem* g_KeyTranslationTable[HASH_TABLE_SIZE];
11863
119- // input histories for toggle usage
120- bool g_MouseToggleHistory[MOUSE_BUTTON_COUNT];
121- bool g_KeyToggleHistory[MAX_VKEY];
122-
123- INPUT g_inputBuffer[MAX_KEY_INPUT_PER_STROKE]; // keyboard output table (named input as it has the INPUT structs)
12464KeyTranslation* g_KeyTranslationHead = NULL ;
12565void * g_KeyTranslationEnd = NULL ; // pointer indicating the end of the input file data
12666HHOOK g_hookMain = NULL ;
@@ -434,227 +374,3 @@ DWORD WINAPI SendInputThread( LPVOID lpParam )
434374#endif
435375 return 0 ;
436376}
437-
438- /*
439- Sends the desired mouse input
440-
441- pKeyDef: pointer to a key definition for a mouse input
442- */
443- void SendInputMouse (KeyDefinition *pKeyDef)
444- {
445- if (pKeyDef->nVkKey == MOUSE_NONE)
446- {
447- return ;
448- }
449-
450- int nIndex = 0 ;
451-
452- if (pKeyDef->bToggle )
453- {
454- if (g_MouseToggleHistory[pKeyDef->nVkKey ])
455- {
456- AppendSingleMouse (&g_inputBuffer[nIndex++], g_MouseUpMap[pKeyDef->nVkKey ]);
457- }
458- else
459- {
460- AppendSingleMouse (&g_inputBuffer[nIndex++], g_MouseDownMap[pKeyDef->nVkKey ]);
461- }
462- g_MouseToggleHistory[pKeyDef->nVkKey ] = !g_MouseToggleHistory[pKeyDef->nVkKey ];
463- }
464- else
465- {
466- AppendSingleMouse (&g_inputBuffer[nIndex++], g_MouseDownMap[pKeyDef->nVkKey ]);
467- AppendSingleMouse (&g_inputBuffer[nIndex++], g_MouseUpMap[pKeyDef->nVkKey ]);
468- g_MouseToggleHistory[pKeyDef->nVkKey ] = false ;
469- }
470- #ifdef _DEBUG
471- char outputchar[256 ];
472- for (int nTemp = 0 ; nTemp < nIndex; nTemp++)
473- {
474- sprintf_s (outputchar, " SendingMouse: (flags)%x %d\n " , g_inputBuffer[nTemp].ki .dwFlags , g_inputBuffer[0 ].mi .dwFlags );
475- OutputDebugStringA (outputchar);
476- }
477- #endif
478- SendInput (nIndex, g_inputBuffer, sizeof (INPUT));
479- }
480-
481- /*
482- Sends the necessary inputs to complete the trigger (modifier keys)
483-
484- pTriggerDefinition: pointer to a key definition for the trigger
485- */
486- void SendTriggerEndInputKeys (KeyDefinition* pTriggerDefinition)
487- {
488- if (!pTriggerDefinition)
489- {
490- return ;
491- }
492- int nIndex = 0 ;
493-
494- if (pTriggerDefinition->bShift )
495- {
496- // check the required input key to see if this was pressed and force a keyup to eliminate it being passed on
497- // example: Shift + S >> f (without the keyup Shift + S >> F because shift is technically down)
498- // problem: multiple key up messages (?) ... does it matter?
499- // yes: slightly, Control + S >> x requires Control to be pressed again due to the keyup message (possible new flag?)
500- AppendSingleKey (VK_SHIFT, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
501- }
502-
503- if (pTriggerDefinition->bControl )
504- {
505- AppendSingleKey (VK_CONTROL, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
506- }
507-
508- if (pTriggerDefinition->bAlt )
509- {
510- // Note: Application menus become active with ALT is pressed, to get out of the menu alt must be "pressed" a second time
511- AppendSingleKey (VK_MENU, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
512- AppendSingleKey (VK_MENU, &g_inputBuffer[nIndex++], 0 );
513- AppendSingleKey (VK_MENU, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
514- }
515-
516- if (nIndex == 0 )
517- {
518- // no keys to send nothing to do
519- return ;
520- }
521-
522- #ifdef _DEBUG
523- char outputchar[256 ];
524- for (int nTemp = 0 ; nTemp < nIndex; nTemp++)
525- {
526- sprintf_s (outputchar, " Sending: (flags)%x %d\n " , g_inputBuffer[nTemp].ki .dwFlags , g_inputBuffer[nTemp].ki .wVk );
527- OutputDebugStringA (outputchar);
528- }
529- #endif
530- SendInput (nIndex, g_inputBuffer, sizeof (INPUT));
531- }
532-
533- /*
534- Sends the desired keyboard input in the necessary order to achieve the desired input (or at least try)
535-
536- pKeyDef: pointer to a key definition for a keyboard input to send
537- pTriggerDefinition: The definition of the triggering key. Modifiers like shift/alt/ctrl require special handling under
538- certain circumstances
539- */
540- void SendInputKeys (KeyDefinition* pKeyDef)
541- {
542- #ifdef _DEBUG
543- char outputchar[256 ];
544- #endif
545-
546- int nIndex = 0 ;
547-
548- bool bSendKeyDown = true ;
549- bool bSendKeyUp = true ;
550-
551- if (pKeyDef->bToggle )
552- {
553- if (pKeyDef->nVkKey >= MAX_VKEY)
554- {
555- sprintf_s (outputchar, " ---- ERROR Cannot have a vkey value over 255: %d\n " , pKeyDef->nVkKey );
556- OutputDebugStringA (outputchar);
557- }
558-
559- if (g_KeyToggleHistory[pKeyDef->nVkKey ])
560- {
561- bSendKeyDown = false ;
562- }
563- else
564- {
565- bSendKeyUp = false ;
566- }
567- g_KeyToggleHistory[pKeyDef->nVkKey ] = !g_KeyToggleHistory[pKeyDef->nVkKey ];
568- }
569-
570- if (bSendKeyDown)
571- {
572- if (pKeyDef->bShift )
573- {
574- AppendSingleKey (VK_SHIFT, &g_inputBuffer[nIndex++], 0 );
575- }
576-
577- if (pKeyDef->bControl )
578- {
579- AppendSingleKey (VK_CONTROL, &g_inputBuffer[nIndex++], 0 );
580- }
581-
582- if (pKeyDef->bAlt )
583- {
584- AppendSingleKey (VK_MENU, &g_inputBuffer[nIndex++], 0 );
585- }
586-
587- // output the actual key
588- #ifdef _DEBUG
589- sprintf_s (outputchar, " ---- Appended (down) %d\n " , pKeyDef->nVkKey );
590- OutputDebugStringA (outputchar);
591- #endif
592- AppendSingleKey (pKeyDef->nVkKey , &g_inputBuffer[nIndex++], 0 );
593- }
594-
595- if (bSendKeyUp)
596- {
597- #ifdef _DEBUG
598- sprintf_s (outputchar, " ---- Appended (up) %d\n " , pKeyDef->nVkKey );
599- OutputDebugStringA (outputchar);
600- #endif
601-
602- AppendSingleKey (pKeyDef->nVkKey , &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
603-
604- // setup any necessary key event up messages
605- if (pKeyDef->bShift )
606- {
607- AppendSingleKey (VK_SHIFT, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
608- }
609- if (pKeyDef->bControl )
610- {
611- AppendSingleKey (VK_CONTROL, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
612- }
613- if (pKeyDef->bAlt )
614- {
615- AppendSingleKey (VK_MENU, &g_inputBuffer[nIndex++], KEYEVENTF_KEYUP);
616- }
617- }
618- #ifdef _DEBUG
619- for (int nTemp = 0 ; nTemp < nIndex; nTemp++)
620- {
621- sprintf_s (outputchar, " Sending: (flags)%x %d\n " , g_inputBuffer[nTemp].ki .dwFlags , g_inputBuffer[nTemp].ki .wVk );
622- OutputDebugStringA (outputchar);
623- }
624- #endif
625- SendInput (nIndex, g_inputBuffer, sizeof (INPUT));
626- }
627-
628- /*
629- Assigns the desired keyboard input to send (see win32 INPUT documentation as it relates to SendInput)
630-
631- keyScan: the virtual key value to set
632- inputChar: The scan value to set
633- dwFlags: The flags value to set
634- */
635- void AppendSingleKey (short keyScan, INPUT* inputChar, DWORD dwFlags)
636- {
637- memset (inputChar, 0 , sizeof (INPUT));
638-
639- inputChar->type = INPUT_KEYBOARD;
640- inputChar->ki .wVk = LOBYTE (keyScan);
641- inputChar->ki .wScan = MapVirtualKey (LOBYTE (keyScan), 0 );
642- inputChar->ki .dwFlags = dwFlags;
643- }
644-
645- /*
646- Assigns the desired mouse input to send (see win32 INPUT documentation as it relates to SendInput)
647-
648- inputChar: The scan value to set
649- nVkKey: The key to set/send as input
650- */
651- void AppendSingleMouse (INPUT* inputChar, unsigned char nVkKey)
652- {
653- memset (inputChar, 0 , sizeof (INPUT));
654-
655- inputChar->type = INPUT_MOUSE;
656- inputChar->mi .dwExtraInfo = 0 ;
657- inputChar->mi .mouseData = 0 ;
658- inputChar->mi .dwExtraInfo = 0 ;
659- inputChar->mi .dwFlags = nVkKey;
660- }
0 commit comments