VB2005 Arrow Keys
I have a nice sub that looks for when the 4 arrow keys are pressed. I then use that info to programically click buttons (1 to 4) that the user would click to move a little figure around the screen. This way, the user can click the buttons, or use the arrow keys.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
Button1.PerformClick()
End If
If e.KeyCode = Keys.Right Then
Button2.PerformClick()
End If
If e.KeyCode = Keys.Down Then
Button3.PerformClick()
End If
If e.KeyCode = Keys.Left Then
Button4.PerformClick()
End If
End Sub
The problem is, when I use the arrow keys, it cycles through the other controls on the form as if I were pressed the Tab key. I have tried turning off all their "Tabstops" and it does no good. Once the focus reaches a textbox, the arrow keys work fine, but until it reaches the textbox, the arrows do not move the figure on the screen.
If I change the Keys commands to be regular keys, like an "A", it works perfect.
How do I turn off the tabbing of the arrow keys?
You need to override the form's Control.IsInputKey(Keys) method to return True when an arrow key is passed to it.
Apologies if the source is incorrect, I'm a C# programmer. [wink]
You might also have to set Form.KeyPreview to True.
Protected Overrides Function IsInputKey(ByVal keyData As Keys) As Boolean Select Case keyData Case Keys.Up, Keys.Down, Keys.Right, Keys.Left Return True Case Else Return MyBase.IsInputKey(keyData) End SelectEnd Function
Apologies if the source is incorrect, I'm a C# programmer. [wink]
You might also have to set Form.KeyPreview to True.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement