Python Forum
keyboard module; must be root problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keyboard module; must be root problem
#1
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.
Reply
#2
(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.
philipbergwerf likes this post
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with memory_graph module akbarza 3 299 Mar-04-2024, 10:15 AM
Last Post: snippsat
  problem using coloeama module akbarza 3 516 Jan-08-2024, 07:31 AM
Last Post: akbarza
  problem in using subprocess module akbarza 5 944 Sep-24-2023, 02:02 PM
Last Post: snippsat
  problem in import module from other folder akbarza 5 1,261 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using pyqrcode module to create QRcode akbarza 9 1,799 Aug-23-2023, 04:17 PM
Last Post: snippsat
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 934 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  Problem with module time and leap seconds Pedroski55 3 1,190 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  keyboard module question DPaul 0 2,110 Mar-23-2021, 04:22 PM
Last Post: DPaul
  keyboard module doesn't work in the microsoft version terminal of python. username 1 2,748 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  Problem with Flask Bcrypt import module marcello86 2 5,599 Aug-31-2020, 08:10 PM
Last Post: marcello86

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020