您如何检测同时按下的按键(例如"Ctrl + T")在VB.NET中? [英] How do you detect simultaneous keypresses such as "Ctrl + T" in VB.NET?

查看:460
本文介绍了您如何检测同时按下的按键(例如"Ctrl + T")在VB.NET中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检测VB.NET中同时按下"Control"和"t"键.到目前为止,我的代码如下:

I am trying to detect the keys "Control" and "t" being pressed simultaneously in VB.NET. The code I have so far is as follows:

Private Sub frmTimingP2P_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyValue = Keys.ControlKey And e.KeyValue = Keys.T Then
        MessageBox.Show("Ctrl + T")
    End If
End Sub

我可以通过删除and语句和第二个keyvalue语句来检测一个键或另一个键,但是当我尝试这样做时我什么也没得到.还有另一种方法吗?

I can detect one key or the other by removing the and statement and the second keyvalue statement, but I don't really get anything when I try this. Is there another method?

谢谢

推荐答案

首先,代码中的And应该是AndAlso,因为它是逻辑运算符. VB中的And是位运算符.接下来,您可以使用 Modifiers属性测试修饰键:

First of all, And in your code should be AndAlso since it’s a logical operator. And in VB is a bit operator. Next, you can use the Modifiers property to test for modifier keys:

If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Ctrl Then
    MessageBox.Show("Ctrl + T")
End If

条件第一部分中的e.KeyCode And Not Keys.Modifiers对于屏蔽修饰键是必需的.

The e.KeyCode And Not Keys.Modifiers in the first part of the condition is necessary to mask out the modifier key.

If e.Modifiers = Keys.Ctrl也可以写为 If e.Control .

If e.Modifiers = Keys.Ctrl can also be written as If e.Control.

或者,我们可以通过直接询问是否按下组合键 Ctrl + T 来整理这两个查询:

Alternatively, we can collate these two queries by asking directly whether the combination Ctrl+T was pressed:

If e.KeyCode = (Keys.T Or Keys.Ctrl) Then …

在两个代码段中,我们都使用位掩码.

In both snippets we make use of bit masks.

这篇关于您如何检测同时按下的按键(例如"Ctrl + T")在VB.NET中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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