473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting arrow keys

How can I detect when one of the arrow keys is pressed?

Thanks,
Nathan
Nov 20 '05 #1
6 1935
Hi Nathan,

You can trap them in the keydown event:
Dim xstring As String

xstring = e.KeyCode()

MessageBox.Show (xstring)

The left arrow is 37, right 39, down 40, up 38.

HTH,

Bernie Yaeger

"Nathan" <nk************ *********@softh ome.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I detect when one of the arrow keys is pressed?

Thanks,
Nathan

Nov 20 '05 #2
don't forget to turn keypreview on in the form, if you don't you wont trap
much ;p

hope it helps

eric
"Bernie Yaeger" <be*****@cherwe llinc.com> wrote in message
news:eb******** ******@TK2MSFTN GP11.phx.gbl...
Hi Nathan,

You can trap them in the keydown event:
Dim xstring As String

xstring = e.KeyCode()

MessageBox.Show (xstring)

The left arrow is 37, right 39, down 40, up 38.

HTH,

Bernie Yaeger

"Nathan" <nk************ *********@softh ome.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I detect when one of the arrow keys is pressed?

Thanks,
Nathan


Nov 20 '05 #3
In addition to the others, here is a more elegent snipit.

'A textbox example.
Private Sub TextBox1_KeyDow n(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles TextBox1.KeyDow n

Select Case e.KeyCode

Case Keys.Down ' << Note this useful enumeration
MsgBox("DOWN")
Case Keys.Up
MsgBox("UP")
Case Keys.Left
MsgBox("LEFT")
Case Keys.Right
MsgBox("RIGHT")
End Select

End Sub

Regards - OHM
Nov 20 '05 #4
Thanks for the info guys.

Nathan
"Bernie Yaeger" <be*****@cherwe llinc.com> wrote in message
news:eb******** ******@TK2MSFTN GP11.phx.gbl...
Hi Nathan,

You can trap them in the keydown event:
Dim xstring As String

xstring = e.KeyCode()

MessageBox.Show (xstring)

The left arrow is 37, right 39, down 40, up 38.

HTH,

Bernie Yaeger

"Nathan" <nk************ *********@softh ome.net> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
How can I detect when one of the arrow keys is pressed?

Thanks,
Nathan


Nov 20 '05 #5
"Nathan" <nk************ *********@softh ome.net> schrieb
How can I detect when one of the arrow keys is pressed?


http://msdn.microsoft.com/library/en...teractions.asp

--
Armin
Nov 20 '05 #6
* "One Handed Man [ OHM# ]" <OneHandedMan{a t}BTInternet{do t}com> scripsit:
In addition to the others, here is a more elegent snipit.

'A textbox example.
Private Sub TextBox1_KeyDow n(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles TextBox1.KeyDow n

Select Case e.KeyCode

Case Keys.Down ' << Note this useful enumeration
MsgBox("DOWN")
Case Keys.Up
MsgBox("UP")
Case Keys.Left
MsgBox("LEFT")
Case Keys.Right
MsgBox("RIGHT")
End Select

End Sub


This will not work, if there are, for example, buttons on the form --
even if 'KeyPreview' is set to 'True'.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3269
by: Paul Nathan | last post by:
While trying to write a simple game which uses the arrow keys, I noticed that under the subroutine that handles mybase.keydown, everytime I pressed any arrow keys, or in fact any of the 9 keys above it (End, Del, PGDN etc) that the result for e.keycode was 'none', yet it still picked up my keypress! All other keys work fine. This problem only recently happened. I have tried the following to help: Roll back to D.X 9.0a and 9.0 SDK, remove...
2
8117
by: Darren Oakey | last post by:
ok - the problem - I made a simple breakout game out of a form, just painting the background - and using keydown for left and right arrow keys to control the bat - worked fine. I then moved all the code into a user control, put it on the form. Now I don't get keydown events when I press an arrow key - on either the form or the usercontrol ?!!!? I suspect somehow the container fucntionality has decided that they are was of migrating...
1
2319
by: bj | last post by:
How to process arrow keys? Hello I need to catch arrow keys down event from control derivied from usercontrol -OnKeyDown method seems to work for all keys besides arrow keys -however OnKeyUp method works just fine for them - OnKeyDown works when i press arrowkeys + modifiers keys like CTRL,
4
5895
by: Neil Wallace | last post by:
Hi there, I have an application in which a grid of 100 or more buttons are put on a form in columns of 10. All the buttons are within a panel. They are added in runtime, and so they adopt a sensible tab value. The tab key moves the focus down the column one by one, and the up and down arrow keys work well.
11
2814
by: Rlrcstr | last post by:
How can you detect when an arrow key gets pressed? Doesn't seem to trigger a KeyPress or KeyDown event. Thanks. Jerry
2
5518
by: Vincent | last post by:
Hi, I have a user control that needs to trap the arrow keys to move items around internally. However, using the arrow keys will move the focus to another control on the form hosting the user control. How do I stop this? Vincent.
1
10157
by: Martijn Mulder | last post by:
/* I have problems detecting the Arrow Keys on a User Control. A control derived from System.Windows.Forms.Control neglects 'bare' Arrow Keys but does react on the combination <Altor <Ctrl+ Arrow Key. The code below shows what I mean. How can I cure this? (excuse me for the line breaks) */
4
3294
by: boopsboops | last post by:
Hi thescripts people, I hope I'm in the right forum for Visual Basic Dotnet (VS 2005). I am trying to make a custom control in which you can nudge a point around using the arrow keys. Actually, the control is meant to be a simple drawing program. To test it out I have put the control on a Windows form which also contains several buttons. I have added a KeyDown event handler to the custom control (see code below). It responds fine to keys...
2
7147
by: Robert Dufour | last post by:
In Vs 2005 Vb.Net How do you detcet the arrow keys, the function keys, the Ctrl Key, The shift key, the PrintScreen key, scroll lock key NumLock key. I know how to detect the keys that return ascii chararcters but these do not seem to. So how do youn detect when they have been pressed? Thanks for any help Bob
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.