1

Question

Is it possible, in a terminal emulator, to run a command whenever a mosue event happens?

What I've tried

I think I'm halfway there - using echo -e "\e[?1003h" (see here for details) and waving the mouse about over the terminal produces a desirable effect of printing a bunch of characters, related to the occurred mouse event. (Note: echo -e "\e[?1003l" turns this off)

echo -e "\e[?1003h"
# move mouse over terminal window, in the character input line, NOT to stdout:
> CF*CC+C@,C;.C:/C8/C8/C8/C8/C9/C9/C9/C90C90C80C81C81C81C81C81C90C:/C;/C;.C<.C<.

# After deleting that, I enter:
> echo -e "\e[?1003l"
# mouse move, all is quiet

I've also tried hooking it up to output to a file. This was to see if I could watch the file and run a command according to that. It went as follows:

> echo -e "\e[?1003h" >> file.txt 
# elevator music whilst I move the mouse about

> echo -e "\e[?1003l"
# move mouse a bit, nothing

> cat file.txt


# nothing but blank lines, fair enough. But, mouse moves and in the terminal input is printed...

> CF*CC+C@,C;.C:/C8/C8/C8/C8/C9/C9/C9/C90C90C8 ...

#  Delete that output, and then I type:
> echo -e "\e[?1003l"
# mouse moves, no output

What I want

How can I either capture this output so that it is going to stdout, or to a file, so that I can then use the changing output to trigger running a command? If that's not possible, is there any other way to capture when a mouse click occurs in a terminal emulator, with the event coordinates?

Nick Bull
  • 9,518
  • 6
  • 36
  • 58

1 Answers1

3

It's not output, it's input.

There are multiple ways to read input in bash. Here's one that reads everything until stopped:

echo -e "\e[?1003h"    
echo "Press enter and then ctrl-d to stop reading"
cat > myfile

When done and correctly exited with Enter and Ctrl+D you can inspect myfile to see all the mouse events.

A proper mouse application would disable local echo and terminal buffering before reading and decoding the escape sequences from the terminal.

that other guy
  • 116,971
  • 11
  • 170
  • 194