0

I found a python module called 'mouse' (installed using 'sudo pip3 install mouse') that allows control of the mouse cursor's position using python, but the mouse.click() method that it provides doesn't do anything.

Is there a way for me to simulate clicking with this module? If not, what is another way that I can simulate a mouse click using Python? I am using Python 3.6.

  • how do you use it? You have to move mouse in position where you have element to click - `mouse.move(...)`. I'm not sure but it may click item in active window - so it may need to activate window before click. If it is in position where there is no element to click then it will not click. – furas Jun 30 '22 at 03:29
  • don't you get error message when you run it? WHen I run it on Linux Mint (based on Ubuntu) I see `ImportError: You must be root to use this library on linux.` – furas Jun 30 '22 at 03:30
  • I have tried to move the cursor so that it is on an active window hovering over the X (close window) button, and the mouse.click() function definitely doesn't work. As for the import error, typing 'sudo -i' in the terminal and typing your password will put you in the root. From here, you can install mouse at the root (if it isn't already installed there), enter a python environment in the terminal, and import mouse. – Luis Davila Jun 30 '22 at 16:58
  • I already have installed mouse as root and it still need to run module mouse as root - it would need to change some privileges in Linux Mint 20 (based on Ubuntu 20.04). So I prefer module `pynput` or `PyAutoGUI` – furas Jun 30 '22 at 17:50
  • other question: did you test it with different windows? Some programs may use non-standard method to catch mouse events (especially games) and then some modules may not work because they send standard mouse event to system and system send this event to active window. – furas Jun 30 '22 at 17:54
  • I have tried pynput and couldn't get anywhere with it because it requires me to be running an X server. I have tried to run one, but it still gives me the same error asking me to run an X server and check that the DISPLAY is configured correctly. I will check PyAutoGUI, but it looks like I'm going to need to work out a lot of dependency issues before installing it. I have tested the 'mouse.click()' function in every window, and it doesn't work in any of them. The other functions, such as 'mouse.move(args),' work in all windows, so it seems like the clicking function is the only problem. – Luis Davila Jun 30 '22 at 18:12
  • Also, the root issue can be resolved by either running 'sudo python yourfilenamehere.py' and typing your password or by typing sudo -i and typing your password to enter the root directory and then running 'python yourfilenamehere.py'. Also, make sure that you're in the same directory as the python file when executing. If you aren't executing a .py file, then exclude the 'yourfilenamehere.py' part and try 'import mouse' and 'mouse.click()'. – Luis Davila Jun 30 '22 at 18:16
  • I know how to run code with sudo but I don't want it run it with sudo - and this is why I don't use `mouse` – furas Jun 30 '22 at 22:16
  • 1
    do you run Linux without `X server`? It is unusual - and most modules which use `mouse` need `X server` because they use functions built-in in `X server` - they send mouse event to `X server` and it sends it to active window. – furas Jun 30 '22 at 22:20

1 Answers1

1

I was able to use

x@y:~$ pip3 install PyAutoGui

to install PyAutoGUI after resolving all of the dependency issues and running

x@y:~$ xhost +

in the terminal before running

x@y:~$ python3
>>> import pyautogui

I think that extra installations are needed to run 'xhost +', but I don't remember what they were. I only remember searching for a fix to an installation error telling me to check that I have an "X server" running.