@@ -350,7 +350,11 @@ private static unsafe HKL FindEnglishKeyboardLayout()
350
350
var handles = new HKL [ count ] ;
351
351
fixed ( HKL * h = handles )
352
352
{
353
- _ = PInvoke . GetKeyboardLayoutList ( count , h ) ;
353
+ var result = PInvoke . GetKeyboardLayoutList ( count , h ) ;
354
+ if ( result != 0 )
355
+ {
356
+ throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
357
+ }
354
358
}
355
359
356
360
// Look for any English keyboard layout
@@ -369,6 +373,11 @@ private static unsafe HKL FindEnglishKeyboardLayout()
369
373
return HKL . Null ;
370
374
}
371
375
376
+ /// <summary>
377
+ /// Switches the keyboard layout to English if available.
378
+ /// </summary>
379
+ /// <param name="backupPrevious">If true, the current keyboard layout will be stored for later restoration.</param>
380
+ /// <exception cref="Win32Exception">Thrown when there's an error getting the window thread process ID.</exception>
372
381
public static unsafe void SwitchToEnglishKeyboardLayout ( bool backupPrevious )
373
382
{
374
383
// Find an installed English layout
@@ -401,11 +410,17 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious)
401
410
PInvoke . ActivateKeyboardLayout ( enHKL , 0 ) ;
402
411
}
403
412
413
+ /// <summary>
414
+ /// Restores the previously backed-up keyboard layout.
415
+ /// If it wasn't backed up or has already been restored, this method does nothing.
416
+ /// </summary>
404
417
public static void RestorePreviousKeyboardLayout ( )
405
418
{
406
419
if ( _previousLayout == HKL . Null ) return ;
407
420
408
421
var hwnd = PInvoke . GetForegroundWindow ( ) ;
422
+ if ( hwnd == HWND . Null ) return ;
423
+
409
424
PInvoke . PostMessage (
410
425
hwnd ,
411
426
PInvoke . WM_INPUTLANGCHANGEREQUEST ,
0 commit comments