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(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!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Python正则表达式初识(三)
前几天给大家分享了Python正则表达式基础(一)和Python正则表达式基础(二),感兴趣的小伙伴可以点击进去学习,今天继续给大家分享Python正则表达式基础。 1、正则表达式特殊字符“+”,其代表的意思“+”号前面的任意字符必须至少出现一次,才能匹配成功。如下图所示,如果没有加特殊字符“+”,则按照前面介绍的贪婪模式从右边进行匹配,输出的结果为“pp”。 2、现在将匹配模式由之前的“.*(p.*p).*”改为“.*(p.+p).*”,即将特殊字符“*”改为特殊字符“+”,用特殊字符“+”来限定前面的字符出现的次数,至少出现一次。运行程序,得到的结果为“ppp”,如下图所示。 简单的来理解一下,首先贪婪模式不在赘述,然后匹配到第一个字符p,之后碰到特殊字符“+”,表示匹配任意字符,但该字符至少出现一次,然后再匹配到第二个字符p,才会提取到匹配的字符串。 3、再次来感受一下,将之前的三个ppp改为现在的php,之后再运行程序,如下图所示,得到的结果是php。 4、如果将之前的三个ppp改为现在的phhp,会有什么样的结果呢?如下图所示,毋庸置疑,答案肯定是phhp。 因为特殊字符“+...
- 下一篇
C# TSF 输入法的获取
原文 C# TSF 输入法的获取 起因: 「添雨跟打器」中存在一个问题。在 windows 8/10 里面,输入法就获取不到了。我一直没有去管这样的问题。但是也大致知道,可能是 TSF 架构的问题。 TSF: Microsoft Windows 文本服务框架(TSF) 是一个包含在Windows XP 及其后继版本操作系统的系统服务。TSF为高级文本输入的通信以及自然语言技术提供了一个简单的可扩展的框架。 以上引自百度百科。 MSDN: 于是第一时间去MSDN查看了一下关于 TSF 。全英文的文档看得云里雾里。但是搞清了一点情况。TSF 它在 Windows 里面所对应的 dll 文件——msctf.dll。 使用Visual Studio自带的命令工具查看该 dll 的函数列表如下: 命令: 1 dumpbin -exports msctf.dll 结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6