Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: update choose to support mouse clicks #273

Open
balupton opened this issue Jan 28, 2025 · 1 comment
Open

Suggestion: update choose to support mouse clicks #273

balupton opened this issue Jan 28, 2025 · 1 comment
Labels
enhancement Improvement or suggestion PR / Bounty only External demand, as such, external funding / effort is required to make this happen

Comments

@balupton
Copy link
Member

balupton commented Jan 28, 2025

Here's the ANSI escape code for getting mouse events:

  ESC [ ? 1000 h
              X11 Mouse Reporting (default off): Set reporting mode to 2
              (or reset to 0)—see below.

   Mouse tracking
       The mouse tracking facility is intended to return
       xterm(1)-compatible mouse status reports.  Because the console
       driver has no way to know the device or type of the mouse, these
       reports are returned in the console input stream only when the
       virtual terminal driver receives a mouse update ioctl.  These
       ioctls must be generated by a mouse-aware user-mode application
       such as the gpm(8) daemon.

       The mouse tracking escape sequences generated by xterm(1) encode
       numeric parameters in a single character as value+040.  For
       example, '!' is 1.  The screen coordinate system is 1-based.

       The X10 compatibility mode sends an escape sequence on button
       press encoding the location and the mouse button pressed.  It is
       enabled by sending ESC [ ? 9 h and disabled with ESC [ ? 9 l.  On
       button press, xterm(1) sends ESC [ M bxy (6 characters).  Here b
       is button-1, and x and y are the x and y coordinates of the mouse
       when the button was pressed.  This is the same code the kernel
       also produces.

       Normal tracking mode (not implemented in Linux 2.0.24) sends an
       escape sequence on both button press and release.  Modifier
       information is also sent.  It is enabled by sending ESC [ ? 1000
       h and disabled with ESC [ ? 1000 l.  On button press or release,
       xterm(1) sends ESC [ M bxy.  The low two bits of b encode button
       information: 0=MB1 pressed, 1=MB2 pressed, 2=MB3 pressed,
       3=release.  The upper bits encode what modifiers were down when
       the button was pressed and are added together: 4=Shift, 8=Meta,
       16=Control.  Again x and y are the x and y coordinates of the
       mouse event.  The upper left corner is (1,1).

Here's using it, with the reporting turned off afterwards, otherwise mouse events will still continue to be captured:

printf '%s\n' $'\e[?1000h mouse reporting enabled'
function stop_listening_to_mouse {
	printf '%s\n' $'\e[?1000l mouse reporting disabled'
	trap - EXIT SIGINT SIGTERM
}
trap stop_listening_to_mouse EXIT SIGINT SIGTERM
while read -n 6; do printf '%s\n' ' mouse event'; done

One must then know how to parse the responses, which occurs for scrolls and clicks. Example output:

 mouse reporting enabled
^[[M "!^[[M#"! mouse event
^[[M#"! mouse event
^[[M $!^[[M#$! mouse event
^[[M#$! mouse event
^[[M '!^[[M#'! mouse event
^[[M#'! mouse event
^[[M *!^[[M#*! mouse event
^[[M#*! mouse event
^[[M !"^[[M#!" mouse event
^[[M#!" mouse event
^[[M ""^[[M#"" mouse event
^[[M#"" mouse event
^[[M $"^[[M#$" mouse event
^[[M#$" mouse event
^C mouse reporting disabled

Could be implemented in read-key with an optional --mouse flag, in which case it could be translated to say:

scroll:x,y
click:x,y
@balupton balupton added the enhancement Improvement or suggestion label Jan 28, 2025
@balupton balupton changed the title Suggestion: update choose and confirm to support mouse clicks Suggestion: update choose to support mouse clicks Jan 29, 2025
balupton added a commit to balupton/dotfiles that referenced this issue Jan 29, 2025
@balupton
Copy link
Member Author

Got a proof of concept going with:

balupton/dotfiles@4bb10c8

Now just needs to be added to a read-key --mouse. Leaving it up to PR or bounty.

@balupton balupton added the PR / Bounty only External demand, as such, external funding / effort is required to make this happen label Jan 29, 2025
@balupton balupton added this to the Post Launch [v4] milestone Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement or suggestion PR / Bounty only External demand, as such, external funding / effort is required to make this happen
Development

No branches or pull requests

1 participant