how to get MouseMove and MouseClick in bash?

后端 未结 6 784
野的像风
野的像风 2020-11-30 04:19

I\'m wondering how to get the MouseClick and MouseMove events in bash scripting for my own simple OS events.

Please tell me how to get that events.

6条回答
  •  旧时难觅i
    2020-11-30 04:50

    Short way

    I made a script to make the command line cursor move on mouse click :

    1. Enable xterm mouse tracking reporting
    2. Set readline bindings to consume the escape sequence generated by clicks

    It can be found here : https://github.com/tinmarino/mouse_xterm/blob/master/mouse.sh

    Long way

    Xterm have a mouse tracking feature

    echo -e "\e[?1000;1006;1015h" # Enable tracking
    echo -e "\e[?1000;1006;1015l" # Disable tracking
    
    • Mouse click looks like \e[<0;3;21M and a release \e[<0;3;21. Where 2 is x (from left) and 22 is y (from top)
    • Mouse whell up : \e[<64;3;21M
    • Mouse whell down : \e[<65;3;21M
    • Press C-v after enabling the mouse tracking to see that

    Readline can trigger a bash callback

    bind -x '"\e[<64;": mouse_void_cb' # Cannot be put in .inputrc
    bind    '"\C-h"   : "$(date) \e\C-e\ef\ef\ef\ef\ef"' #Can be put in .inputrc
    

    Readline can call multiple functions

    # Mouse cursor to begining-of-line before calling click callback
    bind    '"\C-98" : beginning-of-line'
    bind -x '"\C-99" : mouse_0_cb'
    bind    '"\e[<0;": "\C-98\C-99"'
    

    Readline callback can change cursor (point) position with READLINE_POINT environment variable

    bind -x '"\C-h"  : xterm_test'
    function xterm_test {
        echo "line is $READLINE_LINE and point $READLINE_POINT"
        READLINE_POINT=24    # The cursor position (0 for begining of command)
        READLINE_LINE='coco' # The command line current content
    }
    

    Links

    • Xterm control sequences
    • Ctrl keys as used in vim source
    • zsh script for mouse tracking : the same but in zsh (not bash)

提交回复
热议问题