原文 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(); // 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的更多相关文章

  1. WPF 获得当前输入法语言区域

    原文:WPF 获得当前输入法语言区域 本文告诉大家如何获得 WPF 输入法的语言区域 需要使用 user32 的方法,很简单,请看下面 [DllImport("user32.dll" ...

  2. 2019-6-23-WPF-获得当前输入法语言区域

    title author date CreateTime categories WPF 获得当前输入法语言区域 lindexi 2019-06-23 11:51:21 +0800 2018-10-12 ...

  3. Fedora 22中的Locale and Keyboard Configuration

    Introduction The system locale specifies the language settings of system services and user interface ...

  4. Java模拟按键

    JDK自带了Robot类,此类用于为测试自动化.自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件.Robot 的主要目的是便于 Java 平台实现自动测试. 详情可查看jdk1.6 ...

  5. input keyevent发送按键值【转】

    本文转载自:http://blog.csdn.net/moyu123456789/article/details/71209893 1.adb shell进入android设备,执行命令input k ...

  6. adb shell命令模拟按键/输入input使用keycode 列表详解

    在adb shell里有一个非常使用的命令,模拟按键输入,这里首先不要理解为是键盘的模拟按键,下面命令的使用和键值做一个详解. input命令格式 adb shell input keyevent & ...

  7. [工作积累] Software keyboard not shown on Activity.onCreate

    protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setCo ...

  8. OS X: Keyboard shortcuts

    Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a char ...

  9. imx6 matrix keyboard

    imx6需要添加4x4的矩阵键盘.本文记录添加方法. 参考链接 http://processors.wiki.ti.com/index.php/TI-Android-JB-PortingGuide h ...

随机推荐

  1. 对延时敏感的应用是否应该使用Docker?

    在High Scalability上看到一篇文章 How Does The Use of Docker Effect Latency? .文章回答了一个问题 I keep hearing about ...

  2. php实现Bloom Filter

    Bloom Filter(BF) 是由Bloom在1970年提出的一种多哈希函数映射的高速查找算法,用于高速查找某个元素是否属于集合, 但不要求百分百的准确率. Bloom filter通经常使用于爬 ...

  3. [CSS] Conditionally Apply Styles Using Feature Queries @supports

    While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...

  4. JasperReport html 导出

    In my last blog post I discussed about Generating jasper reports in different formats using json fil ...

  5. [Angular Router] Lazy loading Module with Auxiliary router

    Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...

  6. MVC 设置项目默认起始页和多级目录的路由配置

    我们新建一个MVC的项目 默认的路由是这样的,但是由于一些需求,我们需要对Controllers按照一些规则分类. 比如说我们在Controllers下面建了一个School的文件夹,然后建了一个St ...

  7. mui常用功能链接地址

    1.下拉刷新mui.pullToRefresh插件http://ask.dcloud.net.cn/article/12152.打包app权限列表http://ask.dcloud.net.cn/ar ...

  8. 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(1)GCC介绍及C语言编译过程

    一.GCC基本介绍 GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器.它是一套以GPL及LGPL许可证所发布的自由软件,也是GNU计划的关 ...

  9. Power control within a coherent multi-processing system

    Within a multi-processing system including a plurality of processor cores 4, 6operating in accordanc ...

  10. 到底有多少种智能指针(smart pointer)

    最近Qt的blog总结了到底有多少种smart pointer, 下面是一个简要的介绍: 1.   QPointer :提供对指针的保护,当一个指针被删除以后,再使用不会造成野指针或者指针溢出.比如 ...