2020-03-24 01:44:02 +08:00
# include "pch.h"
# include "KeyboardManagerState.h"
// Constructor
KeyboardManagerState : : KeyboardManagerState ( ) :
2020-04-09 05:31:31 +08:00
uiState ( KeyboardManagerUIState : : Deactivated ) , currentUIWindow ( nullptr ) , currentShortcutUI ( nullptr ) , currentSingleKeyUI ( nullptr ) , detectedRemapKey ( NULL )
2020-03-24 01:44:02 +08:00
{
}
// Function to check the if the UI state matches the argument state. For states with activated windows it also checks if the window is in focus.
bool KeyboardManagerState : : CheckUIState ( KeyboardManagerUIState state )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( uiState_mutex ) ;
2020-03-24 01:44:02 +08:00
if ( uiState = = state )
{
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > lock ( currentUIWindow_mutex ) ;
2020-03-24 01:44:02 +08:00
if ( uiState = = KeyboardManagerUIState : : Deactivated )
{
return true ;
}
// If the UI state is activated then we also have to ensure that the UI window is in focus.
// GetForegroundWindow can be used here since we only need to check the main parent window and not the sub windows within the content dialog. Using GUIThreadInfo will give more specific sub-windows within the XAML window which is not needed.
else if ( currentUIWindow = = GetForegroundWindow ( ) )
{
return true ;
}
}
return false ;
}
// Function to set the window handle of the current UI window that is activated
void KeyboardManagerState : : SetCurrentUIWindow ( HWND windowHandle )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( currentUIWindow_mutex ) ;
2020-03-24 01:44:02 +08:00
currentUIWindow = windowHandle ;
}
// Function to set the UI state. When a window is activated, the handle to the window can be passed in the windowHandle argument.
void KeyboardManagerState : : SetUIState ( KeyboardManagerUIState state , HWND windowHandle )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( uiState_mutex ) ;
2020-03-24 01:44:02 +08:00
uiState = state ;
2020-04-04 01:57:46 +08:00
SetCurrentUIWindow ( windowHandle ) ;
2020-03-24 01:44:02 +08:00
}
// Function to reset the UI state members
void KeyboardManagerState : : ResetUIState ( )
{
SetUIState ( KeyboardManagerUIState : : Deactivated ) ;
2020-04-04 01:57:46 +08:00
// Reset the shortcut UI stored variables
2020-04-09 05:31:31 +08:00
std : : unique_lock < std : : mutex > currentShortcutUI_lock ( currentShortcutUI_mutex ) ;
currentShortcutUI = nullptr ;
currentShortcutUI_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > detectedShortcut_lock ( detectedShortcut_mutex ) ;
2020-04-09 00:11:58 +08:00
detectedShortcut . Reset ( ) ;
2020-04-04 01:57:46 +08:00
detectedShortcut_lock . unlock ( ) ;
2020-03-27 23:38:58 +08:00
2020-04-10 00:20:19 +08:00
std : : unique_lock < std : : mutex > currentShortcut_lock ( currentShortcut_mutex ) ;
currentShortcut . Reset ( ) ;
currentShortcut_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
// Reset all the single key remap UI stored variables.
2020-04-09 05:31:31 +08:00
std : : unique_lock < std : : mutex > currentSingleKeyUI_lock ( currentSingleKeyUI_mutex ) ;
currentSingleKeyUI = nullptr ;
currentSingleKeyUI_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > detectedRemapKey_lock ( detectedRemapKey_mutex ) ;
2020-03-27 23:38:58 +08:00
detectedRemapKey = NULL ;
2020-04-04 01:57:46 +08:00
detectedRemapKey_lock . unlock ( ) ;
2020-03-24 01:44:02 +08:00
}
// Function to clear the OS Level shortcut remapping table
void KeyboardManagerState : : ClearOSLevelShortcuts ( )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( osLevelShortcutReMap_mutex ) ;
2020-03-24 01:44:02 +08:00
osLevelShortcutReMap . clear ( ) ;
}
2020-03-27 23:38:58 +08:00
// Function to clear the Keys remapping table.
void KeyboardManagerState : : ClearSingleKeyRemaps ( )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( singleKeyReMap_mutex ) ;
2020-03-27 23:38:58 +08:00
singleKeyReMap . clear ( ) ;
}
2020-03-24 01:44:02 +08:00
// Function to add a new OS level shortcut remapping
2020-04-09 00:11:58 +08:00
bool KeyboardManagerState : : AddOSLevelShortcut ( const Shortcut & originalSC , const Shortcut & newSC )
2020-03-24 01:44:02 +08:00
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( osLevelShortcutReMap_mutex ) ;
// Check if the shortcut is already remapped
auto it = osLevelShortcutReMap . find ( originalSC ) ;
if ( it ! = osLevelShortcutReMap . end ( ) )
{
return false ;
}
2020-04-09 00:11:58 +08:00
osLevelShortcutReMap [ originalSC ] = RemapShortcut ( newSC ) ;
2020-04-04 01:57:46 +08:00
return true ;
2020-03-24 01:44:02 +08:00
}
2020-03-27 23:38:58 +08:00
// Function to add a new OS level shortcut remapping
2020-04-09 00:11:58 +08:00
bool KeyboardManagerState : : AddSingleKeyRemap ( const DWORD & originalKey , const DWORD & newRemapKey )
2020-03-27 23:38:58 +08:00
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( singleKeyReMap_mutex ) ;
// Check if the key is already remapped
auto it = singleKeyReMap . find ( originalKey ) ;
if ( it ! = singleKeyReMap . end ( ) )
{
return false ;
}
2020-03-27 23:38:58 +08:00
singleKeyReMap [ originalKey ] = newRemapKey ;
2020-04-04 01:57:46 +08:00
return true ;
2020-03-27 23:38:58 +08:00
}
2020-03-24 01:44:02 +08:00
// Function to set the textblock of the detect shortcut UI so that it can be accessed by the hook
2020-04-09 05:31:31 +08:00
void KeyboardManagerState : : ConfigureDetectShortcutUI ( const StackPanel & textBlock )
2020-03-24 01:44:02 +08:00
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > lock ( currentShortcutUI_mutex ) ;
currentShortcutUI = textBlock ;
2020-03-24 01:44:02 +08:00
}
2020-03-27 23:38:58 +08:00
// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
2020-04-09 05:31:31 +08:00
void KeyboardManagerState : : ConfigureDetectSingleKeyRemapUI ( const StackPanel & textBlock )
2020-03-27 23:38:58 +08:00
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > lock ( currentSingleKeyUI_mutex ) ;
currentSingleKeyUI = textBlock ;
}
void KeyboardManagerState : : AddKeyToLayout ( const StackPanel & panel , const hstring & key )
{
// Textblock to display the detected key
TextBlock remapKey ;
Border border ;
border . Padding ( { 20 , 10 , 20 , 10 } ) ;
border . Margin ( { 0 , 0 , 10 , 0 } ) ;
border . Background ( Windows : : UI : : Xaml : : Media : : SolidColorBrush { Windows : : UI : : Colors : : LightGray ( ) } ) ;
remapKey . Foreground ( Windows : : UI : : Xaml : : Media : : SolidColorBrush { Windows : : UI : : Colors : : Black ( ) } ) ;
remapKey . FontSize ( 20 ) ;
border . HorizontalAlignment ( HorizontalAlignment : : Left ) ;
border . Child ( remapKey ) ;
remapKey . Text ( key ) ;
panel . Children ( ) . Append ( border ) ;
2020-03-27 23:38:58 +08:00
}
2020-03-24 01:44:02 +08:00
// Function to update the detect shortcut UI based on the entered keys
void KeyboardManagerState : : UpdateDetectShortcutUI ( )
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > currentShortcutUI_lock ( currentShortcutUI_mutex ) ;
if ( currentShortcutUI = = nullptr )
2020-03-24 01:44:02 +08:00
{
return ;
}
2020-04-09 05:31:31 +08:00
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > detectedShortcut_lock ( detectedShortcut_mutex ) ;
2020-04-10 00:20:19 +08:00
std : : unique_lock < std : : mutex > currentShortcut_lock ( currentShortcut_mutex ) ;
// Save the latest displayed shortcut
currentShortcut = detectedShortcut ;
currentShortcut_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
detectedShortcut_lock . unlock ( ) ;
2020-03-24 01:44:02 +08:00
// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
2020-04-09 09:14:22 +08:00
currentShortcutUI . Dispatcher ( ) . RunAsync ( Windows : : UI : : Core : : CoreDispatcherPriority : : Normal , [ this ] ( ) {
std : : vector < hstring > shortcut = detectedShortcut . GetKeyVector ( keyboardMap ) ;
2020-04-09 05:31:31 +08:00
currentShortcutUI . Children ( ) . Clear ( ) ;
for ( auto & key : shortcut )
{
AddKeyToLayout ( currentShortcutUI , key ) ;
}
currentShortcutUI . UpdateLayout ( ) ;
2020-03-24 01:44:02 +08:00
} ) ;
}
2020-03-27 23:38:58 +08:00
// Function to update the detect remap key UI based on the entered key.
void KeyboardManagerState : : UpdateDetectSingleKeyRemapUI ( )
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > currentSingleKeyUI_lock ( currentSingleKeyUI_mutex ) ;
if ( currentSingleKeyUI = = nullptr )
2020-03-27 23:38:58 +08:00
{
return ;
}
// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
2020-04-09 09:14:22 +08:00
currentSingleKeyUI . Dispatcher ( ) . RunAsync ( Windows : : UI : : Core : : CoreDispatcherPriority : : Normal , [ this ] ( ) {
2020-04-09 05:31:31 +08:00
currentSingleKeyUI . Children ( ) . Clear ( ) ;
2020-04-09 09:14:22 +08:00
hstring key = winrt : : to_hstring ( keyboardMap . GetKeyName ( detectedRemapKey ) . c_str ( ) ) ;
2020-04-09 05:31:31 +08:00
AddKeyToLayout ( currentSingleKeyUI , key ) ;
currentSingleKeyUI . UpdateLayout ( ) ;
2020-03-27 23:38:58 +08:00
} ) ;
}
2020-03-24 01:44:02 +08:00
// Function to return the currently detected shortcut which is displayed on the UI
2020-04-09 00:11:58 +08:00
Shortcut KeyboardManagerState : : GetDetectedShortcut ( )
2020-03-24 01:44:02 +08:00
{
2020-04-10 00:20:19 +08:00
std : : lock_guard < std : : mutex > lock ( currentShortcut_mutex ) ;
return currentShortcut ;
2020-03-24 01:44:02 +08:00
}
2020-03-27 23:38:58 +08:00
// Function to return the currently detected remap key which is displayed on the UI
2020-04-04 01:57:46 +08:00
DWORD KeyboardManagerState : : GetDetectedSingleRemapKey ( )
2020-03-27 23:38:58 +08:00
{
2020-04-10 00:20:19 +08:00
std : : lock_guard < std : : mutex > lock ( detectedRemapKey_mutex ) ;
return detectedRemapKey ;
2020-03-27 23:38:58 +08:00
}
2020-03-24 01:44:02 +08:00
// Function which can be used in HandleKeyboardHookEvent before the single key remap event to use the UI and suppress events while the remap window is active.
2020-03-27 23:38:58 +08:00
bool KeyboardManagerState : : DetectSingleRemapKeyUIBackend ( LowlevelKeyboardEvent * data )
2020-03-24 01:44:02 +08:00
{
// Check if the detect key UI window has been activated
2020-03-27 23:38:58 +08:00
if ( CheckUIState ( KeyboardManagerUIState : : DetectSingleKeyRemapWindowActivated ) )
2020-03-24 01:44:02 +08:00
{
2020-04-04 01:57:46 +08:00
// detect the key if it is pressed down
if ( data - > wParam = = WM_KEYDOWN | | data - > wParam = = WM_SYSKEYDOWN )
{
std : : unique_lock < std : : mutex > detectedRemapKey_lock ( detectedRemapKey_mutex ) ;
detectedRemapKey = data - > lParam - > vkCode ;
detectedRemapKey_lock . unlock ( ) ;
UpdateDetectSingleKeyRemapUI ( ) ;
}
2020-03-24 01:44:02 +08:00
// Suppress the keyboard event
return true ;
}
return false ;
}
// Function which can be used in HandleKeyboardHookEvent before the os level shortcut remap event to use the UI and suppress events while the remap window is active.
bool KeyboardManagerState : : DetectShortcutUIBackend ( LowlevelKeyboardEvent * data )
{
// Check if the detect shortcut UI window has been activated
if ( CheckUIState ( KeyboardManagerUIState : : DetectShortcutWindowActivated ) )
{
// Add the key if it is pressed down
if ( data - > wParam = = WM_KEYDOWN | | data - > wParam = = WM_SYSKEYDOWN )
{
2020-04-09 00:11:58 +08:00
// Set the new key and store if a change occured
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > lock ( detectedShortcut_mutex ) ;
2020-04-09 00:11:58 +08:00
bool updateUI = detectedShortcut . SetKey ( data - > lParam - > vkCode ) ;
lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
2020-04-09 00:11:58 +08:00
if ( updateUI )
{
2020-03-24 01:44:02 +08:00
// Update the UI. This function is called here because it should store the set of keys pressed till the last key which was pressed down.
UpdateDetectShortcutUI ( ) ;
}
}
// Remove the key if it has been released
else if ( data - > wParam = = WM_KEYUP | | data - > wParam = = WM_SYSKEYUP )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( detectedShortcut_mutex ) ;
2020-04-09 00:11:58 +08:00
detectedShortcut . ResetKey ( data - > lParam - > vkCode ) ;
2020-03-24 01:44:02 +08:00
}
// Suppress the keyboard event
return true ;
}
// If the detect shortcut UI window is not activated, then clear the shortcut buffer if it isn't empty
2020-04-04 01:57:46 +08:00
else
2020-03-24 01:44:02 +08:00
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( detectedShortcut_mutex ) ;
2020-04-09 00:11:58 +08:00
if ( ! detectedShortcut . IsEmpty ( ) )
2020-04-04 01:57:46 +08:00
{
2020-04-09 00:11:58 +08:00
detectedShortcut . Reset ( ) ;
2020-04-04 01:57:46 +08:00
}
2020-03-24 01:44:02 +08:00
}
return false ;
}