How to get mouse button controls? - python

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.

Related

How do you prevent that a button is clicked too fast again?

I need to detect a mouse click to create a wait for a button so you can't spam. Is there a better way, and if not can you tell me how?
I don't know whether to search for a button pause or a mouse click, or if it would be the right way.

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.

Simulating longer mouse button press in 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

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

In PySide, detect mouse movement and mouse button state within a QGraphicsRectItem, without it being selected

I have a program somewhat similar to Microsoft Paint, and I'm working on implementing canvas resizing. I need to detect if the mouse cursor has moved into resizing range, and if the left mouse button is held down while the mouse is being moved. I have a QGraphicsRectItem set up as my canvas, and I want to detect mouse movement and mouse button state in my QGraphicsRectItem, without it being selected.
I've been using QGraphicsItem.hoverMoveEvent for detecting if the cursor is within resizing range or not, but when the left mouse button is held down, hoverMoveEvent is no longer triggered.
There's mouseMoveEvent, but as the docs say:
If you do receive this event, you can be certain that this item also
received a mouse press event, and that this item is the current mouse
grabber.
In my program, it's impractical for my canvas to be the current mouse grabber when resizing. I may have other objects that need to retain their selection states while the canvas is being resized.
I realize I could do this in the QGraphicsScene containing my QGraphicsRectitem, but to avoid interdependence between them, I'd like to be able to do this all in my QGraphicsRectItem.
So my question is: How, if possible, do I detect mouse movement and mouse button states on a QGraphicsRectitem without the it being selected?

Categories