Reputation: 6274
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
Upvotes: 3
Views: 6209
Reputation: 6274
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
Upvotes: 3
Reputation: 7204
You can try:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If msg.WParam.ToInt32() = CInt(Keys.Left) And txtMytext.Focused = True Then
'your code
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
valter
Upvotes: 0