检测是否在按键事件之外按下了按键 [英] Detecting if a key is pressed outside the key events

查看:108
本文介绍了检测是否在按键事件之外按下了按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测在程序启动期间是否按住了某个键,即在static void Main()

这有可能实现吗?我想创建一个安全模式,如果用户在启动程序时按住Shift键,则会进入此模式.

I want to detect if a key is held down during the start up of my program, i.e. in static void Main()

Is this possible to achieve? I want to make a safe mode that will be entered if the user holds down the shift key when starting the program.

推荐答案

前一段时间,我发现解决方案仅可用于PInvoke.特别是,我用了这个:

A while ago, I found that a comprehensive solution is only possible used PInvoke. In particular, I used this:

const string dllName = "user32.dll";
[DllImport(dllName, EntryPoint = "GetKeyboardState")]
static extern void PInvokeGetKeyboardState(KeyStateValue[] lpKeyState);



WIN API GetKeyboardState检索每个虚拟键的状态,而不仅仅是修饰键(可通过System.Windows.Forms访问);可以将Control,Alt和Shift键的左,右变体视为不同.
此外,此配方对WPFSystem.Windows.Forms均适用.

不幸的是,使用PInvoke是不兼容的:GetKeyboardState仅在Windows平台上有效.



WIN API GetKeyboardState retrieves status of every virtual key, not just modifier keys (accessible with System.Windows.Forms); left and right variants of Control, Alt and Shift keys can be treated as different.
Also, this recipe works for both WPF and System.Windows.Forms.

Unfortunately, using PInvoke is incompatible: GetKeyboardState works only on Windows platform.


这篇关于检测是否在按键事件之外按下了按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆