I know that I can use the KeyDown
, KeyUp
, and KeyPress
events to handle keyboard input, but the input uses ASCII input, meaning that it's not smooth (ie when I hold down a key, the events are repeated as if I was typing something: triggering it once, waiting half a second, and only then repeating). How can I get raw key input from the keyboard that directly checks whether or not a key is pressed down, instead of looking at the keyboard buffer?
It's only the KeyPress
event that looks at the keyboard buffer.
When a key is pressed the KeyDown
event is triggered, and if it's not a control key (shift, ctrl, alt etc.) it also produces a character for the keyboard buffer which triggers the KeyPress
event.
As long as the key is pressed, more characters will be generated for the keyboard buffer, each triggering another KeyPress
event.
When a key is released, the KeyUp
event is triggered.
To keep track of keys that are pressed, you use the KeyDown
and KeyUp
events. There is no way to directly check if a key is pressed or not, the keyboard only communicates with the computer by sending down and up events for keys.
Note that the KeyDown
event is not always followed by a KeyUp
event for the same key, but the events can overlap. Typing an uppercase A for example typically produces these KeyDown
and KeyUp
events:
KeyDown for shift
KeyDown for a
KeyUp for a
KeyUp for shift
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.