2

I've 5 buttons in my windows application. When I click arrow keys the focus changing between buttons, then only

KeyUp

event firing. How to stop this?

Karthik AMR
  • 1,694
  • 21
  • 29
  • 1
    I want the KeyUp event to be fired from exact button which had focus when I pressed the Arrow key. – Karthik AMR Feb 07 '14 at 12:18
  • It is default behavior. When you press arrow key focus will change to next control and then you release key so you got `KeyUp` event from next Button. If you don't release key for long time it will continues focus change from one to another control and when you release key you will get `KeyUp` event from focused control. – Sameer Feb 07 '14 at 12:29
  • 1
    @Sameer ya correct, But my question is how to stop the default behaviour? – Karthik AMR Feb 07 '14 at 12:30

3 Answers3

1

Subscribe to the PreviewKeyDown event instead.

Occurs before the KeyDown event when a key is pressed while focus is on this control.

As you move through the buttons, the sender parameter will contain the previously selected button.


I found a solution that should work for you, adapted from here. Apparently, MS made the decision that the arrow keys wouldn't trigger the KeyDown event, so you can't cancel them.

One workaround is to specify that your arrow keys are normal input keys, like any other key. Then the KeyDown event will fire and you can cancel the button press if you want.

private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
        e.IsInputKey = true;
}

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

You may want to read the other answers and comments in that post to see what would work best in your situation.

Community
  • 1
  • 1
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

Answer for your question in comment

    void button1_LostFocus(object sender, EventArgs e)
    {
        button1.Focus();
    }
Sameer
  • 2,143
  • 1
  • 16
  • 22
  • It only focus the button1 every time. Actually I want the focus on the button in which I pressed the arrow key. – Karthik AMR Feb 07 '14 at 12:44
0

To prevent Up from moving focus from a Button you have to utilize at least 3 methods:

    bool _focus;

    private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
            _focus = true;
    }

    private void button1_KeyUp(object sender, KeyEventArgs e)
    {
        _focus = false;
    }

    private void button1_Leave(object sender, EventArgs e)
    {
        if(_focus)
            button1.Focus(); // or (sender as Control)
    }

Trick is to use flag when user press Up and to return focus in Leave. You have to unflag in KeyUp, otherwise it would be impossible to change focus (by pressing Tab to example).

You could possible unflag in Leave, I didn't test it.

Sinatr
  • 20,892
  • 15
  • 90
  • 319