Listening to keyboard events without trapping them?

后端 未结 4 1761
别跟我提以往
别跟我提以往 2021-02-05 14:03

I\'m writing an command-line application which listens for Control key release events in X Windows and alerts another process when it detects them.

Being new to GNU/Linu

4条回答
  •  悲&欢浪女
    2021-02-05 14:28

    Thanks to the pykeylogger library mentioned by Croad Langshan, and to the helpful example code provided by Tim Alexander, the author of such library, I've been able to change my program to:

        #!/usr/bin/env python
    
        from pyxhook import HookManager
    
        watched_keys = ["Control_R", "Control_L"]
    
        def handle_event (event):
            if event.Key in watched_keys:
                print "KeyRelease"
    
    
        hm = HookManager()
        hm.HookKeyboard()
        hm.KeyUp = handle_event
        hm.start()
    

    This program accomplishes my goal without any issue. You can read the fields of the "event" object for further information about the event (see source code of "pyxhook.py").

提交回复
热议问题