By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,949 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,949 IT Pros & Developers. It's quick & easy.

How can I get a custom control to react to arrow keys?

P: 6
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 like Enter or Shift-X, but not to the arrow keys. Pressing an arrow key just jumps from one button on the test form to the next. It does not even get into my KeyDown handler.

In fact, I'd like to handle any possible key or key combination in my custom control, and not in the form. Perhaps I'm missing something obvious. Can anyone tell me how to do it?

thanks in advance, BB


Here's the code of a test version of the KeyDown handler:

Private Sub Drawing_Sheet_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

Select Case e.KeyCode

Case Keys.Left
m_PointLocation.X -= 1

Case Keys.Right
m_PointLocation.X += 1

Case Keys.Up
m_PointLocation.Y -= 1

Case Keys.Down
m_PointLocation.Y += 1

'debugging tests:

Case Keys.Enter
MsgBox("Enter pressed")

Case Keys.X
If e.Modifiers = Keys.Shift Then MsgBox("Shift x pressed") Else MsgBox("x pressed")

End Select

End Sub
Nov 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: 6
I'll transfer this query to the VB.Net forum.
(I got a bit confused because VB.Net the thescripts site doesn't list vb.net in the "discussion zones" panel or under the "discussions" tab. I only just discovered the /vb/ link at the bottom of the page.)


[quote=boopsboops]Hi thescripts people, I hope I'm in the right forum for Visual Basic Dotnet (VS 2005).
<snip>
Nov 20 '06 #2

P: 6
I really am getting confused. It seems the vb.net section isn't a live forum at all but an archive. At least, I can see no buttons there for starting a new thread or replying to anything. So I'll hang on in here.

Meanwhile, I discovered an almost identical query dating from about a year ago in the C# archive. The conclusion was to use:

Protected Overrides Function ProcessCmdKey

I've tried this a few ways but it doesn't seem to work for me. Sometimes the point gets nudged, but most of the time the form button selection cycles as before.

I'm feeling out of my depth here. Does the override belong in the form's methods or those of the custom control? What should it contain?

I would be glad if someone could give me some guidance.

regards, BB
Nov 20 '06 #3

P: 1
I am having the same problem. I found that if i hold the Ctrl Key down my user control then captures the KeyDown properly. but ofcourse i dont want to have to do it. have you found a solution?
Nov 30 '06 #4

P: 6
I am having the same problem. I found that if i hold the Ctrl Key down my user control then captures the KeyDown properly. but ofcourse i dont want to have to do it. have you found a solution?
Hi Elat, after some trial and error I did get it working. I hadn't tried the Ctrl key. It does seem to stop the buttons from flickering, but the nudging routine doesn't work. No doubt your code was slightly different.

For the record, here's an outline of my solution:

Private Enum Direction
Left
Right
Up
Down
End Enum
Private Event m_ArrowKeyPressed(ByVal dir As Direction)

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

' Prevent the normal action of the arrow keys by disabling the form buttons. They will be reenabled on KeyUp or MouseMove.
FormButtonsEnabled(False)
' 'Set the nudge direction according to the arrow key
Select Case keyData
Case Keys.Left
RaiseEvent m_ArrowKeyPressed(Direction.Left)
Case Keys.Right
RaiseEvent m_ArrowKeyPressed(Direction.Right)
Case Keys.Up
RaiseEvent m_ArrowKeyPressed(Direction.Up)
Case Keys.Down
RaiseEvent m_ArrowKeyPressed(Direction.Down)
Case Else
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function

Private Sub FormButtonsEnabled(Private Sub FormButtonsEnabled(ByVal b As Boolean)
'prevent Form1 buttons from flickering during nudging
Form1.button1.Enabled = b
'... etc.
End Sub

Private Sub Drawing_Sheet_m_ArrowKeyPressed(ByVal dir As Direction) Handles Me.m_ArrowKeyPressed
'nudge the image left (1), right (2), up (3) or down (4):
Select Case dir
Case Direction.Left : m_Image.Location.X -= 1
Case Direction.Right : m_Image.Location.X += 1
Case Direction.Up : m_Image.Location.Y -= 1
Case Direction.Down : m_Image.Location.Y += 1
End Select
EndSub

End Sub

NOTE: This result wasn't perfect because now the buttons were grayed out during nudging so they still flickered distractingly when an arrow key was held down. I dealt with that by giving the buttons a different colour: foreground ControlDarkDark and background ControlLightLight. Apparently graying out isn't defined for all colours.
Dec 9 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.