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
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?
Thanks
First of all,
Andin your code should beAndAlsosince it’s a logical operator.Andin VB is a bit operator. Next, you can use theModifiersproperty to test for modifier keys:The
e.KeyCode And Not Keys.Modifiersin the first part of the condition is necessary to mask out the modifier key.If e.Modifiers = Keys.Ctrlcan also be written asIf e.Control.Alternatively, we can collate these two queries by asking directly whether the combination Ctrl+T was pressed:
In both snippets we make use of bit masks.