码迷,mamicode.com
首页 > Windows程序 > 详细

C#: Get current keyboard layout\input language

时间:2018-09-28 12:43:27      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:lte   useful   eth   ica   catch   input   exception   pointer   func   

原文 https://yal.cc/csharp-get-current-keyboard-layout/

  On some occasions, you may want to get a "global" input language - that is, the keyboard layout used by the current foreground window\application\whatever. Basically, simulating the behaviour of the language panel on Windows.

The common use cases are on-screen keyboards, fullscreen applications, and widgets.

While I wasn‘t able to find a premade function that get this particular thing during my searches, it turned out not to be too hard to assemble:

[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr proccess);
[DllImport("user32.dll")] static extern IntPtr GetKeyboardLayout(uint thread);
public CultureInfo GetCurrentKeyboardLayout() {
    try {
        IntPtr foregroundWindow = GetForegroundWindow();
        uint foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
        int keyboardLayout = GetKeyboardLayout(foregroundProcess).ToInt32() & 0xFFFF;
        return new CultureInfo(keyboardLayout);
    } catch (Exception _) {
        return new CultureInfo(1033); // Assume English if something went wrong.
    }
}

 

So, first, we import a couple of functions from user32.dll:

  • GetForegroundWindow, to get the current foreground\active window‘ pointer.
  • GetWindowThreadProcessId, to get the ID of the thread that created the particular window.
  • GetKeyboardLayout, to get the keyboard layout ID currently used by the given thread.

The actual function then proceeds to combine these in a straightforward manner to obtain the keyboard layout ID for the current active window.

Then a System.Globalization.CultureInfo is created based on ID, permitting to conveniently get the language name in various formats and a handful of other useful information.

If there‘s no foreground window available, GetKeyboardLayout will return 0 (which is not a valid ID for CultureInfo), and the catch-block will return En-US as a fallback language (alternatively, you can return null and handle that separately).

And that‘s it. Have fun!

C#: Get current keyboard layout\input language

标签:lte   useful   eth   ica   catch   input   exception   pointer   func   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9717466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!