@ -29,10 +29,55 @@ constexpr decltype(Windows_Hook::DLL_NAME) Windows_Hook::DLL_NAME;
Windows_Hook * Windows_Hook : : _inst = nullptr ;
bool Windows_Hook : : StartHook ( std : : function < bool ( bool ) > & _key_combination_callback )
int ToggleKeyToNativeKey ( ingame_overlay : : ToggleKey k )
{
struct {
ingame_overlay : : ToggleKey lib_key ;
int native_key ;
} mapping [ ] = {
{ ingame_overlay : : ToggleKey : : ALT , VK_MENU } ,
{ ingame_overlay : : ToggleKey : : CTRL , VK_CONTROL } ,
{ ingame_overlay : : ToggleKey : : SHIFT , VK_SHIFT } ,
{ ingame_overlay : : ToggleKey : : TAB , VK_TAB } ,
{ ingame_overlay : : ToggleKey : : F1 , VK_F1 } ,
{ ingame_overlay : : ToggleKey : : F2 , VK_F2 } ,
{ ingame_overlay : : ToggleKey : : F3 , VK_F3 } ,
{ ingame_overlay : : ToggleKey : : F4 , VK_F4 } ,
{ ingame_overlay : : ToggleKey : : F5 , VK_F5 } ,
{ ingame_overlay : : ToggleKey : : F6 , VK_F6 } ,
{ ingame_overlay : : ToggleKey : : F7 , VK_F7 } ,
{ ingame_overlay : : ToggleKey : : F8 , VK_F8 } ,
{ ingame_overlay : : ToggleKey : : F9 , VK_F9 } ,
{ ingame_overlay : : ToggleKey : : F10 , VK_F10 } ,
{ ingame_overlay : : ToggleKey : : F11 , VK_F11 } ,
{ ingame_overlay : : ToggleKey : : F12 , VK_F12 } ,
} ;
for ( auto const & item : mapping )
{
if ( item . lib_key = = k )
return item . native_key ;
}
return 0 ;
}
bool Windows_Hook : : StartHook ( std : : function < bool ( bool ) > & _key_combination_callback , std : : set < ingame_overlay : : ToggleKey > const & toggle_keys )
{
if ( ! _Hooked )
{
if ( ! _key_combination_callback )
{
SPDLOG_ERROR ( " Failed to hook Windows: No key combination callback. " ) ;
return false ;
}
if ( toggle_keys . empty ( ) )
{
SPDLOG_ERROR ( " Failed to hook Windows: No key combination. " ) ;
return false ;
}
void * hUser32 = System : : Library : : GetLibraryHandle ( DLL_NAME ) ;
if ( hUser32 = = nullptr )
{
@ -71,6 +116,15 @@ bool Windows_Hook::StartHook(std::function<bool(bool)>& _key_combination_callbac
SPDLOG_INFO ( " Hooked Windows " ) ;
_KeyCombinationCallback = std : : move ( _key_combination_callback ) ;
for ( auto & key : toggle_keys )
{
uint32_t k = ToggleKeyToNativeKey ( key ) ;
if ( k ! = 0 )
{
_NativeKeyCombination . insert ( k ) ;
}
}
BeginHook ( ) ;
HookFuncs (
std : : make_pair < void * * , void * > ( & ( PVOID & ) GetRawInputBuffer , & Windows_Hook : : MyGetRawInputBuffer ) ,
@ -134,12 +188,9 @@ bool Windows_Hook::PrepareForOverlay(HWND hWnd)
POINT pos ;
if ( this - > GetCursorPos ( & pos ) & & ScreenToClient ( hWnd , & pos ) )
{
io . MousePos = ImVec2 ( ( float ) pos . x , ( float ) pos . y ) ;
io . AddMousePosEvent ( ( float ) pos . x , ( float ) pos . y ) ;
}
io . KeyCtrl = ( this - > GetKeyState ( VK_CONTROL ) & 0x8000 ) ! = 0 ;
io . KeyShift = ( this - > GetKeyState ( VK_SHIFT ) & 0x8000 ) ! = 0 ;
io . KeyAlt = ( this - > GetKeyState ( VK_MENU ) & 0x8000 ) ! = 0 ;
return true ;
}
@ -219,13 +270,18 @@ LRESULT CALLBACK Windows_Hook::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
if ( inst - > _Initialized )
{
// Is the event is a key press
if ( uMsg = = WM_KEYDOWN )
if ( uMsg = = WM_KEYDOWN | | uMsg = = WM_SYSKEYDOWN | | uMsg = = WM_KEYUP | | uMsg = = WM_SYSKEYUP )
{
// Tab is pressed and was not pressed before
if ( wParam = = VK_TAB & & ! ( lParam & ( 1 < < 30 ) ) )
int key_count = 0 ;
for ( auto const & key : inst - > _NativeKeyCombination )
{
// If Left Shift is pressed
if ( inst - > GetAsyncKeyState ( VK_LSHIFT ) & ( 1 < < 15 ) )
if ( inst - > GetAsyncKeyState ( key ) & ( 1 < < 15 ) )
+ + key_count ;
}
if ( key_count = = inst - > _NativeKeyCombination . size ( ) )
{ // All shortcut keys are pressed
if ( ! inst - > _KeyCombinationPushed )
{
if ( inst - > _KeyCombinationCallback ( true ) )
{
@ -234,12 +290,13 @@ LRESULT CALLBACK Windows_Hook::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
// so we can spoof the GetCursorPos return value.
inst - > GetCursorPos ( & inst - > _SavedCursorPos ) ;
}
else
{
clean_keys = true ;
}
inst - > _KeyCombinationPushed = true ;
}
}
else
{
inst - > _KeyCombinationPushed = false ;
}
}
if ( skip_input & & IgnoreMsg ( uMsg ) )
@ -248,7 +305,8 @@ LRESULT CALLBACK Windows_Hook::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
if ( clean_keys )
{
auto & io = ImGui : : GetIO ( ) ;
memset ( io . KeysDown , 0 , sizeof ( io . KeysDown ) ) ;
io . ClearInputKeys ( ) ;
io . ClearInputCharacters ( ) ;
}
return 0 ;
}
@ -369,6 +427,7 @@ Windows_Hook::Windows_Hook() :
_RecurseCallCount ( 0 ) ,
_GameHwnd ( nullptr ) ,
_GameWndProc ( nullptr ) ,
_KeyCombinationPushed ( false ) ,
GetRawInputBuffer ( nullptr ) ,
GetRawInputData ( nullptr ) ,
GetKeyState ( nullptr ) ,