Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio

前端 未结 5 518
小蘑菇
小蘑菇 2020-11-29 03:36

Can anyone tell me the difference between the KeyDown event, the KeyPress event and the KeyUp event? I checked the msdn site and it do

5条回答
  •  遥遥无期
    2020-11-29 03:46

    In addition to the other answers:

    When trying to figure to which of these events you should hookup your action, mind that the KeyDown event will be fired multiple times while the key is held down. Sometimes you want this behavior, sometimes not. Based on that I suggest the following usage (based on my experience):

    (Order in which events are fired)

    KeyDown

    Occurs: When key is pressed and while held down
    Usage: Perform action immediately on button press or even multiple times when held down
    Example: Moving cursor with arrow keys

    .

    KeyPress

    Occurs: Character key is pressed (Higher level event)
    Usage: Anything typing related
    Example: Handle textbox input

    .

    KeyUp

    Occurs: Key is released
    Usage: Perform critical action which should only occur once per keystroke
    Example: Write data to file

提交回复
热议问题