Why do Enter and Space keys behave differently for buttons?

后端 未结 2 1663
情深已故
情深已故 2021-02-18 19:14

As far as I know, these are the only keys that react when a button has focus.

Pressing Enter instantly \'clicks\' the button, even if you keep it the key dow

2条回答
  •  你的背包
    2021-02-18 20:05

    You're seeing two different behaviors, which aren't associated except that they both deal with keyboard events on a winform.

    Enter is special because it's the keypress to activate the acceptButton of a form. In fact, you missed another key that can affect buttons: Esc is the cancelButton, and will throw events as well.

    As PhaDaPhunk explained, Space is a MouseClick for any component that accepts a MouseClick, but I haven't found a detailed explanation for it. I'd assume it's the default behavior of all controls. The Microsoft guide to accessibility seems to imply that is so in their section on keyboard-based navigation

    Incidentally, this Microsoft support knowledge base entry seems to show that the spacebar implementation went from Button.Click to Button.MouseClick. Perhaps that's the reason for it's different behavior.

提交回复
热议问题