Solved

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

Posted on 2004-08-13
17
266 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Error 11 58
designing in object programming 12 77
Video Player 2017 5 22
transaction in asp.net, sql server 6 33
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 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