Why do Enter and Space keys behave differently for buttons?

后端 未结 2 1673
情深已故
情深已故 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:00

    I can't find any articles explaining this and it's a really good question. I personally think that it's for functionality purposes

    Enter Key the classic AcceptButton acts like a FullClick (Click/ClickReleased) that's why if you hold it you will have the effect of clicking multiple times.

    Space however is a SingleClick (No click release until you release the key) so it can accomplish task where only a Click is required without a ClickRelease and actions where only the selection of a control is required to activate it. Like the CheckBox or RadioButtons which can't be activate with the Enter but can be activated with the Space like if you click on it.

    In conclusion, the Space would be the official MouseClick since it has the same effects of a MouseClick uppon pressing or releasing. Enter would be sort of a shortcut for a One click full click. All, of course, in the idea of giving more possibilities to the keyboard itself.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题