Python Forum
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:
  1. If I use
    import keyboard
    I get 'ImportError: You must be root to use this library on linux.'. I found already that I need to install keyboard using 'sudo pip3 install keyboard' but after reinstalling using sudo the same problem stays. How to use keyboard without being root/sudo?
  2. I tried running my app using 'sudo python3 app.py' but my app became incredibly slow. Why? I want to detect if the shift key is pressed when I click the <Button-1>.
    if shiftkeypressed: do something
    For the shiftkey I use this function:
    def shiftkey(): while 1: if keyboard.is_pressed("shift"): shift = 1 else: shift = 0
    In another function I can check if shift == 1 and perform a action on that. How would you detect if the shift key is pressed?

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:
(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.
I found a solution that works excellent using tkinter!
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.


This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.