Get current mouse/cursor position (x and y)

2,254 views
Skip to first unread message

Paulo Martins

unread,
Jun 20, 2016, 6:06:36 PM6/20/16
to pyglet-users
Hi,

Imagine that I am in a "update(dt)" function doing animation stuff, and need to position some sprite according to the mouse position: is there an easy way, like a function, to get the current mouse position (x, y coordinates)?
Or the only way is to creat something like a global variable x and y and keep track/registe the mouse x, y inside the on_mouse_motion event each time it moves, since the start of the app?

I am tired and sleepy, working in a hurry because of deadlines, searched but can't find an easy way - sorry if this question is stupid and has an obvious answer I am not seeing in this moment.

Thanks, all the best 

magu...@gmail.com

unread,
Jun 20, 2016, 6:57:39 PM6/20/16
to pyglet-users
From what I understand, its more or less like that, though I tend to sub-class the window and use it as a root class for the program to pass relevant data to other classes as needed, or for things like drawing a cursor, etc. Example:

import pyglet
from pyglet.window import mouse

class Test(pyglet.window.Window):
   
def __init__(self):
       
super(Test, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
       
self.set_exclusive_mouse(True)
       
self.clear()

   
#mouse cursor position
       
self.mouse = [0,0]

       
self.cursor = pyglet.image.load('cursor.png').get_texture()

        pyglet
.clock.get_fps()
       
self.fps_display = pyglet.clock.ClockDisplay()

        pyglet
.clock.schedule_interval(self.update, .01)



   
def update(self,dt):
       
self.draw()


   
def draw(self):
       
self.clear()
       
self.cursor.blit(self.mouse[0],self.mouse[1])
       
self.fps_display.draw()

       
   
def on_mouse_motion(self,x,y,dx,dy):
       
self.mouse[0] += dx
       
self.mouse[1] += dy

   
def on_mouse_drag(self,x,y,dx,dy,buttons,modifiers):
       
self.mouse[0] += dx
       
self.mouse[1] += dy


if __name__ == '__main__':
    window
= Test()
    pyglet
.app.run()



Benjamin Moran

unread,
Jun 21, 2016, 8:52:37 AM6/21/16
to pyglet-users
And of course if you don't subclass Window, there is also the nice decorators for window events, as you probably already know:

mouse_pos = 0, 0

@window.event
def on_mouse_motion(x, y, dx, dy):
    mouse_pos
= x, y


Keep in mind that changing the Sprite.position is a somewhat expensive operation, because the Sprite's vertex list is re-calculated every time you do. For this reason, it makes more sense to use an intermediate variable (like mouse_pos above), and update the Sprite's position once every update of your application.

Paulo Martins

unread,
Jun 26, 2016, 8:29:44 PM6/26/16
to pyglet-users

Thank you both for your time and nice answers :)

In the next morning after writing this question I did something similar to magu example code (track of the mouse positon: self.mouse = [0,0] ) and it is working like a charm.

I was afraid that having on_mouse_motion always writing the x and y in each little mouse movement could impact performance, so I asked this question, in case there were other way like global acess to the x y of the mouse. But the fact is that it is something very light, did not noticed any impact on the FPS at all, I overacted and lost time reading the documentation for something that don't exist and is not a problem.

Thank you

PS: I thought that by receiving here an answear would give me automatically an e-mail alert, but after all not. That is why I took too long to come here, thought I had no answears. Sorry dealy.

Benjamin Moran

unread,
Jun 27, 2016, 12:08:50 AM6/27/16
to pyglet-users
Glad you got it sorted out. The way pyglet does it is pretty similar to other libraries (like SDL2), where the mouse is already continuously firing update events to the Window. You just need to catch them. I really do like pyglet's event system.

Paulo Martins

unread,
Jun 27, 2016, 12:34:40 PM6/27/16
to pyglet-users
Me too, Pyglet is the most pythonic and clean thing I have encountered in a long time :D
Reply all
Reply to author
Forward
0 new messages