0

我的应用基于 Visual Basic 2010/2012(两种语言的所有代码都相同)。'F' or 'G'当从键盘按下任何一个时,我正在尝试做某些事情。我已经尝试了所有的东西,比如使用Keypress, keydown and keyup events,但它们都没有用。

这是我的应用程序的示例代码。这只是在消息框中显示用户按下了哪个键,这也不起作用,即没有任何反应,甚至没有错误消息。

   Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

    If (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.F)) Then
        MessageBox.Show("pressed F")
    ElseIf (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.B)) Then
    MessageBox.Show("pressed B")
    End If
End Sub`
4

3 回答 3

0

除了为您的Form设置 KeyPreview() 为 True 之外,正如@Tony Hopkinson 已经指出的那样,这里是如何正确检查 Ctrl-F 或 Ctrl-B:

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.Control AndAlso e.KeyCode = Keys.F Then
        MessageBox.Show("Ctrl-F")
    ElseIf e.Control AndAlso e.KeyCode = Keys.B Then
        MessageBox.Show("Ctrl-B")
    End If
End Sub
于 2013-05-12T15:44:33.583 回答
0

在按键中

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
dim kc as string = e.KeyChar

if kc = "F" OR kc = "G" then msgbox "Horeeee"
End Sub
于 2013-05-12T15:46:48.750 回答
0

我对优化KeyDown处理程序的看法,使用 DRY 原则(=不要重复自己):

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  If Not e.Control Then Exit Sub
  Select Case e.KeyCode
    Case Keys.F : MessageBox.Show("Ctrl-F")
    Case Keys.B : MessageBox.Show("Ctrl-B")
  End Select
End Sub
于 2013-05-12T15:49:53.337 回答