1+ using EddiConfigService ;
2+ using System ;
3+ using System . Runtime . InteropServices ;
4+ using System . Windows . Input ;
5+ using Utilities ;
6+
7+ namespace EddiCore . Hotkeys
8+ {
9+ public class HotkeyRegistration
10+ {
11+ public HotkeyActionCollection Collection { get ; }
12+
13+ private int hotkeyIdCounter ;
14+ private readonly IntPtr handle ;
15+
16+ public HotkeyRegistration ( IntPtr handle , HotkeyActionCollection collection )
17+ {
18+ this . handle = handle ;
19+ Collection = collection ;
20+ }
21+
22+ [ DllImport ( "user32.dll" ) ]
23+ private static extern bool RegisterHotKey ( IntPtr hWnd , int id , uint fsModifiers , uint vk ) ;
24+
25+ internal void RegisterAll ( )
26+ {
27+ foreach ( var configuredHotkey in ConfigService . Instance . eddiConfiguration . GetHotkeysCopy ( ) )
28+ {
29+ if ( Collection . TryGetValue ( configuredHotkey . Key , out var hotkeyAction ) )
30+ {
31+ RegisterHotkey ( hotkeyAction . Name , HotkeyConverter . FromString ( configuredHotkey . Value ) ) ;
32+ }
33+ }
34+ }
35+
36+ public void RegisterHotkey ( string name , KeyGesture keyGesture )
37+ {
38+ try
39+ {
40+ if ( TryRegisterHotkey ( name , keyGesture , out var id ) )
41+ {
42+ Collection . AddGesture ( name , keyGesture , id ) ;
43+ ConfigService . Instance . eddiConfiguration . AddHotkey ( name , HotkeyConverter . ToString ( keyGesture ) ) ;
44+ }
45+ else
46+ {
47+ throw new InvalidOperationException ( "Hotkey registration failed." ) ;
48+ }
49+ }
50+ catch ( Exception e )
51+ {
52+ Logging . Error ( e . Message , e ) ;
53+ }
54+ }
55+
56+ private bool TryRegisterHotkey ( string name , KeyGesture keyGesture , out int id )
57+ {
58+ if ( string . IsNullOrWhiteSpace ( name ) )
59+ {
60+ throw new ArgumentNullException ( nameof ( name ) ) ;
61+ }
62+
63+ if ( keyGesture == null )
64+ {
65+ throw new ArgumentNullException ( nameof ( keyGesture ) ) ;
66+ }
67+
68+ // Unregister previous hotkey for this name, if any
69+ TryUnregisterHotkey ( name ) ;
70+
71+ // Register a new hotkey
72+ var modifiers = ( uint ) keyGesture . Modifiers ;
73+ var key = ( uint ) KeyInterop . VirtualKeyFromKey ( keyGesture . Key ) ;
74+ id = hotkeyIdCounter ++ ;
75+ return RegisterHotKey ( handle , id , modifiers , key ) ;
76+ }
77+
78+ private bool TryUnregisterHotkey ( string name )
79+ {
80+ // Unregister previous hotkey for this name, if any
81+ if ( Collection . TryGetValue ( name , out var hotkeyAction ) && hotkeyAction . id is int oldId )
82+ {
83+ return UnregisterHotKey ( handle , oldId ) ;
84+ }
85+
86+ return false ;
87+ }
88+
89+ [ DllImport ( "user32.dll" ) ]
90+ private static extern bool UnregisterHotKey ( IntPtr hWnd , int id ) ;
91+
92+ public void UnregisterHotkey ( string name )
93+ {
94+ if ( TryUnregisterHotkey ( name ) )
95+ {
96+ Collection . RemoveKeyGestures ( name ) ;
97+ ConfigService . Instance . eddiConfiguration . RemoveHotkey ( name ) ;
98+ }
99+ }
100+
101+ internal void UnregisterAll ( )
102+ {
103+ foreach ( var hotkeyAction in Collection . HotkeyActions )
104+ {
105+ if ( hotkeyAction . id is int id )
106+ {
107+ UnregisterHotKey ( handle , id ) ;
108+ }
109+ }
110+
111+ Collection . ClearAllKeyGestures ( ) ;
112+ }
113+ }
114+ }
0 commit comments