如何检测当前按下的键? [英] How to detect the currently pressed key?

查看:34
本文介绍了如何检测当前按下的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows Forms中,您可以随时知道光标的当前位置感谢 光标 类.

In Windows Forms, you can know, at any time, the current position of the cursor thanks to the Cursors class.

同样的东西似乎不适用于键盘.例如,是否可以知道 Shift 键是否被按下?

The same thing doesn't seem to be available for the keyboard. Is it possible to know if, for example, the Shift key is pressed?

是否绝对有必要追踪每个键盘通知(KeyDown 和 KeyUp 事件)?

Is it absolutely necessary to track down every keyboard notification (KeyDown and KeyUp events)?

推荐答案

if ((Control.ModifierKeys & Keys.Shift) != 0) 

如果 Ctrl+Shift 按下,这也将成立.如果要检查是否单独按下了 Shift,

This will also be true if Ctrl+Shift is down. If you want to check whether Shift alone is pressed,

if (Control.ModifierKeys == Keys.Shift)

如果你在一个继承了Control的类中(比如一个表单),你可以去掉Control.

If you're in a class that inherits Control (such as a form), you can remove the Control.

这篇关于如何检测当前按下的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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