How do you detect simultaneous keypresses such as “Ctrl + T” in VB.NET?

我与影子孤独终老i 提交于 2019-11-29 18:05:35

问题


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


回答1:


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

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 can also be written as If e.Control.

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.




回答2:


Private Sub frmMain_Zaporka_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown

Select Case e.KeyData
    Case (Keys.Control + Keys.Shift + Keys.F12)
        MsgBox("Control + Shift + F12")
    Case (Keys.Escape)
        Me.Close()
End Select

' or

If e.KeyCode = Keys.F12 AndAlso e.Modifiers = (Keys.Control Or Keys.Shift) Then
    MsgBox("Control + Shift + F12")
ElseIf e.KeyCode = Keys.Escape Then
    Me.Close()
End If

' or

Select Case e.KeyCode
    Case (Keys.F12 And e.Control And e.Shift)
        MsgBox("Control + Shift + F12")
    Case (Keys.Escape)
        Me.Close()
End Select

End Sub




回答3:


I had the same problem, but for me to get this to work I had to set the forms KeyPreview property to true. In Visual studio you can change this in the Forms [Design] Property window or changing the property on load.

Private Sub frmTimingP2P_Load(ByVal sender As System.Object, ByVal e As _ 
                               System.EventArgs) Handles MyBase.Load

    Me.KeyPreview = True

End Sub

then use by using:

Private Sub frmTimingP2P_KeyDown(ByVal Sender As Object, ByVal e As _ 
                        System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown


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

End Sub

or other program logic as provided in the answers above.




回答4:


I dont have vb.net installed right now but try this on your keydown or keypress event:

If e.KeyCode = Keys.T AndAlso e.Control = True Then
MsgBox("Ctrl + T")
End If



回答5:


I'll save you from the long code. Here:

If e.Control And e.Alt And e.KeyCode = Keys.G Then
    MsgBox("Control Alt G")
End If


来源:https://stackoverflow.com/questions/13803761/how-do-you-detect-simultaneous-keypresses-such-as-ctrl-t-in-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!