Search code examples
mouseshortcut

How to assign various commands to a mouse button, time dependent in xbindkeys?


I need to execute differents commands according to the time I keep pressed a mouse key. for example

  1. ctrl+v
  2. ctrl+c
  3. copyq menu #clipboard history

I was looking for an alternative to XMBC in linux, I'm using xbindkeys from a time to now but what I miss from my old config is executing various commands assigned to a button respect to different delays each one. I found the scripts in xbindkeys doubleclick useful but those don't acomplish my goal. My choices then were working with bash or with guile I go with bash I'll leave my solution below.


Solution

  • My solution, I uploaded to a repo but its a small script that takes differences between the press time and the release time.

    #!/bin/bash
    # hold_n_release.sh
    
    BASE="`dirname "$(readlink -f "$0")"`"
    BUTTON=$1
    DELAY=$2
    PROGS=$BASE/hold_n_release.$BUTTON.commands
    LOCK=$BASE/tmp/hold_n_release.$BUTTON.lock
    LINES=$(wc -l < $PROGS)
    
    if [ -z "$BUTTON" -o -z "$DELAY" -o ! -f $PROGS ]; then
      echo "Usage : hold_n_release <Button> <Delay (sec)> [release]"
      echo "put the commands on a file named $BASE/hold_n_release.<Button>.commands one per line" 
      exit
    fi
    EPOCH=$(date +'%s')
    
    if [[ "$3" == "release" &&  -e $LOCK ]] ; then
      LASTTIME=`cat $LOCK`
      CHOICE=$(( (EPOCH - LASTTIME) / DELAY + 1 )) 
      # echo $CHOICE
      if [ $CHOICE -le $LINES ]; then
        exec $(sed  -n "$CHOICE{p;q}" $PROGS) &
      fi
    else 
      rm -f $LOCK
      echo  "$EPOCH" > $LOCK; 
    fi
    

    It needs a file with the commands each one in a line like in the example below, the last line it's intended.

    xdotool key ctrl+v
    xdotool key ctrl+c
    copyq menu
    
    

    Then simple using in xbindkeysrc in the form:

    #Multicommand hold
    "hold_n_release.sh 8 1"
      b:8
    #Multicommand release
    "hold_n_release.sh 8 1 release"
      b:8 + Release
    

    More solutions are welcomed.