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 (3586) [earned 5 cycles]
View Answer
Commentshotnewtop
PYer (3586)

This using @mat1 xterm module:
clicks.py

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)

main.py

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 (778)

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