2011-08-27 178 views
0

我正試圖在Visual Basic(VS 2010)中編寫一個程序來響應箭頭鍵。我知道在Java中有關鍵的監聽器,但不知道在VB中是否存在這樣的事情以及如何對它進行編碼。請給我看一些例子。 謝謝。Visual Basic密鑰監聽器

+1

console?的WinForms? WPF? XNA? –

回答

2

如果您正在執行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 
+0

非常感謝。這就是我想要的。 – yihangho

+0

好的。現在,當按下2個鍵時,程序是否可以響應?就像按向上箭頭鍵和向左箭頭鍵時? – yihangho

+0

我修改了我的示例以捕獲向上箭頭,然後向左箭頭。 –