如何在 C# 中以编程方式检查和切换 Num-Lock?
我想以编程方式检查 的值,并能够切换数字锁定。在 C# 中执行此操作的最简单方法是什么?
原因是我想在程序启动时验证数字锁定是否为“ON”。
谢谢
I would like to programmatically check the value of, and be able to toggle num-lock. What's the simplest way to do that in C#?
The reason is that I want to verify num-lock is "ON" at program start.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查 如何以编程方式打开 Numlock 键
Check How to programmatically turn on the Numlock Key
您可以通过 P/Invoke 使用 GetKeyboardState 和 keybd_event 来执行此操作。
keybd_event 的 MSDN 页面准确显示了如何切换数字锁定,以及获取其状态(在 C++ 中)。
pinvoke.net 上有针对 keybd_event 和 GetKeyboardState。
You can do this via P/Invoke with GetKeyboardState and keybd_event.
The MSDN page for keybd_event shows exactly how to toggle num-lock, as well as get it's state (in C++).
There are P/Invoke signitures available on pinvoke.net for keybd_event and GetKeyboardState.
除了 Arsen 给出的答案:
There are issues with heap crashing in 64-bit builds。使用此代码的程序可能随时崩溃。要查看这一点,请启用调试选项“启用 Windows 调试堆分配器”。调试器在调用 FreeHGlobal 时停止。
它有助于计算 INPUT 结构的大小,如下所示。
In addition to the answer given by Arsen:
There are problems with heap corruption in 64-bit builds. Programs using this code may crash at any point. To see this, enable the debug option "Enable Windows debug heap allocator". The debugger stops on calling FreeHGlobal.
It helps to calculate the size of the INPUT structure as follows.