Search code examples
user-interfaceansi-escapeterminal-emulator

Get mouse position in pixels using escape sequences


I'm trying to obtain the position of the mouse in pixels within an application running in a terminal.

  • The top answer to how to get MouseMove and MouseClick in bash? explains how to get the mouse position, counted in character cells, not in pixels.
  • I'm looking for a solution which also works if the app is running on a remote server and accessed via SSH (using xdotool will not work in this case, unless ssh -X was used).
  • I guess the solution will therefore involve escape sequences or an IOCTL.
  • It's okay if the escape sequences only work with one or few terminal emulators (I can use a detection mechanism to provide a fallback on the terminals which lack support for the escape sequence).
  • If the escape sequence only works on a few terminal emulators, I'm also curious to know the "group" of escape sequences that allow graphical output on these terminals (e.g. Sixel, Tektronix or ReGIS).

The goal is to embed small GUI elements in mostly text-based applications. It is currently possible on quite a few terminal emulators using Sixel, Tektronix or ReGIS do draw things, and \e[1000h or similar escape codes to get mouse events, unfortunately these mouse events are low-resolution (the coordinates in character cells, not in pixels).


Solution

  • xterm reports the mouse position with pixel resolution with the following escape sequences:

    • switch on pixel resolution: \e[2;1'z
    • report mouse position: \e['|

    Details are described at http://invisible-island.net/xterm/ctlseqs/ctlseqs.html