views:

6865

answers:

4

Hello!

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.

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.

I tried something like

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);
    }

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?

Any ideas?

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.

+4  A: 

Have you set the KeyPreview property of the form to true? That will cause the form to get a "first look" at key events.

Update: getting this to work properly when a Button has focus seems to be a bit tricky. The Button control intercepts the arrow key presses and moves focus to the next or previous control in the tab order in a manner so that the KeyDown, KeyUp and KeyPress events are not raised. However, the PreviewKeyDown event is raised, so that can be used:

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    e.Handled = ProcessKeyDown(e.KeyCode);
}

// event handler for the PreViewKeyDown event for the buttons
private void ArrowButton_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    ProcessKeyDown(e.KeyCode);

}

private bool ProcessKeyDown(Keys keyCode)
{
    switch (keyCode)
    {
        case Keys.Up:
            {
                // act on up arrow
                return true;
            }
        case Keys.Down:
            {
                // act on down arrow
                return true;
            }
        case Keys.Left:
            {
                // act on left arrow
                return true;
            }
        case Keys.Right:
            {
                // act on right arrow
                return true;
            }
    }
    return false;
}

Still, the focus moves around in a rather ugly manner...

Fredrik Mörk
It's semi-working, now i get the keydown event but when i press the buttons, they still become focused and then it stops to work unless i press the top arrow key until it loops thru them all and they loose focus.
Patrick
@Patrick: yes, the arrow keys and Button control seem to have a very tight relationship. I have updated my answer pusing it one step forward.
Fredrik Mörk
+1  A: 

Hello,


Override UseInputKey behaviour


You must override the UseInputKey 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

The class below creates a custom Button that overrides the UseInputKey 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);
            }
        }
    }


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

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

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

-- OR --

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();
}
Luis Filipe
It's semi-working, now i get the keydown event but when i press the buttons, they still become focused and then it stops to work unless i press the top arrow key until it loops thru them all and they loose focus.
Patrick
How are you catching the "Keys.Left" and "Keys.Right" KeyDown events?
Luis Filipe
I edited my answer to refer IsInputKey Method
Luis Filipe
It still says "UseInputKey".
bentsai
A: 

can be solved via

ActiveControl = Button;
Button.Leave += new EventHandler(Button_Leave);
void Button_Leave(object sender, EventArgs e)
    {
        Control ctrl = (Control)sender;
        ctrl.Focus();
    }

not the best way but still works

dnkira
+1  A: 

Hello!
I believe the easiest way of solving this problem is through overriding the ProcessCmdKey() method of the form. That way, your key handling logic gets executed no matter what control has focus at the time of keypress. Beside that, you even get to choose whether the focused control gets the key after you processed it (return false) or not (return true).
Your little game example could be rewritten like this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Left)
    {
        MoveLeft(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else if (keyData == Keys.Right)
    {
        MoveRight(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else if (keyData == Keys.Up)
    {
        MoveUp(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else if (keyData == Keys.Down)
    {
        MoveDown(); DrawGame(); DoWhatever();
        return true; //for the active control to see the keypress, return false
    }
    else
        return base.ProcessCmdKey(ref msg, keyData);
}
RudolfW