?
Solved

How do I capture the keyboard cursor up/down/left/right event?

Posted on 2004-08-13
17
Medium Priority
?
273 Views
Last Modified: 2010-04-15
How do I capture thekeyboard  cursor up/down/keft/right events?  THese can be from the number pad or the little cursor buttons nexxt to it.  The closest I have come is turning on num lock and capturing the NumPad8/2/4/6 events.  But I want to leave num lock off and I really want to capture the 4 cursor keys on my keyboard.

My application is a c# windows form.
0
Comment
Question by:lynx2sw1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11797456
Do you want all events or just the ones from a particular control?  And how are you capturing the keys now?
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11797490

I am capturing some events now by handling the KeyDown event in the main form.  But it seems only to work for the main keyboard, not the cursor, and not all of the numeric keypad.  This seems to be working fine with the other events.  When the other controls get the focus I dont want their events anyway.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798144
Strange, it works for all keys on my keyboard including the cursor keys on the keypad.  It reports key values of Left, Right, etc. which are part of the System.Windows.Forms.Keys enumeration.  Do you have some fancy keyboard that needs a driver or something?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798208
Its an IBM Net Vista.  I didn't install a driver the last time I installed Win2K on it.
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798245
I tried downloading the program to my IBM T30 laptop which has no numeric key pad.


When I hit any key excpet the cursor keys.  Even "Browser Forward".

Do I have to set some property differenty?

Here is the code setting up my handlers.  I tried it with and without the Key Preview with the same result.
                  //
                  // MainForm
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(1016, 730);
                  this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                  this.Menu = this.mainMenu;
                  this.Name = "MainForm";
                  this.Text = "itcouldbe";
                  this.KeyPreview=true;
                  this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyDown);
                  this.KeyPress+= new KeyPressEventHandler(MainForm_KeyPress);

0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798280
But isince it works for you I did what I should have done in the first place.  I wrote a simple program form scratch.

This one *does* capture my cursor keys.  So I have to figure out what strange thing in my real application is capturing my cursor keys but nothing else.
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798284
I looked in my misbehaving application.  It had one control that handles key down events (not cursor) and I commented this out.  It didn't fix my problem.  
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798322
I added a text box and a button to my new clean application.

Now I don't capture the cursor keys anymore.  

The reason seems to be that clickng the cursor key changes the focus to another control.  So the event is getting handled in that way.

So how can I get around that?  Or can I?  Do I just have to glue little arrows to soome keys that do work and call them my cursors for this app?
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798356
You'll need to find a real .NET GUI expert for that one.  Once you put controls on the form (other than labels, it seems), the controls handle the key events and the form does not get them any more because the form itself does not get the focus.  There must be a way to bubble the events up to the main form, but I don't know what it is.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798370
OK, turns out to be really easy.  I just set the KeyPreview property of teh form to true after the call to InitializeComponents in the for constructor and I get all the key events again.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798375
Oops, you are correct - no cursor keys.  Everything but that.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798384
Have you check MS knowledge base for bug reports of this?  Seems like a bug.
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798425
I am getting there!

I added an implementation of the interface IMessageFilter and it is capturing events when the cursors or anytthing else are triggered.

0
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 11798439
It's quite bizarre, really.  I put a bunch of different controls on the form.  If you're in a text box, you get all the keys in the form keyup/keydown handlers.  If you're on a checkbox or button (controls that don't handle cursor keys), you only get a KeyUp event but not a KeyDown.  The KeyDown is apparently handled internally and used to navigate between controls.  So you would seem always to get a KeyUp event.
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798499
     
I added an implementation of IMessage Filter

to my application I did this:
                        Application.AddMessageFilter(new MessageFilterer(this));

and hereis the application.

public class MessageFilterer: IMessageFilter
      {
            private MainForm _mainForm;

            public KeyBoardHandler(MainForm mainForm)
            {
                  _mainForm=mainForm;
            }

            public bool PreFilterMessage(ref Message m)
            {

                  if(m.Msg==256)//WM_KEYDOWN
                  {
                        
                        switch(m.WParam.ToInt32())
                        {
                              case 38:      //VK_UP                                                               break;
                                                                 }


                  }

                  return false;
            }
0
 
LVL 1

Author Comment

by:lynx2sw1
ID: 11798507
Thanks d. Sorry for the wild ride.  I am sure I wouldnt have gotten there without someone to bounce ideas off of.  You ought to get some points for that since the aim was to get this darn thing working so I can go forward.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11798530
Looks good.  I'll have to remember that.

And here's the story with the event handler.  It turns out that if the form decides to handle the key itself, it does not generate an event at all.  So, when you put controls on the form, the form handles KeyDown on the cursor keys to effect navigation.  You therefore do not get events for these keys because the form does not generate them any more.  If the focus is in a control from which you cannot navigate with the cursor keys (like the textbox) you DO get the events.  So although it seems strange, it is really quite logical in a Microsoft sort of way.

So if you absolutely need the KeyDown events, the message filter is the way to go.

Whew...  who'd have thunk it would be that hard.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question