1
votes

I'm trying to detect when the user presses the Return key while in a Winforms textbox, but neither control statement below works when I use Keys.Enter and/or Keys.Return. It does work when I detect other keys such as Alt and Shift. What am I missing? They only vague lead that I have is that I'm testing this on a MacBook keyboard (running Windows), but surely those keys are mapped 100% correctly?

private void txtInput_KeyUp(object sender, KeyEventArgs e)
{
    if ((Control.ModifierKeys == Keys.Enter))
    {
        btnOK_Click(null, null);
    }

    if ((Control.ModifierKeys & Keys.Return) != 0)
    {
        btnOK_Click(null, null);
    }
}
1
Enter is not a modifier key...Ed S.

1 Answers

2
votes

Try using:

e.KeyCode == Keys.Enter

Control.ModifierKeys catch only if ctrl,alt,shift are pressed!

if you need catch only 'enter' pressed w/o any key pressed use:

e.KeyData == Keys.Enter