![]() |
| keyboard module; must be root problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: keyboard module; must be root problem (/thread-33152.html) |
keyboard module; must be root problem - philipbergwerf - Apr-02-2021 Two problems:
Context; I have made a piano keyboard in tkinter canvas that inputs notes to my program. When I hold shift while clicking the keyboard I want it to be in chord mode. Else it is in melody mode and the music cursor is going to the next position. RE: keyboard module; must be root problem - bowlofred - Apr-02-2021 (Apr-02-2021, 09:56 PM)philipbergwerf Wrote: How to use keyboard without being root/sudo? For this particular module, you cannot. This is listed as a limitation in the docs: Quote:To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requires root. The function itself seems okay (but you should probably profile it to see what the performance is). I'd be more concerned that the check was being run when it wasn't necessary. Maybe add some logging and make sure it's only being called on button presses and not other times. RE: keyboard module; must be root problem - philipbergwerf - Apr-04-2021 (Apr-02-2021, 10:44 PM)bowlofred Wrote:I found a solution that works excellent using tkinter!(Apr-02-2021, 09:56 PM)philipbergwerf Wrote: How to use keyboard without being root/sudo? def keypress(event): renderkey() global shiftkey if event.keysym == 'Shift_L' or event.keysym == 'Shift_R': shiftkey = 1 else: return def keyrelease(event): global shiftkey if event.keysym == 'Shift_L' or event.keysym == 'Shift_R': shiftkey = 0 else: return root.bind('<KeyPress>', keypress) root.bind('<KeyRelease>', keyrelease)Using the 'shiftkey' variable I can detect if the shiftkey is pressed or not. In the keypress and keyrelease function, I can put multiple key-pressed variables if I want. |