Visual Basic按键侦听器 [英] Visual Basic Key Listener

查看:82
本文介绍了Visual Basic按键侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Visual Basic(与VS 2010)编写一个程序,该程序将响应箭头键.我知道Java中有关键侦听器,但是不确定VB中是否存在这种侦听器以及如何对其进行编码.请给我看一些例子. 谢谢.

I am trying to write a program in Visual Basic (with VS 2010) that will respond to the arrow keys. I am aware that there are key listener in Java but not sure if such thing exist in VB and how to code it. Please show me some example on this. Thanks.

推荐答案

如果正在执行winforms,则将表单的KeyPreview属性设置为true,然后设置KeyDown事件.您的代码将如下所示:

If you are doing winforms then set the KeyPreview property of the form to true and then set the KeyDown event. Your code will be like this:

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

这篇关于Visual Basic按键侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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