-1

There are events called 'onkeydown' and 'onkeyup' in Javascript. Can anyone please suggest the python equivalent of it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Steve
  • 381
  • 1
  • 6
  • 16
  • 1
    possible 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
  • 1
    The 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 Answers1

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
  • @martineau So answer to the OP's question! – Spirine Apr 30 '15 at 11:34
  • There's no single way to do it as it's an OS dependent operation. – martineau Apr 30 '15 at 11:36
  • @martineau So this discussion is just useless – Spirine Apr 30 '15 at 11:47
  • 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