• 0

[VB.NET] Capture Arrow Keys


Question

How can I capture the arrow keys with my form, I can capture all of the keys except the arrow keys, does any one know how? :unsure:

I have tried and only can capture them pressing Ctrl + the arrow, but the arrow alone can be captured by my form :angry:

Hope anyone can help me :D

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0
   Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Up Then
            MessageBox.show("Up Arrow Pressed")
        End If
    End Sub

Link to comment
Share on other sites

  • 0

Thanks, but I have tried that already It captures all of the keys I need except the arrow keys, I don't know maybe some property of the form, here is my code:

Private Sub ControlForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        MessageBox.Show("KeyCode:" & e.KeyCode & " KeyData: " & e.KeyData & " KeyValue " & e.KeyValue & " Modifiers " & e.Modifiers)
    End Sub

Link to comment
Share on other sites

  • 0

You have to override ProcessCmdKey.. Heres an exmaple in C# :)

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	if (keyData == Keys.Up)
	{
 ?MessageBox.Show("UP!");
 ?return true;
	}

	return base.ProcessCmdKey (ref msg, keyData);
}

Dan

Link to comment
Share on other sites

  • 0

Thanks Dan!! this is what I get..

Protected Overloads Overrides Function ProcessCmdKey(ByRef msg As Windows.Forms.Message, ByVal keydata As Windows.Forms.Keys) As Boolean
        If keydata = Keys.Up Then
            MessageBox.Show("Up Key")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keydata)
    End Function

Thankyou verymuch :yes: :cool:

Link to comment
Share on other sites

  • 0

ALso a note about this.. it will catch keys being pressed in any child controls (unless they use the keys themselves)... This probably wont be a problem but just letting ya know :)

Dan

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.