There are events called 'onkeydown' and 'onkeyup' in Javascript. Can anyone please suggest the python equivalent of it?
Asked
Active
Viewed 1,195 times
-1

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

Steve
- 381
- 1
- 6
- 16
-
1possible duplicate of [Detect key input in Python](http://stackoverflow.com/questions/17815686/detect-key-input-in-python) – Michel de Nijs Apr 30 '15 at 11:17
-
1The javascript events you are referring to are specific to web-browsers. In what context would your python code be running? – ekhumoro Apr 30 '15 at 19:23
1 Answers
1
Without any external library, python can't provide GUI features, such as events handling. Listening to an event such onkeydown
or onkeyup
is thus impossible with the python sdl.
If you really want to react to these events, you must use an external library providing event-driven operations, such as the Qt binding PyQt, the TKinter module, or other libraries.

Spirine
- 1,837
- 1
- 16
- 28
-
Detecting keystrokes isn't something specific to GUIs — which the OP never mentioned — and is quite possible without one. – martineau Apr 30 '15 at 11:31
-
-
-
-
No, I don't think it's useless. My point was that your statement that it's impossible in Python without a GUI library/module is incorrect. – martineau Apr 30 '15 at 12:11
-
@martineau actually `spirine` is right, there is no easy way to track `key_up` and `key_down` events, you could catch only key_press. Feel the difference. And using key_press and key repeat action you could do the workaround. However there is a another thing like pygame. – Reishin Apr 30 '15 at 12:49
-
@martineau But is it constructive for the OP? Couldn't you help hime, instead of trying to show me in what extend I'm wrong? – Spirine Apr 30 '15 at 12:55
-
The context of the question was an equivalent to some event handlers *of a web browser window*. It is indeed impossible to have a window and the equivalent event handlers without, well, a GUI. If the context is instead a console application, then it's just text input, and the question is not too well phrased. – Alejandro Exojo Oct 05 '22 at 08:46