0

i want to use Pyautogui and Keyboard module at the same time (Using Python), but i can't do that. i have installed Keyboard module and Pyautogui module too, but when i try to run Pyautogui via root i get this error (sudo python3)

    raise error.DisplayConnectionError(self.display_name, r.reason)
Xlib.error.DisplayConnectionError: Can't connect to display ":1": b'Authorization required, but no authorization protocol specified\n'

without root, it works. but keyboard doesn't work without root.

    raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.

I am using .is_pressed() function in keyboard module.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Varsima
  • 31
  • 1
  • 6

1 Answers1

0

Install pyautogui using root:

sudo pip install pyautogui

Then run your main.py:

sudo python main.py

If the above doesn't work then a better alternative would be pynput. pynput doesn't require any root permission.

pynput example:

from pynput import keyboard

def on_press(key):
    if key.char == "a":
        print(f"alphanumeric key {key.char} pressed")

listener = keyboard.Listener(on_press=on_press)

listener.start()

Output:

alphanumeric key a pressed
rafathasan
  • 524
  • 3
  • 15