Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow key events not arriving

Basically, I have a form with a custom control on it (and nothing else). The custom control is completely empty, and the form has KeyPreview set to true.

With this setup, I am not receiving any KeyDown events for any arrow keys or Tab. Every other key that I have on my keyboard works. I have KeyDown event handlers hooked up to everything that has such events, so I'm sure I'm not missing anything.

Also of note is that if I remove the (completely empty) custom control, I DO get the arrow key events.

What on earth is going on here?

EDIT:

I added this to both the form and the control, but I'm STILL not getting arrow keys:

protected override void WndProc(ref Message m) {
    switch (m.Msg) {
        case 0x100: //WM_KEYDOWN
            //this is the control's version. In the form, it's this.Text
            ParentForm.Text = ((Keys)m.WParam).ToString();
            break;
    }
    base.WndProc(ref m);
}

I also checked with Spy++, and determined that the form itself is not getting any WM_KEYDOWN messages, they're all going to the control. However, that said, the control IS getting the arrow key WM_KEYDOWN messages. Sigh.

Edit 2: I've also updated the ZIP file with this version. Please look at it, if you want to help...

Edit 3:

I've figured this out, sort of. The form is eating the arrow keys, probably in an attempt to maintain focus amongst its children. This is proven by the fact that I DO get the events if the form is empty.

Anyway, if I add this code to the form, I start getting the events again:

public override bool PreProcessMessage(ref Message msg) {
    switch (msg.Msg) {
        case 0x100: //WM_KEYDOWN
            return false;
    }
    return base.PreProcessMessage(ref msg);
}

When I override this, the form doesn't get a chance to do its dirty work, and so I get my KeyDown events as I expect. I assume that a side effect of this is that I can no longer use my keyboard to navigate the form (not a big deal in this case, as it's a game, and the entire purpose of this exercise is to implement keyboard navigation!)

The question still remains about how to disable this "properly", if there is a way...

like image 755
Mike Caron Avatar asked Jul 21 '10 15:07

Mike Caron


People also ask

What do I do if my arrow keys aren't working?

Turn off Scroll Lock on your keyboard Most of the time, if your arrow keys aren't moving the cursor from cell to cell, the fix is as simple as disabling the Scroll Lock key on your keyboard.

How do I enable arrow keys?

To use the arrow keys to move between cells, you must turn SCROLL LOCK off. To do that, press the Scroll Lock key (labeled as ScrLk) on your keyboard. If your keyboard doesn't include this key, you can turn off SCROLL LOCK by using the On-Screen Keyboard.

What are the 4 arrow keys?

Cursor control keys ) and Home, PgUp, End, PgDn. The arrows are known as cursor control keys (the cursor is the flashing bar on the computer screen that shows your current position). Many keyboards also have a separate pad for these keys (look for a set of arrow keys).


1 Answers

I've done some extensive testing, and I've figured everything out. I wrote a blog post detailing the solution.

In short, you want to override the ProcessDialogKey method in the form:

protected override bool ProcessDialogKey(Keys keyData) {
    return false;
}

This will cause the arrow keys (and tab) to be delivered as normal KeyDown events. HOWEVER! This will also cause the normal dialogue key functionality (using Tab to navigate controls, etc) to fail. If you want to retain that, but still get the KeyDown event, use this instead:

protected override bool ProcessDialogKey(Keys keyData) {
    OnKeyDown(new KeyEventArgs(keyData));
    return base.ProcessDialogKey(keyData);
}

This will deliver a KeyDown message, while still doing normal dialogue navigation.

like image 85
Mike Caron Avatar answered Oct 26 '22 21:10

Mike Caron