C#: Get current keyboard layout\input language
原文 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的更多相关文章
- WPF 获得当前输入法语言区域
原文:WPF 获得当前输入法语言区域 本文告诉大家如何获得 WPF 输入法的语言区域 需要使用 user32 的方法,很简单,请看下面 [DllImport("user32.dll" ...
- 2019-6-23-WPF-获得当前输入法语言区域
title author date CreateTime categories WPF 获得当前输入法语言区域 lindexi 2019-06-23 11:51:21 +0800 2018-10-12 ...
- Fedora 22中的Locale and Keyboard Configuration
Introduction The system locale specifies the language settings of system services and user interface ...
- Java模拟按键
JDK自带了Robot类,此类用于为测试自动化.自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件.Robot 的主要目的是便于 Java 平台实现自动测试. 详情可查看jdk1.6 ...
- input keyevent发送按键值【转】
本文转载自:http://blog.csdn.net/moyu123456789/article/details/71209893 1.adb shell进入android设备,执行命令input k ...
- adb shell命令模拟按键/输入input使用keycode 列表详解
在adb shell里有一个非常使用的命令,模拟按键输入,这里首先不要理解为是键盘的模拟按键,下面命令的使用和键值做一个详解. input命令格式 adb shell input keyevent & ...
- [工作积累] Software keyboard not shown on Activity.onCreate
protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setCo ...
- OS X: Keyboard shortcuts
Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a char ...
- imx6 matrix keyboard
imx6需要添加4x4的矩阵键盘.本文记录添加方法. 参考链接 http://processors.wiki.ti.com/index.php/TI-Android-JB-PortingGuide h ...
随机推荐
- 怎么不让控制台system.out.println()打印
1.System类有一个public static void setOut(PrintStream out)方法,你可以调用这个方法将out重定向到任何一个全局PrintStream对象上: 2.如果 ...
- TensorFlow on Windows: “Couldn't open CUDA library cudnn64_5.dll”
TensorFlow on Windows: "Couldn't open CUDA library cudnn64_5.dll" 在 windows 下,使用 import te ...
- 线程堆栈大小 pthread_attr_setstacksize 的使用
pthread_create 创建线程时,若不指定分配堆栈大小,系统会分配默认值,查看默认值方法如下: # ulimit -s8192# 上述表示为8M:单位为KB. 也可以通过# ulimit -a ...
- 多事务运行并发问题spring学习笔记——数据库事务并发与锁详解
多事务运行并发问题 在实际应用中,往往是一台(或多台)服务器向无数客户程序提供服务,当服务器查询数据库获取数据时,如果没有采用必要的隔离机制,可能会存在数据库事务的并发问题,下面是一些常见的并发问题分 ...
- [Angular Directive] 1. Write an Angular Directive
Angular 2 Directives allow you manipulate elements by adding custom behaviors through attributes. Th ...
- 文本处理之可视化wordcloud
什么是词云 词云又叫文字云,是对文本数据中出现频率较高的“关键词”在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意思. 准备工作: python开发 ...
- 简单实现的Servlet文件上传,并显示
http://my.oschina.net/Barudisshu/blog/157481
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- dom对象常用的属性和方法有哪些?
dom对象常用的属性和方法有哪些? 一.总结 一句话总结: 1.document属性和方法:document的属性有head,body之类,方法有各种获取element的方法 2.element的属性 ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...