Is there a cross-platform python low-level API to capture or generate keyboard events?

后端 未结 6 480
北恋
北恋 2020-12-16 01:21

I am trying to write a cross-platform python program that would run in the background, monitor all keyboard events and when it sees some specific shortcuts, it generates one

6条回答
  •  孤城傲影
    2020-12-16 02:19

    There is no such API. My solution was to write a helper module which would use a different helper depending on the value of os.name.

    On Windows, use the Win32 extensions.

    On Linux, things are a bit more complex since real OSes protect their users against keyloggers[*]. So here, you will need a root process which watches one of[] the handles in /dev/input/. Your best bet is probably looking for an entry below /dev/input/by-path/ which contains the strings "kbd" or "keyboard". That should work in most cases.

    [*]: Jeez, not even my virus/trojan scanner will complain when I start a Python program which hooks into the keyboard events...

提交回复
热议问题