@@ -330,6 +330,13 @@ internal static HWND GetWindowHandle(Window window, bool ensure = false)
330330 0x3009 , 0x3409 , 0x3C09 , 0x4009 , 0x4409 , 0x4809 , 0x4C09 ,
331331 } ;
332332
333+ private static readonly uint [ ] ImeLanguageIds =
334+ {
335+ 0x0004 , 0x7804 , 0x0804 , 0x1004 , 0x7C04 , 0x0C04 , 0x1404 , 0x0404 , 0x0011 , 0x0411 , 0x0012 , 0x0412 ,
336+ } ;
337+
338+ private const uint KeyboardLayoutLoWord = 0xFFFF ;
339+
333340 // Store the previous keyboard layout
334341 private static HKL _previousLayout ;
335342
@@ -350,7 +357,7 @@ private static unsafe HKL FindEnglishKeyboardLayout()
350357 foreach ( var hkl in handles )
351358 {
352359 // The lower word contains the language identifier
353- var langId = ( uint ) hkl . Value & 0xFFFF ;
360+ var langId = ( uint ) hkl . Value & KeyboardLayoutLoWord ;
354361
355362 // Check if it's an English layout
356363 if ( EnglishLanguageIds . Contains ( langId ) )
@@ -374,10 +381,18 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious)
374381 var threadId = PInvoke . GetWindowThreadProcessId ( PInvoke . GetForegroundWindow ( ) ) ;
375382 if ( threadId == 0 ) throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
376383
384+ // If the current layout has an IME mode, disable it without switching to another layout
385+ var currentLayout = PInvoke . GetKeyboardLayout ( threadId ) ;
386+ var currentLayoutCode = ( uint ) currentLayout . Value & KeyboardLayoutLoWord ;
387+ if ( ImeLanguageIds . Contains ( currentLayoutCode ) )
388+ {
389+ return ;
390+ }
391+
377392 // Backup current keyboard layout
378393 if ( backupPrevious )
379394 {
380- _previousLayout = PInvoke . GetKeyboardLayout ( threadId ) ;
395+ _previousLayout = currentLayout ;
381396 }
382397
383398 // Switch to English layout
0 commit comments