【问题标题】:Visual Basic Key ListenerVisual Basic 键侦听器
【发布时间】:2011-08-27 16:17:49
【问题描述】:

我正在尝试用 Visual Basic(使用 VS 2010)编写一个响应箭头键的程序。我知道 Java 中有关键监听器,但不确定 VB 中是否存在这样的东西以及如何对其进行编码。请给我看一些这方面的例子。 谢谢。

【问题讨论】:

    标签: vb.net keylistener


    【解决方案1】:

    如果你在做winforms,那么将表单的KeyPreview 属性设置为true,然后设置KeyDown 事件。您的代码将如下所示:

    Dim previousKey As Keys? = Nothing
    
    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Up Then
            'up arrow
        End If
    
    
        If e.KeyCode = Keys.Left Then
            'left arrow
            If Not previousKey Is Nothing And previousKey = Keys.Up Then
                'Up arrow and then Left Arrow
                MessageBox.Show("there, that's better")
            End If
        End If
    
        If e.KeyCode = Keys.Right Then
            'right arrow
        End If
    
        If e.KeyCode = Keys.Down Then
            'down arrow
        End If
    
        'After everything is done set the current key as the previous key.
        previousKey = e.KeyCode
    End Sub
    

    【讨论】:

    • 非常感谢。这就是我想要的。
    • 好的。现在,是否可以在按下 2 个键时让程序响应?就像按下向上箭头键和向左箭头键一样?
    • 我已经修改了我的示例以捕获向上箭头,然后是向左箭头。
    • 是否可以让它表现得像赛车游戏一样?
    • 嗯,赛车游戏是存在的,所以我认为这是可能的。尽管您遇到的具体问题以及您为解决该问题所做的尝试,但您需要创建一个新帖子。
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2011-12-18
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多