Im trying to figure out how to use the keyboard library. Every time I try to run it on spyder, I get a ImportError: You must be root to use this library on linux. I tried countering this by running the sudo python3 command in a terminal with the specified program below. However, when I do, I get a different error as seen below:
Traceback (most recent call last):
File "Mouse_simulator.py", line 9, in <module>
import pyautogui
File "/usr/local/lib/python3.8/dist-packages/pyautogui/__init__.py", line 249, in <module>
import mouseinfo
File "/usr/local/lib/python3.8/dist-packages/mouseinfo/__init__.py", line 223, in <module>
_display = Display(os.environ['DISPLAY'])
File "/usr/local/lib/python3.8/dist-packages/Xlib/display.py", line 80, in __init__
self.display = _BaseDisplay(display)
File "/usr/local/lib/python3.8/dist-packages/Xlib/display.py", line 62, in __init__
display.Display.__init__(*(self, ) + args, **keys)
File "/usr/local/lib/python3.8/dist-packages/Xlib/protocol/display.py", line 129, in __init__
raise error.DisplayConnectionError(self.display_name, r.reason)
Xlib.error.DisplayConnectionError: Can't connect to display ":0": b'No protocol specified\n'
After reading similar posts, I think it has something to do with the program requiring root access to get the correct display from a specific file, but I'm not sure. I cannot find a specific cause after trying numerous options.
The program I'm testing is below:
import pyautogui
import time
from keyboard import is_pressed as press
distance = 200
#time.sleep(5)
while distance > 0:
"""draw a spiral rectangle"""
if press("q"):
#if q is pressed, then break
break
pyautogui.drag(distance, 0, duration=0.5) # move right
distance -= 5
pyautogui.drag(0, distance, duration=0.5) # move down
pyautogui.drag(-distance, 0, duration=0.5) # move left
distance -= 5
pyautogui.drag(0, -distance, duration=0.5) # move up