Ask coding questions

← Back to all posts
python mouse position?
skull_is_dull (42)

is it possible to return a mouses x and y position in python?

Answered by PYer (3936) [earned 5 cycles]
View Answer
PYer (3936)

This using @mat1 xterm module:

import xterm
printing = False
def getclicks():
  for char in xterm.readchrs():
    if type(char)!=str:
      x = char.x
      y = char.y
      yield (x, y, char)

import clicks
coords = clicks.getclicks()

There may be a few edits from the original because I have modified it to suit me.

The module yields only within the terminal and the top left of the terminal is (0, 0)
Please upvote if this was useful thanks!

Scoder12 (839)

What do you mean? In the terminal? Where do you want to capture the mouse position from?