11using ManagedShell . Common . Structs ;
22using ManagedShell . Interop ;
33using System . Collections . Generic ;
4- using System . Globalization ;
5- using System . Linq ;
4+ using System ;
65
76namespace ManagedShell . Common . Helpers
87{
98 public static class KeyboardLayoutHelper
109 {
11- public static KeyboardLayout GetKeyboardLayout ( bool currentThread = false )
10+ public static KeyboardLayout GetKeyboardLayout ( )
1211 {
13- uint threadId = 0 ;
14- if ( ! currentThread )
15- threadId = NativeMethods . GetWindowThreadProcessId ( NativeMethods . GetForegroundWindow ( ) , out _ ) ;
12+ uint threadId = NativeMethods . GetWindowThreadProcessId ( NativeMethods . GetForegroundWindow ( ) , out _ ) ;
1613 var layout = NativeMethods . GetKeyboardLayout ( threadId ) ;
1714
18- return new KeyboardLayout ( )
19- {
20- HKL = layout ,
21- NativeName = CultureInfo . GetCultureInfo ( ( short ) layout ) . NativeName ,
22- ThreeLetterName = CultureInfo . GetCultureInfo ( ( short ) layout ) . ThreeLetterISOLanguageName . ToUpper ( )
23- } ;
15+ return new KeyboardLayout ( ( uint ) layout ) ;
2416 }
2517
2618 public static List < KeyboardLayout > GetKeyboardLayoutList ( )
2719 {
20+ var keyboardLayouts = new List < KeyboardLayout > ( ) ;
21+
2822 var size = NativeMethods . GetKeyboardLayoutList ( 0 , null ) ;
29- var result = new long [ size ] ;
30- NativeMethods . GetKeyboardLayoutList ( size , result ) ;
23+ var layoutIds = new IntPtr [ size ] ;
24+ NativeMethods . GetKeyboardLayoutList ( layoutIds . Length , layoutIds ) ;
3125
32- return result . Select ( x => new KeyboardLayout ( )
26+ foreach ( var layoutId in layoutIds )
3327 {
34- HKL = ( int ) x ,
35- NativeName = CultureInfo . GetCultureInfo ( ( short ) x ) . NativeName ,
36- ThreeLetterName = CultureInfo . GetCultureInfo ( ( short ) x ) . ThreeLetterISOLanguageName . ToUpper ( )
37- } ) . ToList ( ) ;
28+ var keyboardLayout = new KeyboardLayout ( ( uint ) layoutId ) ;
29+ keyboardLayouts . Add ( keyboardLayout ) ;
30+ }
31+
32+ return keyboardLayouts ;
3833 }
3934
40- public static bool SetKeyboardLayout ( int layoutId )
35+ public static bool SetKeyboardLayout ( uint layoutId )
4136 {
42- return NativeMethods . PostMessage ( 0xffff ,
43- ( uint ) NativeMethods . WM . INPUTLANGCHANGEREQUEST ,
44- 0 ,
45- NativeMethods . LoadKeyboardLayout ( layoutId . ToString ( "x8" ) , ( uint ) ( NativeMethods . KLF . SUBSTITUTE_OK | NativeMethods . KLF . ACTIVATE ) ) ) ;
37+ return NativeMethods . PostMessage ( NativeMethods . GetForegroundWindow ( ) , ( int ) NativeMethods . WM . INPUTLANGCHANGEREQUEST , IntPtr . Zero , new IntPtr ( layoutId ) ) ;
4638 }
4739 }
48- }
40+ }
0 commit comments