get position of mouse in python on click or button press - python

I want to get the x, y position of the mouse (in windows 11) and use this position in the rest of the code.
I have tried two different modules but neither seem to work.
pyautogui (for a mouse click or button press)
keyboard (for a button press)
So far, i am able to get the current position (with pyautogui), but i cannot break out of the while loop to proceed to the next piece of code or even return the function.
Here is the function with my attempts:
import time
import pyautogui
import keyboard
def spam_ordinates():
''' function to determin the mouse coordinates'''
print('press "x" key to lock position...')
while True:
# Check if the left mouse button is clicked
time.sleep(0.1)
print(pyautogui.displayMousePosition())
# various methods i have tried ...
if keyboard.is_pressed('x'):
print('x key pressed...')
break
if pyautogui.mouseDown():
print("Mouse clicked!")
break
if pyautogui.keyDown('x'):
print('x key pressed (autogui)...')
break
# Get the current mouse position
x, y = pyautogui.position()
print(f'spam at position: {x}, {y}')
return x, y
# call function
ords = spam_ordinates()
i see answers like this:
Python get mouse x, y position on click, but unfortunately it doesn't actually return a value on the mouse click or button press.
So, how can i break out of the while loop such that the function returns the x, y position of the mouse?
update
it appears as though print(pyautogui.displayMousePosition()) was preventing the code from breaking out of the while loop.
I am not sure why, but commenting out that line corrected the issue.

I noticed that for some reason the print(pyautogui.displayMousePosition()) line of code was creating problems from breaking out of the loop.
when the above print statement was removed, i was able to use any of the modules:
pynput
keyboard
so this code works with the `keyboard module:
def spam_ordinates():
''' function to determin the mouse coordinates'''
print('press "x" key to lock position...')
while True:
# Check if x key is pressed
time.sleep(0.1)
if keyboard.is_pressed('x'):
print('x key pressed...')
break
# Get the current mouse position
x, y = pyautogui.position()
print(f'spam at position: {x}, {y}')
return x, y
I cannot explain completely why print(pyautogui.displayMousePosition()) caused this error, other than it must have been blocking the if statements that would have evoked the break.
I post this answer in case anybody else encounters the same.

Related

pyautogui.displayMousePosition() none

I'm using pycharm and this is my code.
import pyautogui
while True:
position = pyautogui.displayMousePosition()
print(position)
After I hit play, the code starts working however the only response I get is Press "Ctrl-C to quit" and after I cancel the code an infinite list of none appears.
"pyautogui.DisplayMousePosition()" is more complex. You can search why it's not working but you can use the code below.
import pyautogui
while True:
x, y = pyautogui.position() # Setting x and y to the coordinates of mouse
print(x, y) # Printing x and y values
You can say "print(pyautogui.position().x, pyautogui.position().y)" but if you want to use x and y on something diffrent, use "x,y=pyautogui.position()".
Note that the code above will print coordinates so fast until closing the program :)

I need the "if" to go once but not end the loop

I'm making a program to control the mouse with WASD but whenever I press the button the mouse just moves to the top of the screen when I need it to move slightly then be able to move again if I press the key again
I've tried to have the loop turn off then back on but it just ends the loop before it can switch back on.
import keyboard
from pynput.mouse import Controller
mouse = Controller()
repeat = True
while repeat:
if keyboard.is_pressed('w'):
mouse.move(0, -5)
if keyboard.is_pressed('s'):
mouse.move(0, 5)
I need it to move up slightly when I press w but stop when it isn't pressed so I can press it again.
You might want your keyboard not to be so sensitive, so you may need to sample the pressing time. sleep can help in this case. In the code below, the program will sleep for 0.2 seconds every time it enters while loop.
import keyboard
import time
repeat = True
while repeat:
time.sleep(0.2)
if keyboard.is_pressed('w'):
mouse.move(0, -5)
if keyboard.is_pressed('s'):
mouse.move(0, 5)
# do something
EDIT: I was misunderstanding what you want, just sleep is enough for this. The problem is because your computer does everything too fast, so a while loop might be completed in a microsecond or less. So it sees that you are pressing the key for too long (though you think you did fast) then it just repeats the loop until you release the key. That's why your mouse always moves to the top.
Sampling is the way to tell computer "if you've done, then sleep, don't do anything else".
Another way is to count how many times it loops (ie, scaling), but it looks like dump since the computer has to do worthless things.
import keyboard
from pynput.mouse import Controller
mouse = Controller()
repeat = True
w_count, s_count = 0, 0
while repeat:
if keyboard.is_pressed('w'):
w_count += 1
if w_count == 10: # i'm scaling it
mouse.move(0, -5)
w_count = 0
if keyboard.is_pressed('s'):
s_count += 1
if s_count == 10: # i'm scaling it
mouse.move(0, 5)
s_count = 0
There are also other ways: measure the pressing time, edge detection (detect key release)... You can search about "software debouncing" for more ideas and find one best fits your requirement.

Getting Turtle in Python to recognize click events

I'm trying to make Connect 4 in python, but I can't figure out how to get the coordinates of the screen click so I can use them. Right now, I want to draw the board, then have someone click, draw a dot, then go back to the top of the while loop, wipe the screen and try again. I've tried a couple different options but none have seemed to work for me.
def play_game():
"""
When this function runs, allows the user to play a game of Connect 4
against another person
"""
turn = 1
is_winner = False
while is_winner == False:
# Clears screen
clear()
# Draws empty board
centers = draw_board()
# Decides whose turn it is, change color appropriately
if turn % 2 == 0:
color = RED
else:
color = BLACK
# Gets coordinates of click
penup()
onscreenclick(goto)
dot(HOLE_SIZE, color)
turn += 1
As well intentioned as the other answers are, I don't believe either addresses the actual problem. You've locked out events by introducing an infinite loop in your code:
is_winner = False
while is_winner == False:
You can't do this with turtle graphics -- you set up the event handlers and initialization code but turn control over to the main loop event handler. My following rework show how you might do so:
import turtle
colors = ["red", "black"]
HOLE_SIZE = 2
turn = 0
is_winner = False
def draw_board():
pass
return (0, 0)
def dot(color):
turtle.color(color, color)
turtle.stamp()
def goto(x, y):
global turn, is_winner
# add code to determine if we have a winner
if not is_winner:
# Clears screen
turtle.clear()
turtle.penup()
# Draws empty board
centers = draw_board()
turtle.goto(x, y)
# Decides whose turn it is, change color appropriately
color = colors[turn % 2 == 0]
dot(color)
turn += 1
else:
pass
def start_game():
"""
When this function runs, sets up a new
game of Connect 4 against another person
"""
global turn, is_winner
turn = 1
is_winner = False
turtle.shape("circle")
turtle.shapesize(HOLE_SIZE)
# Gets coordinates of click
turtle.onscreenclick(goto)
start_game()
turtle.mainloop()
Run it and you'll see the desired behavior you described.
I'm assuming that your using Turtle in python(hence the name.)
If that's the case, Here's a link to a helpful post: Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates
I know, i know. I hate just link answers as much as the next guy. But The post I gave a link to can probably do a much better job of answering your question than I can.
~Mr.Python
Assuming you're using turtle as mentioned in your title:
>>> import turtle
>>> help(turtle.onscreenclick)
Help on function onscreenclick in module turtle:
onscreenclick(fun, btn=1, add=None)
Bind fun to mouse-click event on canvas.
Arguments:
fun -- a function with two arguments, the coordinates of the
clicked point on the canvas.
num -- the number of the mouse-button, defaults to 1
Example (for a TurtleScreen instance named screen)
>>> onclick(goto)
>>> # Subsequently clicking into the TurtleScreen will
>>> # make the turtle move to the clicked point.
>>> onclick(None)
That means that your callback function, which you have apparently named goto, will take two parameters, an X and Y location.
import turtle
def goto(x, y):
print('Moving to {}, {}'.format(x,y))
turtle.goto(x, y)
turtle.onscreenclick(goto)
turtle.goto(0,0)
Each click that you make will move the turtle to a different position. Note that turtle already has an event loop - you don't need one of your own. Just respond to the clicks.
basically, you need to add an 'x' and 'y' parameter for the onclick and onscreenclick functions. You don't need to use them, they're just dummy params. After filling those out the clicks will work no problem:
window = turtle.Screen()
This function uses the x, y params because i'm saving the clicks in order to specify an area to fill with turtles
def on_left_click_save_coordinates(x, y):
global counter, Fill_COORS1, Fill_COORS2
counter += 1
print(x, y)
if counter == 1:
Fill_COORS1 = (x, y)
elif counter == 2:
Fill_COORS2 = (x, y)
counter = 0
This one doesn't use the x,y params because they are dummies, this one is used to allow multiple options, one of which exits, another tells the turtle to fill in the specified area saved in clicks above.
def on_right_click_open_options(x, y):
global going
last_color = options(window, filler, Fill_COORS1, Fill_COORS2, LAST_BLOCK_USED)
if type(Last_COLOR) == type(bool):
going = True
window.onscreenclick(on_click, btn=1)
window.onscreenclick(open_options, btn=3)
This is an example of a snippet of my code. hope this helps.
btn 3 refers to the right click
btn 1 is the default and isn't necessary to specify and refers to left click
btn 2 is the scroll wheel click, not scroll.
and sorry if this isn't formatted the best, it's my first time posting to stackoverflow. Hope it helps nonetheless

Python: Keyboard input without repeat delay

For those who don't know, repeat delay is the slight pause between, when holding down a key, the letter first appearing and it repeating. This might be a useful feature when typing, however when you start to write games, it becomes very annoying. An example is when you need to move a character; it will move a tiny bit, pause, and then start to move again. Tkinter code:
ball = canvas.create_rectangle(50, 50, 100, 100)
def move():
canvas.move(ball, 0, 3)
canvas.bind_all("<space>", move)
If space is pressed, the ball will move down 3 pixels, pause, and then start moving normally. I was wondering if there is any way to avoid that pause, for example a module that reads directly from the keyboard, and not the windows-processed keyboard. I know that it is possible to "cheat" by, for example, automatically running the function when you expect the delay to occur; sadly that is inaccurate and can result in choppy movement. Thanks in advance
Make a recursion loop for while it is pressed. Or that's how I do it, at least.
moving = False
def move():
global moving
moving = True
def stop_moving():
global moving
moving = False
def myloop():
global moving
if moving == True:
canvas.move(ball, 0, 3)
root.after(1, myloop)
root.bind('<space>', lambda e: move())
root.bind('<KeyRelease-space>', lambda e: stop_moving())
just make sure you call your loop once before root.mainloop()
#like this
root.after(1, myloop)
root.mainloop()

Don't print result after calling get_axis() in pygame

I'm writing a code that reads the value of a joystick axis and then sends this value to an arduino. It works, however I do not want it constantly print 0 to the LXTerminal when nothing is being pressed.
The code prints: SDL_JoystickGetAxis value:0: repeatably when the axis is not being pressed.
It then prints:
SDL_JoystickGetAxis value:-32768:
SDL_JoystickGetAxis value:0:
SDL_JoystickGetAxis value:-32768:
The left drive is
-1.0
Repeatably when the axis is being pulled all the way down, or a number between -1 and 1 depending where the joystick is.
I only want it to print The left drive is <leftDrive> when the left axis is being pressed, and then nothing when it is not. How do I achieve this?
Here is my code:
import pygame
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
leftDrive = 0
rightDrive = 0
while True:
try:
pygame.event.pump()
joyCheckLeft = joystick.get_axis(1)
joyCheckRight = joystick.get_axis(2)
if joyCheckLeft != 0:
pygame.event.pump()
leftDrive = joystick.get_axis(axisLeftDrive)
print 'The left drive is'
print leftDrive
writeNumber(int(2+(leftDrive*-10)))
pygame.event.pump()
time.sleep(0.1)
except KeyboardInterrupt:
quit()
Note: The code doesn't work without pygame.event.pump(). This is needed so that "get_axis()" actually returns the value of the axis.
This is related to this question Disable the Pygame console Output and the thread: http://archives.seul.org/pygame/users/Aug-2009/msg00110.html This seems to have been fixed in the bitbucket source https://bitbucket.org/pygame/pygame/downloads you could try downloading the version from there.

Categories