469,572 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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 3085

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Darren Oakey | last post: by
11 posts views Thread by Rlrcstr | last post: by
1 post views Thread by Martijn Mulder | last post: by
reply views Thread by Martijn Mulder | last post: by
8 posts views Thread by =?Utf-8?B?RyBIdXN0aXM=?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.