On my Kubuntu home machine, I run a script to give a beep every time a key is pressed, no matter which window or application has focus, adapted from this insightful page
#!/usr/bin/env python from Xlib.display import Display import os import sys ZERO=[] for i in range(0,32): ZERO.append(0) ignorelist=[ZERO] def main(): if os.getuid()==0: os.system("modprobe pcspkr") print("Speaker enabled, start as normal user") sys.exit() print("If no beep is heard, then run as root to enable pcspkr") disp = Display() while 1: keymap=disp.query_keymap() if keymap not in ignorelist: os.system("beep") if __name__ == '__main__': main()
The script works fine, but it binds both processors to my Intel dual-core machine for about 80%, so I can work a little with the machine. How can I reduce the processor requirements of this simple script without interfering with its operation? In other words, it should still sound when you press a key, regardless of which window or application has focus.
If this is not possible in Python, what other technologies should I look at? C? I would suggest that there is some kernel component that notifies applications of keystrokes: how else does KDE handle global shortcuts? How can I get my application to receive these notifications?
The goal is to make a sound at the moment each key is pressed , as I train my fingers to type a mechanical keyboard without a bottom, but without missed keystrokes. I just graduated from Cherry Browns before Cherry Blues, and the lack of tactical feedback takes some time to get used to.
Please note that any solution should emit a beep no matter which window has focus. This program is intended to be used as a daemon that will work against all the applications that I use.
Thanks.
source share