首页 / C# / c# – 正确检测键盘布局
c# – 正确检测键盘布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 正确检测键盘布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2358字,纯文字阅读大概需要4分钟。
内容图文
我有一个winforms应用程序,我需要获取用户的当前键盘布局.为此,我使用System.Windows.Forms.InputLanguage.CurrentInputLanguage.LayoutName.
只要用户将表单作为其活动窗口,一旦他将其他内容聚焦并更改了前一个属性不会返回正确值的语言,它就会正常工作,当表单仍然是活动窗口时,它将返回最后使用的语言.
是否有一种方法可以获得用户键盘布局的名称,即使他没有关注表单,对可以使用的内容没有任何限制.
解决方法:
您可能已经知道System.Windows.Forms.InputLanguage.CurrentInputLanguage.LayoutName属性返回当前线程的键盘布局,无论您选择哪种布局,它都将保持相同的执行线程,除非您选择该窗口并更改该窗口的键盘输入布局.
也就是说,您实际上要做的是检查当前的键盘布局文化,并能够知道它何时发生变化.前一段时间我有类似的要求,我想出了以下代码,这些代码很适合我:
public delegate void KeyboardLayoutChanged(int oldCultureInfo, int newCultureInfo);
class KeyboardLayoutWatcher : IDisposable
{
private readonly Timer _timer;
private int _currentLayout = 1033;
public KeyboardLayoutChanged KeyboardLayoutChanged;
public KeyboardLayoutWatcher()
{
_timer = new Timer(new TimerCallback(CheckKeyboardLayout), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
}
[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 int GetCurrentKeyboardLayout()
{
try
{
IntPtr foregroundWindow = GetForegroundWindow();
uint foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
int keyboardLayout = GetKeyboardLayout(foregroundProcess).ToInt32() & 0xFFFF;
if (keyboardLayout == 0)
{
// something has gone wrong - just assume English
keyboardLayout = 1033;
}
return keyboardLayout;
}
catch (Exception ex)
{
// if something goes wrong - just assume English
return 1033;
}
}
private void CheckKeyboardLayout(object sender)
{
var layout = GetCurrentKeyboardLayout();
if (_currentLayout != layout && KeyboardLayoutChanged != null)
{
KeyboardLayoutChanged(_currentLayout, layout);
_currentLayout = layout;
}
}
private void ReleaseUnmanagedResources()
{
_timer.Dispose();
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~KeyboardLayoutWatcher()
{
ReleaseUnmanagedResources();
}
}
并使用它像:
new KeyboardLayoutWatcher().KeyboardLayoutChanged += (o, n) =>
{
this.CurrentLayoutLabel.Text = $"{o} -> {n}"; // old and new KB layout
};
内容总结
以上是互联网集市为您收集整理的c# – 正确检测键盘布局全部内容,希望文章能够帮你解决c# – 正确检测键盘布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。