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

后端 未结 4 1786
被撕碎了的回忆
被撕碎了的回忆 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

    You can use PyUserInput for this purpose, which is a cross-platform solution, compatible, in your case, with X11.

    Example with Super:

    from pykeyboard import PyKeyboardEvent
    
    class MonitorSuper(PyKeyboardEvent):
        def tap(self, keycode, character, press):
            '''Monitor Super key.'''
            if character == 'Super_L':
                if press:
                    print('Super pressed')
                else:
                    print('Super released')
    mon = MonitorSuper()
    mon.run()
    

提交回复
热议问题