Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net Get arrow keys press whilst in a textbox

Tags:

vb.net

While inside a textbox, i can get the enter and escape keypress but i cant get the arrows (up down left right) key press. Instead it moves the cursor inside the textbox. I guess i have to override this but how? I need to be able to catch these events only when im inside the textbox, ....so a form listener is out of the question(?)

Private Sub txtMytext_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Mytext.KeyPress

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
        MsgBox("Enter pressed!")
    End If

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Escape) Then
        MsgBox("Escape pressed!")
    End If

    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Up) Then
        MsgBox("Up key pressed!") ' this never fires :(
    End If

End Sub
like image 320
Sharky Avatar asked Dec 30 '25 14:12

Sharky


1 Answers

posting an answer to my own question, after helpful comments from DatVM and MrPaulch:

Instead of KeyPress i used the KeyDown event:

Private Sub txtMyText_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMyText.KeyDown

    If e.KeyCode = Keys.Up Then
        MsgBox("UP")
    End If

End Sub
like image 101
Sharky Avatar answered Jan 02 '26 12:01

Sharky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!