@@ -330,6 +330,13 @@ internal static HWND GetWindowHandle(Window window, bool ensure = false)
330
330
0x3009 , 0x3409 , 0x3C09 , 0x4009 , 0x4409 , 0x4809 , 0x4C09 ,
331
331
} ;
332
332
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
+
333
340
// Store the previous keyboard layout
334
341
private static HKL _previousLayout ;
335
342
@@ -350,7 +357,7 @@ private static unsafe HKL FindEnglishKeyboardLayout()
350
357
foreach ( var hkl in handles )
351
358
{
352
359
// The lower word contains the language identifier
353
- var langId = ( uint ) hkl . Value & 0xFFFF ;
360
+ var langId = ( uint ) hkl . Value & KeyboardLayoutLoWord ;
354
361
355
362
// Check if it's an English layout
356
363
if ( EnglishLanguageIds . Contains ( langId ) )
@@ -374,10 +381,18 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious)
374
381
var threadId = PInvoke . GetWindowThreadProcessId ( PInvoke . GetForegroundWindow ( ) ) ;
375
382
if ( threadId == 0 ) throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
376
383
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
+
377
392
// Backup current keyboard layout
378
393
if ( backupPrevious )
379
394
{
380
- _previousLayout = PInvoke . GetKeyboardLayout ( threadId ) ;
395
+ _previousLayout = currentLayout ;
381
396
}
382
397
383
398
// Switch to English layout
0 commit comments