C#试图捕获KeyDown事件在窗体上 [英] C# trying to capture the KeyDown event on a form

查看:181
本文介绍了C#试图捕获KeyDown事件在窗体上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个小游戏,游戏打印到Windows窗体上的面板。现在我想要捕捉的keydown事件,看看它已经按下方向键,问题不过是,我似乎无法捕捉到它。

I am creating a small game, the game is printed onto a panel on a windows form. Now i want to capture the keydown event to see if its the arrow keys that has been pressed, the problem however is that i can't seem to capture it.

让我解释一下,在表单上我有4个按钮和其他各种控制和举例来说如果用户按下其中一个按钮(触发游戏事件),那么按钮具有焦点,我不能抓住用箭头键移动。

Let me explain, on the form i have 4 buttons and various other controls and if the user for instance press one of the buttons (to trigger a game event) then the button has focus and i can't capture the movements with the arrow keys.

我想是这样

private void KeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
            game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.E);
            game.DrawObjects(panel1.CreateGraphics());
        }
        else if (e.KeyCode == Keys.Right)
        {
            game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.W);
            game.DrawObjects(panel1.CreateGraphics());
        }
        else if (e.KeyCode == Keys.Up)
        {
            game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.N);
            game.DrawObjects(panel1.CreateGraphics());
        }
        else if (e.KeyCode == Keys.Down)
        {
            game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.S);
            game.DrawObjects(panel1.CreateGraphics());
        }
    }



,然后当按下形式键按下事件,我用这个

and then when the form key down event was pressed, i used this

private void MainForm_KeyDown(object sender, KeyEventArgs e)
    {
        KeyDown(e);
    }



我还添加的keydown为Windows窗体上的按钮和各种其他控件,但我没有得到任何回应了。我已经安装在函数中断点,看它是否被呼叫,但该断点从未触发?

I also added keydown for the buttons and the various other controls on the windows form, but i am not getting any response back. I have setup a breakpoint inside the function to see if it's being called, but that breakpoint never triggers?

任何想法?

最理想的是有一个一般的KeyDown事件触发(无论当前具有焦点是什么控制的),然后调用的KeyDown方法。

The most optimal was to have a general KeyDown event that triggers (regardless of what control that currently has focus) and then calls the KeyDown method.

推荐答案

覆盖 IsInputKey 行为

您必须重写 IsInputKey 行为的通知所需的右箭头键被视为InputKey,而不是作为一种特殊的行为的关键。
对于您必须覆盖每个控件的方法。
我会建议你创建你赢了按钮,让我们说的用作MyButton

You must override the IsInputKey behavior to inform that you want the Right Arrow key to be treated as an InputKey and not as a special behavior key. For that you must override the method for each of your controls. I would advise you to create your won Buttons, let's say MyButton

类下面创建一个覆盖自定义按钮的< STRONG> IsInputKey 方式使右箭头键不是作为一个特殊的键处理。从那里,你可以很容易地使它成为其他箭头键或其他任何东西。

The class below creates a custom Button that overrides the IsInputKey method so that the right arrow key is not treated as a special key. From there you can easily make it for the other arrow keys or anything else.

    public partial class MyButton : Button
    {
        protected override bool IsInputKey(Keys keyData)
        {
            if (keyData == Keys.Right)
            {
                return true;
            }
            else
            {
                return base.IsInputKey(keyData);
            }
        }
    }






之后,您可以在每个不同的按钮或窗体本身对待你keyDown事件事件:


Afterwards, you can treat your keyDown event event in each different Button or in the form itself:

在按钮的KeyDown方法尝试设置这些属性

In the Buttons' KeyDown Method try to set these properties:

private void myButton1_KeyDown(object sender, KeyEventArgs e)
{
  e.Handled = true;
  //DoSomething();
}



- 或 -

-- OR --

处理的形式共同的行为:(不设置的 e.Handled = TRUE; 的的按钮)

handle the common behaviour in the form: (do not set e.Handled = true; in the buttons)

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    //DoSomething();
}

这篇关于C#试图捕获KeyDown事件在窗体上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆