简体   繁体   中英

C# key press problem?

I have a countdown timer - a user enters values and then it start a new form - A min size one or a max size one based on a radio button selected from first form. There is buttons on the user control form to pause/stop the timer, etc. However I want to also add if a keyboard button is pressed do the same.

Here is the code for click the pause button...

    private void btnPause_Click (object sender, EventArgs e)
    {
        _CountdownTimer.Pause ();
    }

This works fine and if the pause button is clicked it pauses the Timer. I then tried to add the following code for KeyDown - does anyone know why this would not be working? When I press p it just keeps counting down.....

Thanks.

    private void btnPause_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.ToString() == "p")
        {
            _CountdownTimer.Pause();
        }
    }

That's because e.KeyCode.ToString() does not return "p" The correct code is:

private void btnPause_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.P)
        {
            _CountdownTimer.Pause();
        }
    }

You should use the debugger and set breakpoints to make sure that the event is fireing. It the event does fire, make sure that it enters the if.

Correct Property is KeyCode

private void btnPause_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.P)
    {
        _CountdownTimer.Pause();
    }
}

And better use Form's KeyDown Event .

The KeyDown event is only fired if the control has focus. You should use the KeyDown event from the form to check if the user is pressing the p key.

Also, e.KeyCode does not need to be a string. Just check the Keys enum.

The KeyDown event delivers virtual keys. Like Keys.P. If you want to detect typing keys, like "p" then you should use the KeyPressed event instead.

But that's not the proper way. The Button control already knows you to display and handle shortcut keys. Set its Text property to &Pause. The & character indicates the mnemonic, the P gets underlined as soon as the user holds down the Alt key. And pressing Alt+P automatically invokes the Click event. A Windows user interface standard, no need to educate the user. Another significant advantage of doing it this way is that the button doesn't have to be focused. And you don't have to write any code. Recommended.

If I get it right, what you are trying to acheive is that when the user presses the "p" button while the timer runs the clock need to be stopped. in the case that the button is not focused, the keydown event will be fired by the parent of the button, that is the form itself. therefore you'll need to handle the form's KeyDown event with the code that was proposed by Skomski.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM