F10 Key is not caught

限于喜欢 提交于 2019-12-22 04:51:40

问题


I have a Windows.Form and there overriden ProcessCmdKey. However, this works with all of the F-Keys except for F10. I am trying to search for the reason why ProcessCmdKey is not called when I press F10 on my Form.

Can someone please give me a tip as to how I can find the cause?

Best Regards, Thomas


回答1:


Windows treats F10 differently. An explanation is given in the "Remarks" section here on MSDN




回答2:


I just tested this code with Windows Forms on .NET 4 and I got the message box as expected.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.F10)
    {
        MessageBox.Show("F10 Pressed");
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}



回答3:


May be I got your problem, so trying to guess:

Did you set KeyPreview property of your WindowsForm to true ?

This will enable possiblity to WindowsForm proceed keypress events before they pump to the control that holds the focus on UI in that precise moment.

Let me know if it works, please.

Regards.




回答4:


In my case I was trying to match e.key to system.windows.input.key.F10 and it didn't not work (althougth F1 thru F9 did)

Select Case e.Key

Case is = Key.F10
... do some stuff

end select

however, I changed it to

Select Case e.Key

Case is = 156
... do some stuff

end select

and it worked.




回答5:


If running into this issue in a WPF app, this blog post shows how to capture the F10 key:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
        if (e.SystemKey == Key.F10)
        {
            YourLogic(e.SystemKey);
        }

        switch (e.Key)
        {
            case Key.F1:
            case Key.F2:
        }
}



回答6:


Right, and as this is a special key you must add

e.Handled = true; 

it tells the caller that you handled it.

So, your code could look like:

switch (e.Key)
...
case Key.System:
    if (e.SystemKey == Key.F10)
    {
        e.Handled = true;
        ... processing
    }


来源:https://stackoverflow.com/questions/6664420/f10-key-is-not-caught

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