2017-03-03 31 views
0

我想知道當前輸入語言的佈局(鍵盤佈局)在Windows操作系統上何時更改。我試圖用這個工作 -用於更改當前輸入語言的佈局名稱的Windows事件

InputLanguageManager.Current.InputLanguageChanged += Current_InputLanguageChanged; 

但此事件不上來,如果我從葡萄牙語(巴西),葡萄牙語(巴西ABNT) ---->葡萄牙語(巴西改變)美國 - 國際

另一個例子是,當我從英語(美國改變)美國 ---->英語(美國)德國

+0

你看過http://stackoverflow.com/questions/10299659/is-it-possible-to-detect-keyboard-focus-events-globally ?? – BugFinder

+0

你能告訴我們手動改變當前輸入語言的步驟嗎? –

+0

我使用Windows 10操作系統,並添加了以下鍵盤佈局:*語言首選項 - >選擇其中一種語言 - >單擊選項 - >添加鍵盤*。現在在您的語言選項中,當您在兩個佈局名稱(例如: - 葡萄牙語(巴西ABNT)和美國 - 國際)之間切換時,則不會引發事件* InputLanguageChanged *。 –

回答

0

基於thisthis,您可以使用下面的代碼:

public partial class MainWindow : Window 
{ 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern IntPtr GetKeyboardLayout(uint idThread); 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    private static extern IntPtr GetForegroundWindow(); 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     Task.Factory.StartNew(() => 
     { 
      while (true) 
      { 
       HandleCurrentLanguage(); 
       System.Threading.Thread.Sleep(500); 
      } 
     }); 
    } 


    IntPtr _currentKeyboardLayout = IntPtr.Zero; 
    private void HandleCurrentLanguage() 
    { 
     var newLayout = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)); 
     if (_currentKeyboardLayout != newLayout) 
     { 
      _currentKeyboardLayout = newLayout; 
      // do something 
     } 
    } 
} 

請注意,結果轉換的GetKeyboardLayoutCultureInfo爲我提供的第二個鏈接的建議,並沒有爲我工作。

+0

請注意,代替Task.Factory.StartNew,您可以檢查keydown和mousedoen事件中的情況。 – Ron