Check if key is pressed using python (a daemon in the background)

后端 未结 4 1777
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 22:55

I\'ve created a python script in which an event needs to be executed each time I press the Super (or WinKey) on my keyboard.

How can one achieve this without the pyt

4条回答
  •  执念已碎
    2020-12-13 23:37

    My previous answer apparently was completely wrong, sorry. I think the correct approach would be to read from /dev/input/event1 (?)

    This short test showed scancodes for me, even if the terminal did not have focus:

    from struct import unpack
    port = open("/dev/input/event1","rb")    
    
    while 1:    
      a,b,c,d = unpack("4B",port.read(4))    
      print a,b,c,d
    

    I do not know if /dev/input/event1 is always the keyboard or how to determine which one is, but at least for me it worked

提交回复
热议问题