Simulating longer mouse button press in python - python

Okay, so I want to make mouse macro in Python 3.8 that will automatically press and release mouse buttons (combination for fishing game, so that I don't have to manually repeat clicking mouse buttons). I have read descriptions for both mouse module and PyAutoGUI module, and from what I can see, PyAutoGUI seems more powerful, but I don't know how to set time how long mouse button will be pressed in either of those. From what I can see they have "click" function, but I want to set how long the mouse button will be pressed. Regards

here there is the parameters for pyautogui.click()
x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0, tween=linear, logScreenshot=None, _pause=True
as you can see you can modify the duration by saying
pyautogui.click(your_x, your_y, duration=5.0)
i have put five but you can put any float value

Related

Is it possible to disable the spacebar in Tkinter?

I'm working on a little game with Tkinter, but I'm facing a issue and I dont know how to fix it
The issue is that sometime when I press a button in the game (like the right arrow to move right for example), the function corresponding to the button is working well, but when I press spacebar just after, it calls the function again which is something I dont want.
So I'm wondering if there is any way to completly disable the space bar for tkinter ?
I've already tried 2 things:
Bind the spacebar to another function which is doing nothing, but when I press the spacebar it is still repeating the last button pressed
Unbind the space bar at the start of the code like this:
import tkinter as tk
game = tk.Tk()
game.unbind('<space>')
If you want, here is the full code of the game:
https://github.com/Nirs123/World_Of_Boats
Appreciate every feedback :)
Here is an answer of what I said in my comment.
You can use game.unbind_class() to unbind a specific event for a specific type of widget. To unbind the spacebar from a button, you can use game.unbind_class("Button", "<Key-space>"). You can call it right after you create game, and all the buttons in the whole window will no longer be able to be pressed with the spacebar.

Stimulate REAL mouse release event

I am trying to trigger a mouse left click event then release that click so it should be like a real mouse click.
I am currently doing it like that:
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
I've also tried pynput like that:
mouse.click(Button.left)
and:
mouse.press(Button.left)
mouse.release(Button.left)
So, all the three codes works fine and they almost work the same way... But try a real mouse click on https://cookie.riimu.net/speed/ then try the click made using Python on the same site.
The real mouse click is like that (GIF picture): https://i.vgy.me/l6JMtn.gif
But, the click made using Python is like that (GIF picture): https://i.vgy.me/dHSkux.gif
So as you can see, in the real mouse click: the circle pops in then pops out. But in the Python mouse click, the circle pops in and doesn't pop out again.
What can be causing that? And is there's any possible fix?
30 milliseconds is a correct delay between the click and the release.
So this would stimulate a real left mouse click and release:
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
time.sleep(30 / 1000)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

Pyautogui mouse clicks without actually moving the mouse

I'd like to know if it's possible to automate clicks with pyautogui without compromising the funcionality of my cursor. I automate clicks with pyautogui but my cursor becomes useless while the script is running as the cursor moves around the screen. I was wondering if it is possible to either 1) have two cursos and have pyautogui automating one while I operate the other myself, or 2) have pyautogui click on the screen without actually moving my cursor.
I'm guessing the OS (like most of them) does not have support for multiple mouse pointers. This means that pyautogui does not have it either. The closest you can get to the behavior you are describing is to save your current mouse position with pyautogui.position(), then pressing where you want to press and then jumping back to that position. When done quickly you will have control of your mouse pointer between the automated clicks.
Example:
# Save mouse position
(x, y) = pyautogui.position()
# Your automated click
pyautogui.click(200, 300)
# Move back to where the mouse was before click
pyautogui.moveTo(x, y)
PyAutoGui to my knowledge does not support this functionality, however, at least according to this thread here, using autoit with the function ControlClick allows simulated mouse clicks without any associated cursor movement.
first solution is to save the current mouse location then return to it after mouse click as follows
import pyautogui
(x,y)=pyautogui.position()
pyautogui.click(600,300)
pyautogui.moveTo(x,y)
second solution is to make a keyboard shortcut for the button then simply pressing the button without moving the mouse

Python 3 - Is there a way to keep the mouse inside the tkinter window

Python 3
Tkinter
Hey, I'm looking for something that don't allow the user to leave a window if a variable is true/false.
basicly, somethings that keeps the mouse inside the tkinter window
Not directly, no. However you could monitor the mouse position with the <Motion> event and correct it every time it goes outside. How to correct it would depend on your OS; look into pyautogui for a crossplatform solution.
You can't force the mouse to stay in the window, though you can "grab" all of the mouse events, preventing the user from clicking anywhere but on your app. This is very dangerous as you can lock up your computer if your code has a bug in it.
See How can I make area outside toplevel unclickable?

How to get mouse button controls?

I trying to work out how to get the mouse button controls.
I tried looking in pygame docs but it doesn't say; it just returns the state of the mouse. I do understand this but I don't know how to use that info to control the buttons on the mouse.
I have made a game where you could shoot bullets. Whenever you place the MOUSEBUTTONDOWN command in my python script and you try clicking the buttons on the mouse, the right and the left shoots bullets. How can I make it so that only the left mouse button only shoots bullets while the right button does something else?
Also, can you add automatic on the mouse? Instead of clicking the mouse button several times, whenever the left or the right mouse button is pressed the bullet shoots automatically instead of clicking on the button several times.
It starts to get annoying when you're playing a game. Any help is appreciated.
You can use pygame.mouse.get_pressed() (link to docs) to get a
sequence of booleans representing the state of all the mouse buttons. A True value means the mouse is currently being pressed at the time of the call.
As far as "automatic" fire goes, each time round your pygame.event loop you can check if the button is still pressed and fire another bullet.

Categories