Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to detect keys [up, down, left, right] on keyboard.
Posted
Comments
[no name] 4-Mar-14 9:08am    
In which language ? .NET or Javascript or what..? please give details your question.
Jhony Spd 4-Mar-14 9:13am    
vb.net
Vedat Ozan Oner 4-Mar-14 9:19am    
see here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx
Thomas Daniels 4-Mar-14 9:46am    
In Windows Forms, WPF or Console Application?

If you are using Windows Forms, add this in the constructor:
VB
Me.KeyPreview = True

And then create this method:
VB
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
	Select Case e.KeyCode
		Case Keys.Up
			' up arrow
			Exit Select
		Case Keys.Down
			' down arrow
			Exit Select
		Case Keys.Left
			' left arrow
			Exit Select
		Case Keys.Right
			' right arrow
			Exit Select
	End Select
End Sub

Setting KeyPreview to True makes sure that the Form1_KeyDown will always be called if a key is pressed, even if the focus is on another control, such as a TextBox.

If you are using WPF:
VB
Private Sub MainWindow_PreviewKeyDown(sender As Object, e As KeyEventArgs) Handles Me.PreviewKeyDown
	Select Case e.Key
		Case Key.Up
			' up arrow
			Exit Select
		Case Key.Down
			' down arrow
			Exit Select
		Case Key.Left
			' left arrow
			Exit Select
		Case Key.Right
			' right arrow
			Exit Select
	End Select
End Sub

In WPF, the PreviewKeyDown event has the same purpose as the KeyPreview property in Windows Forms. If you don't want that, just use KeyDown.

If you are using a Console Application:
VB
Dim c As ConsoleKeyInfo = Console.ReadKey()
Select Case c.Key
	Case ConsoleKey.UpArrow
		' up arrow
		Exit Select
	Case ConsoleKey.DownArrow
		' down arrow
		Exit Select
	Case ConsoleKey.LeftArrow
		' left arrow
		Exit Select
	Case ConsoleKey.RightArrow
		' right arrow
		Exit Select
End Select

Note that this will only work if you have called Console.ReadKey().
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 10:25am    
5, for covering 3 types of UI, but it would be fair not to answer until OP specifies what is used.
—SA
Thomas Daniels 4-Mar-14 10:29am    
Thank you!
There's a event called Control.KeyDown[^] and Keys[^] enumeration in .NET.

-KR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 10:26am    
How do you know what OP is using? It's the best not to answer until OP clarifies on the application type.
—SA
Krunal Rohit 4-Mar-14 10:58am    
You might have not seen the "TAGS".
Yeah UI is different thing, but still I have answered regarding Form and WPF.

-KR
Sergey Alexandrovich Kryukov 4-Mar-14 11:05am    
Look at the tags yourself. They does not say it. And sorry, you only answered for Forms. Where is anything for WPF?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900