473,569 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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_K eyDown(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) 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
4 3268
boopsboops
6 New Member
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 "discussion s" tab. I only just discovered the /vb/ link at the bottom of the page.)


[quote=boopsboop s]Hi thescripts people, I hope I'm in the right forum for Visual Basic Dotnet (VS 2005).
<snip>
Nov 20 '06 #2
boopsboops
6 New Member
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
elat
1 New Member
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
boopsboops
6 New Member
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_ArrowKeyPress ed(ByVal dir As Direction)

Protected Overrides Function ProcessCmdKey(B yRef 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.
FormButtonsEnab led(False)
' 'Set the nudge direction according to the arrow key
Select Case keyData
Case Keys.Left
RaiseEvent m_ArrowKeyPress ed(Direction.Le ft)
Case Keys.Right
RaiseEvent m_ArrowKeyPress ed(Direction.Ri ght)
Case Keys.Up
RaiseEvent m_ArrowKeyPress ed(Direction.Up )
Case Keys.Down
RaiseEvent m_ArrowKeyPress ed(Direction.Do wn)
Case Else
Return MyBase.ProcessC mdKey(msg, keyData)
End Select
End Function

Private Sub FormButtonsEnab led(Private Sub FormButtonsEnab led(ByVal b As Boolean)
'prevent Form1 buttons from flickering during nudging
Form1.button1.E nabled = b
'... etc.
End Sub

Private Sub Drawing_Sheet_m _ArrowKeyPresse d(ByVal dir As Direction) Handles Me.m_ArrowKeyPr essed
'nudge the image left (1), right (2), up (3) or down (4):
Select Case dir
Case Direction.Left : m_Image.Locatio n.X -= 1
Case Direction.Right : m_Image.Locatio n.X += 1
Case Direction.Up : m_Image.Locatio n.Y -= 1
Case Direction.Down : m_Image.Locatio n.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 ControlLightLig ht. Apparently graying out isn't defined for all colours.
Dec 9 '06 #5

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

Similar topics

3
10050
by: Jerome Terry | last post by:
Hi, Why don't UserControl's received Key events for the arrow keys? Further, when a user control is added to a Form, why does the form stop receiving Key events for the arrow keys? I have tried setting IsInputKey(Keys.Left, Keys.Right, Keys.Up, Keys.Down), but that doesn't work. Regards, Jerome.
1
1540
by: Msqln | last post by:
Hi I made a form with picturebox and scrollbars. However, scrollbars react to arrow keys. What I would like is that scrollbars react only to mouse. Is there a way to ignore keyboard for scrollbars or to know if mouse has been used on scrollbar?
1
1407
by: Serdge Kooleman | last post by:
i would like to write my event that will react on specific keys in my custom DataGrid (WinForms)... please help to do this. so far i have this custom grid... and i create it in main object namespace CustomGrid
2
3179
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp The problem I am having is that the data in the custom datagridcolumn is not saved to viewstate and after...
4
5880
by: Stuart | last post by:
I hope someone can shed some light on an error I've been experiencing for sometime now, but can no longer continue to ignore :-( I've created a custom entity class which implements IEditableObject. I have then created a custom collection for this custom entity which naturally inherits CollectionBase and implements IBindingList. When I bind...
1
10130
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) */
2
3914
by: Steve | last post by:
Hi All I am using a numericupdown in a VB.net 2005 App and set the interceptarrowkeys property to false when not in edit mode The numericupdown still scrolls the value when I click on the arrows?? Is there a known bug in this control??
1
1245
by: Sparky74 | last post by:
Hi. There are so many help offerings out there for the detection of keypresses. I would like to know what the "correct way" is to intercept a keypress for a control. I have overidden the ProcessKeyPreview() method on a PropertGrid and I'm trying to intercept the arrows keys. Whats bugging me is that the wParam value is returning 0x25 (37) for...
2
3310
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 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...
0
7694
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...
0
7921
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. ...
0
8118
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...
1
7666
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...
0
7964
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...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1208
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.