KeyDown :识别多个键
如何在 KeyDown
中确定 CtrlUp 已被按下。
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control && e.KeyCode == Keys.Up)
{
//do stuff
}
}
无法工作,因为两个键永远不会在同一秒内完全相同地按下。 你总是先按Ctrl,然后再按另一个......
How can I determine in KeyDown
that CtrlUp was pressed.
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control && e.KeyCode == Keys.Up)
{
//do stuff
}
}
can't work, because never both keys are pressed exactly in the same second. You always to at first the Ctrl and then the other one...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以使用 ModifierKeys 属性:
请注意,ModifierKeys 值可以是值的组合,因此如果您想要检测是否按下了 CTRL,无论 SHIFT 的状态如何或 ALT 键,您将需要执行按位比较,如我上面的示例所示。 如果您想确保没有按下其他修饰符,您应该检查是否相等:
You can use the ModifierKeys property:
Note that the ModifierKeys value can be a combination of values, so if you want to detect that CTRL was pressed regardless of the state of the SHIFT or ALT keys, you will need to perform a bitwise comparison as in my sample above. If you want to ensure that no other modifiers were pressed, you should instead check for equality:
这肯定会起作用。 请小心处理
KeyUp
事件而不是keyDown
。对我来说,
keyDown
不起作用,keyU
p 对于相同的代码起作用。我不知道为什么,但似乎是因为
keyDown
事件在您按下任意键后立即发生,即使这是 ctrl 键,所以如果您按下 ctrl +Up 您将在按 UP 键之前按 ctrl 键,因此该事件将在您按下另一个键之前发生,也按第二个键将再次触发该事件。而使用
KeyUp
时,只有松开按键才会触发该事件,因此您可以按ctrl,然后再按第二个键,这将触发一个事件。this will work for sure. Be careful to handle
KeyUp
event and notkeyDown
.For me,
keyDown
didn't work,keyU
p worked instead for the same code.I don't know why, but it seems because
keyDown
event happens directly after you press any key, even if that was ctrl key, so if you pressed ctrl+Up you will press ctrl key before the UP key and thus the event will occur before you can press the other, also pressing the second key will triggers the event again.While using
KeyUp
will not trigger the event until you release the key, so you can press ctrl, and the press the second key, which will trigger one event.你必须记住按下的键(即在布尔数组中)。 并在按下(keydown)时将位置设置为 1,在 up 时将位置设置为 0。
这样您就可以跟踪多个密钥。 我建议只为特殊键创建一个数组,
这样你就可以这样做:
you have to remember the pressed keys (ie in a bool array). and set the position to 1 when its pressed (keydown) and 0 when up .
this way you can track more than one key. I suggest doing an array for special keys only
so you can do:
您可以像这样检查 KeyEventArgs 的修饰符:
MSDN 参考
You can check the modifiers of the KeyEventArgs like so:
MSDN reference
从 KeyEventArgs 上的 MSDN 页面:
From the MSDN page on KeyEventArgs:
仅当您先按 LeftCtrl,然后按“UP”时,此代码才会起作用。
如果顺序不重要,我建议这样做:
在这种情况下,两个 Ctrl 都会被考虑在内,并且顺序不重要。
This code will work only if you press first LeftCtrl, then "UP".
If order has no importance, I recommend that one :
In that case, both Ctrl are taken in account, and no importance about the order.
在
KeyEventArgs
中,有属性 Ctrl、Alt 和 Shift 显示这些按钮是否被按下。In the
KeyEventArgs
there are properties Ctrl, Alt and Shift that shows if these buttons are pressed.您可以尝试使用
Keyboard
对象来检测IsKeyDown
属性。 另外,如果您不希望覆盖浏览器快捷方式,可以将Handled
属性设置为 true。但是在覆盖浏览器快捷方式时要小心,因为它可能会导致混乱。You can try using the
Keyboard
object to detect theIsKeyDown
property. Also, if you don't want the browser shortcut to over-ride you can setHandled
property to true.But be careful when over-riding browser shortcuts as it could cause confusion.您可以尝试我的工作代码:
我使用此代码是因为我不知道为什么当我使用时:
不起作用。
you can try my working code :
i use this code because i don't know why when i use :
didn't work.
if (e.Control && e.Shift && e.KeyCode == Keys.A)
{
}
if (e.Control && e.Shift && e.KeyCode == Keys.A)
{
}
KeyDown 事件将仅保存最近按下的键的信息。 我已经成功构建了一个字符串,其中包含所有最新的按键连接在一起。
如果单击“Control”键,或者字符串长度超过 10 个字符,我会清除该字符串。
每次检查字符串,然后执行任务,这对于应用程序中的秘密热键功能非常有用。
对于上面的函数,我的热键是“Control(清除字符串)+ i + n + t”来触发我的 AdminLoginEvn 方法。 adminLogin 布尔值已合并,因此我只在应用程序打开时运行 AdminLoginEvn 一次。
The KeyDown event will only hold the information for the most recent key that was pressed. I have had success building a string that contains all the most recent key down keys concatenated together.
If a key down of "Control" was clicked, or the strings becomes greater than 10 chars long, I clear the string.
Checking the string each time, then performing a task afterwords gives a great use for a secret hotkey function within your application.
For the function above my hotkeys would be "Control (clears the string) + i + n + t" to fire my AdminLoginEvn method. The adminLogin boolean was incorporated so I only run my AdminLoginEvn one time while the application is open.
我测试了下面的代码。 有用...
I tested below code. it works...
我花了一段时间才找到在尝试检测 [Alt][Right] 时最终需要的提示。 我在这里找到它:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355ab9a-9214-4fe1-87ea-b32dfc22946c/issue-with -alt-key-and-key-down-event?forum=wpf
它归结为我使用的
Shortcut
辅助类中的类似内容:在“重新映射”原始内容之后值(注意 e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key 部分),进一步处理可以照常进行。
It took me a while to find the hint I ultimately needed when trying to detect [Alt][Right]. I found it here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355ab9a-9214-4fe1-87ea-b32dfc22946c/issue-with-alt-key-and-key-down-event?forum=wpf
It boils down to something like this in a
Shortcut
helper class I use:After "re-mapping" the original values (notice the
e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key
part), further processing can go on as usual.