Arrow key events not arriving

梦想与她 提交于 2019-12-01 17:25:23

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.

If focus is your issue, and you can't get your user control to take a focus and keep it, a simple work-around solution would be to echo the event to your user control on the key event you are concerned about. Subscribe your forms keydown or keypress events and then have that event raise an event to your user control.

So essentially, Form1_KeyPress would Call UserControl1_KeyPress with the sender and event args from Form1_KeyPress e.g.

protected void Form1_KeyPress(object sender, KeyEventArgs e)
{
    UserControl1_KeyPress(sender, e);
}

Otherwise, you may have to take the long route and override your WndProc events to get the functionality you desire.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!