@@ -16,34 +16,30 @@ class HotKeyGlobal {
1616
1717 static Thread _thread_ ;
1818 static uint _threadId_ ;
19- static int _baseId_ ;
2019 static List < int > _hotKeys_ = new List < int > ( 5 ) ;
2120 static List < Action > _actions_ = new List < Action > ( 5 ) ;
22- static int _registeredCount_ = 0 ;
2321
24- public static void Subscribe ( int modifier , int virtualKey , Action action ) {
22+ public static void DefineHotKey ( int modifier , int virtualKey , Action action ) {
2523 if ( _thread_ == null ) {
2624 _thread_ = new Thread ( RunMessagePump ) ;
27- _thread_ . SetApartmentState ( ApartmentState . STA ) ;
2825 _thread_ . IsBackground = true ;
26+ _thread_ . Start ( ) ;
2927 lock ( _thread_ ) {
30- _thread_ . Start ( ) ;
3128 Monitor . Wait ( _thread_ ) ;
3229 }
3330 }
3431
35- int hotKey = ( virtualKey << 16 ) | modifier ;
32+ int hotKey = ( virtualKey << 16 ) | ( modifier & 0xFFFF ) ;
3633 _hotKeys_ . Add ( hotKey ) ;
3734 _actions_ . Add ( action ) ;
3835
3936 PostThreadMessage ( WM_USER_HOTKEY_REGISTER , modifier , virtualKey ) ;
4037 }
4138
42- public static void SubscribeAgain ( ) {
43- _registeredCount_ = 0 ;
44- for ( int i = _actions_ . Count ; i -- > 0 ; ) {
45- int modifierCode = _hotKeys_ [ i ] & 0xFFFF ;
46- int virtualKeyCode = _hotKeys_ [ i ] >> 16 ;
39+ public static void SubscribeAll ( ) {
40+ foreach ( int hotkey in _hotKeys_ ) {
41+ int modifierCode = hotkey & 0xFFFF ;
42+ int virtualKeyCode = hotkey >> 16 ;
4743 PostThreadMessage ( WM_USER_HOTKEY_REGISTER , modifierCode , virtualKeyCode ) ;
4844 }
4945 }
@@ -62,39 +58,42 @@ static void EvalHotKey(uint hotkey) {
6258
6359 static void RunMessagePump ( ) {
6460 _threadId_ = Native . GetCurrentThreadId ( ) ;
65- _baseId_ = ( int ) ( _threadId_ & 0x0FFF ) ;
66- //signal the message pump is up and running
61+
62+ // signal that the message pump is up and running
6763 lock ( _thread_ ) {
6864 Monitor . Pulse ( _thread_ ) ;
6965 }
66+
7067 //run message pump
7168 try {
7269 DispatchThreadMessages ( ) ;
7370 } catch { }
7471 }
7572
7673 static void DispatchThreadMessages ( ) {
74+ uint atom = ( ( _threadId_ ^ ( _threadId_ >> 16 ) ) & 0x0ffff ) - 25 ;
75+ uint count = 0 ;
7776 var msg = new Native . Message ( ) ;
7877 while ( Native . GetMessage ( ref msg , ( IntPtr ) 0 , 0 , 0 ) > 0 ) {
79- switch ( msg . Msg ) {
80- case WM_HOTKEY :
81- EvalHotKey ( ( uint ) msg . LParam ) ; // lword=modifiers, hword=virtualKey
82- break ;
78+ switch ( msg . Msg ) {
8379 case WM_USER_HOTKEY_REGISTER :
8480 bool rhk = Native . RegisterHotKey ( IntPtr . Zero ,
85- _baseId_ + _registeredCount_ + 1 ,
81+ atom + count ,
8682 ( uint ) msg . WParam ,
8783 ( uint ) msg . LParam ) ;
8884 if ( rhk ) {
89- _registeredCount_ ++ ;
85+ count ++ ;
9086 }
9187 break ;
9288 case WM_USER_HOTKEY_UNREGISTER_ALL :
93- while ( _registeredCount_ > 0 ) {
94- Native . UnregisterHotKey ( IntPtr . Zero , _baseId_ + _registeredCount_ ) ;
95- _registeredCount_ -- ;
89+ while ( count > 0 ) {
90+ count -- ;
91+ Native . UnregisterHotKey ( IntPtr . Zero , atom + count ) ;
9692 }
9793 break ;
94+ case WM_HOTKEY :
95+ EvalHotKey ( ( uint ) msg . LParam ) ; // lword=modifiers, hword=virtualKey
96+ break ;
9897 }
9998 }
10099 }
@@ -112,10 +111,10 @@ static class Native {
112111 public const string USER32 = "user32.dll" ;
113112
114113 [ DllImport ( USER32 ) ]
115- public static extern bool RegisterHotKey ( IntPtr hWnd , int id , uint fsModifiers , uint vk ) ;
114+ public static extern bool RegisterHotKey ( IntPtr hWnd , uint id , uint fsModifiers , uint vk ) ;
116115
117116 [ DllImport ( USER32 ) ]
118- public static extern bool UnregisterHotKey ( IntPtr hWnd , int id ) ;
117+ public static extern bool UnregisterHotKey ( IntPtr hWnd , uint id ) ;
119118
120119 [ DllImport ( KERNEL32 , SetLastError = false ) ]
121120 public static extern uint GetCurrentThreadId ( ) ;
0 commit comments