473,232 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

How to Trap Arrow/Direction Keys in a User Control

I'll kick myself when you tell me, but ...

I have a user control on a form, and I want the user control to see the
arrow keys when I press them. If I press just about any other key the
control's KeyDown event is fired, but not when I press a direction key.

I want to see them in the KeyDown event so that I can respond as soon as the
key is depressed, and I want to detect when the key is held down so that I
can perform an action repeatedly.

I am using VB.NET in Visual Studio 2005.

Please take pity on me.

TIA

Charles
Aug 10 '07 #1
2 3287

Here is a snippet from one of my recent projets. Modify it to suit
your needs:

---snip---
Protected Overrides Function IsInputKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
'Need to trap the following input combinations to prevent
the usercontrol
'from losing focus:

'1. Navigation keys alone
'2. Navigation keys + shift and/or ctrl

'Navigation keys are
'- arrow keys
'- home/end
'- page up/down

'Note: We do not process other editing keys (such as
Delete, copy, paste, etc)
' as these do not cause the control to lose focus.

Dim stdkeys As System.Windows.Forms.Keys = keyData And Not
Keys.Modifiers
Dim mods As System.Windows.Forms.Keys = keyData And
Keys.Modifiers

Dim shift As Boolean = ((mods And Keys.Shift) =
Keys.Shift)
Dim ctrl As Boolean = ((mods And Keys.Control) =
Keys.Control)

Dim shiftorctrl As Boolean = shift Or ctrl
Dim nomods As Boolean = (mods = Keys.None)

Dim navigationkeys As Boolean = (stdkeys = Keys.Left Or _
stdkeys = Keys.Up Or _
stdkeys = Keys.Right Or _
stdkeys = Keys.Down Or _
stdkeys = Keys.Home Or _
stdkeys = Keys.End Or _
stdkeys = Keys.PageUp Or
_
stdkeys = Keys.PageDown)

If (navigationkeys And nomods) Or _
(navigationkeys And shiftorctrl) Then

Return True

Else
Return MyBase.IsInputKey(keyData)

End If

End Function

---snip---

Regards,

Joergen Bech

On Fri, 10 Aug 2007 20:07:52 +0100, "Charles Law" <bl***@nowhere.com>
wrote:
>I'll kick myself when you tell me, but ...

I have a user control on a form, and I want the user control to see the
arrow keys when I press them. If I press just about any other key the
control's KeyDown event is fired, but not when I press a direction key.

I want to see them in the KeyDown event so that I can respond as soon as the
key is depressed, and I want to detect when the key is held down so that I
can perform an action repeatedly.

I am using VB.NET in Visual Studio 2005.

Please take pity on me.

TIA

Charles
Aug 11 '07 #2
Hi Joergen

Thanks for the reply. That is exactly what I needed; I didn't have to change
a line.

Many thanks.

Charles
"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAMwrote in message
news:7f********************************@4ax.com...
>
Here is a snippet from one of my recent projets. Modify it to suit
your needs:

---snip---
Protected Overrides Function IsInputKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
'Need to trap the following input combinations to prevent
the usercontrol
'from losing focus:

'1. Navigation keys alone
'2. Navigation keys + shift and/or ctrl

'Navigation keys are
'- arrow keys
'- home/end
'- page up/down

'Note: We do not process other editing keys (such as
Delete, copy, paste, etc)
' as these do not cause the control to lose focus.

Dim stdkeys As System.Windows.Forms.Keys = keyData And Not
Keys.Modifiers
Dim mods As System.Windows.Forms.Keys = keyData And
Keys.Modifiers

Dim shift As Boolean = ((mods And Keys.Shift) =
Keys.Shift)
Dim ctrl As Boolean = ((mods And Keys.Control) =
Keys.Control)

Dim shiftorctrl As Boolean = shift Or ctrl
Dim nomods As Boolean = (mods = Keys.None)

Dim navigationkeys As Boolean = (stdkeys = Keys.Left Or _
stdkeys = Keys.Up Or _
stdkeys = Keys.Right Or _
stdkeys = Keys.Down Or _
stdkeys = Keys.Home Or _
stdkeys = Keys.End Or _
stdkeys = Keys.PageUp Or
_
stdkeys = Keys.PageDown)

If (navigationkeys And nomods) Or _
(navigationkeys And shiftorctrl) Then

Return True

Else
Return MyBase.IsInputKey(keyData)

End If

End Function

---snip---

Regards,

Joergen Bech

On Fri, 10 Aug 2007 20:07:52 +0100, "Charles Law" <bl***@nowhere.com>
wrote:
>>I'll kick myself when you tell me, but ...

I have a user control on a form, and I want the user control to see the
arrow keys when I press them. If I press just about any other key the
control's KeyDown event is fired, but not when I press a direction key.

I want to see them in the KeyDown event so that I can respond as soon as
the
key is depressed, and I want to detect when the key is held down so that I
can perform an action repeatedly.

I am using VB.NET in Visual Studio 2005.

Please take pity on me.

TIA

Charles

Aug 11 '07 #3

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

Similar topics

2
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...
7
by: Seash | last post by:
Hi friends , here is the sample code private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) //enter { txtbox2.Focus(); }
4
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...
11
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
1
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...
0
by: Martijn Mulder | last post by:
/* I override IsInputKey() to direct the Arrow Keys (Cursor Keys) to my custom System.Windows.Forms.Control. But, holding down the Shift-Key prevents the Arrow Keys from coming through. How can...
4
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,...
8
by: =?Utf-8?B?RyBIdXN0aXM=?= | last post by:
This is the 2nd time posting so sorry for duplications. I am using VB.NT 2005 & a standard Combobox. I've been wracking my brain over this problem for a over a month & cannot seem to find a way to...
2
by: Charles Law | last post by:
I'll kick myself when you tell me, but ... I have a user control on a form, and I want the user control to see the arrow keys when I press them. If I press just about any other key the control's...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.