LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   [xbindkeys] Advanced mouse binds? (https://www.linuxquestions.org/questions/linux-desktop-74/%5Bxbindkeys%5D-advanced-mouse-binds-4175428297/)

Zero Angel 09-20-2012 07:25 PM

[xbindkeys] Advanced mouse binds?
 
I have a logitech mouse with some extra buttons on it. Previously I installed xbindkeys and set it up so that b:8 and b:9 (the shoulder buttons) on the mouse changed workspaces (well, emulated some keypresses).

Code:

"xdotool key --clearmodifiers ctrl+alt+Down"
b:8
"xdotool key --clearmodifiers ctrl+alt+Up"
b:9

But now I want to get even more jazzier with it.

I want to have it set up so that b:8 acts as a modifer to my scrollwheel. So that if I scroll while holding down one of the shoulder buttons, it will change my workspace in the direction that I scroll. And I want to set it up so that when I hold the other shoulder button and use the scrollwheel it will activate the flip switcher.

Now here's the clincher. xbindkeys doesnt appear to support multiple mouse keys to perform a single action. I tried to make my .xbindkeysrc contain the following:
Code:

"xdotool key --clearmodifiers ctrl+alt+Down"
    b:9 + b:4
"xdotool key --clearmodifiers ctrl+alt+Up"
    b:9 + b:5

However this causes all kind of weird effects like causing the ctrl or alt modifiers to turn 'on' and not turn off. And meanwhile the command wont execute.

Does anyone know how to do what i'm trying to do?

Zero Angel 09-20-2012 10:21 PM

I figured it out on my own. After hours of searching, I realized that the default .xbindkeysrc file syntax could not do sophisticated enough logic to handle multiple mouse keys, however it did mention that it supported the guile scripting language so after more searching I stumbled upon this guide:

http://computers-stuff.blogspot.ca/2...nf-file-i.html


Using that guide I managed to make xbindkeys perform these functions:
  • Front shoulder button (b9) + Scrollwheel emulates CTRL+ALT+UP/DOWN
  • Rear shoulder button (b8) + Scrollwheel emulates ALT+TAB and ALT+SHIFT+TAB

The file is as follows

~/.xbindkeysrc.scm [OBSOLETE: See next post]
Code:

;;  This configuration is guile based.
;;  http://www.gnu.org/software/guile/guile.html
;;  This config script us supposed to live in the homedirectory.
;;  This was a not so quick but probably yet dirty hack by Zero Angel
;;  You'll need xdotool and xbindkeys with -guile support compiled for this to work (The Ubuntu xbindkeys will have this support by default).
;;  This is heavily based on Vee Lee's configuration file
;;  It assigns keybindings to the scroll wheel  on the fly when mouse modifier keys are pressed. Useful for mice with lots of buttons!

(define (first-binding)
"First binding"
;; Logitech Front Shoulder Button
(xbindkey-function '("b:9") b9-second-binding)
;; Logitech Rear Shoulder Button
(xbindkey-function '("b:8") b8-second-binding)
)


(define (reset-first-binding)
"reset first binding"
(ungrab-all-keys)
(remove-all-keys)
(first-binding)
(grab-all-keys))


(define (b9-second-binding)
"Front Shoulder Button Extra Functions"
(ungrab-all-keys)
(remove-all-keys)

;; Scroll Up
(xbindkey-function '("b:4")
                (lambda ()
                  (run-command "xdotool key --clearmodifiers ctrl+alt+Up&")
))

;; Scroll Down
(xbindkey-function '("b:5")
                (lambda ()
                  (run-command "xdotool key --clearmodifiers ctrl+alt+Down&")
))

(xbindkey-function '(release "b:9") (lambda ()
 (run-command "echo -e 'Placeholder for button release command' &")
(reset-first-binding)))
(grab-all-keys))

(define (b8-second-binding)
"Rear Shoulder Button Extra Functions"
(ungrab-all-keys)
(remove-all-keys)

;; Scroll Up
(xbindkey-function '("b:4")
                (lambda ()
                  (run-command "xdotool key --clearmodifiers alt+shift+tab&")
))

;; Scroll Down
(xbindkey-function '("b:5")
                (lambda ()
                  (run-command "xdotool key --clearmodifiers alt+tab&")
))

(xbindkey-function '(release "b:8") (lambda ()
 (run-command "echo -e 'Placeholder for button release command'&")
(reset-first-binding)))
(grab-all-keys))

;; (define (scroll-apps-binding)
;; "scroll-apps-binding"
;; (ungrab-all-keys)
;; (remove-all-keys)
;; (xbindkey-function '("b:4")(run-command "echo -e 'KeyStrPress Tab\nKeyStrRelease Tab' | xmacroplay :0 &"))
;; (xbindkey-function '("b:5")(run-command "echo -e 'KeyStrPress Shift_L\nKeyStrPress Tab\nKeyStrRelease Tab\nKeyStrRelease Shift_L' | xmacroplay :0 &"))
;; (xbindkey-function '("b:1") reset-first-binding)
;; (grab-all-keys))
;; (debug)
(grab-all-keys)
(first-binding)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; End of xbindkeys configuration ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

It can be a little buggy sometimes though. Not releasing the modifier keys that it uses. However this is basic, and I can always repurpose the emulation to windowmanager-specific DBUS commands that accomplish the same thing.

Zero Angel 09-22-2012 06:43 PM

Improved the script.

- Modifier keys will no longer get stuck as they are forcefully released
- Can properly handle events where the shoulder buttons are pressed by themselves (no scrollwheel used).

.xbindkeysrc.scm
Code:

;;  This configuration is guile based.
;;  http://www.gnu.org/software/guile/guile.html
;;  This config script is supposed to live in the homedirectory.
;;  Awesome script created by Zero Angel
;;  This couldnt have been possible without seeing Vee Lee's configuration file
;;  You'll need xdotool and xbindkeys with -guile support compiled for this to work (The Ubuntu xbindkeys will have this support by default).
;;  It assigns keybindings to the scroll wheel  on the fly when mouse modifier keys are pressed. Useful for mice with lots of buttons!
;;  v1.0 -- Shoulder button + scrollwheel bindings
;;  v1.1 -- Fixes some 'stuckness' problems with the modifer keys (ctrl, alt, shift)
;;  v1.2 -- Can trigger events properly if the modifier button is simply pressed and released by itself. Forcefully clears modifier keys when the shoulder buttons are depressed.

(define actionperformed 0)

(define (first-binding)
"First binding"
;; Logitech Front Shoulder Button
(xbindkey-function '("b:9") b9-second-binding)
;; Logitech Rear Shoulder Button
(xbindkey-function '("b:8") b8-second-binding)
)


(define (reset-first-binding)
"reset first binding"
(ungrab-all-keys)
(remove-all-keys)
;; Set Action Performed state back to 0
(set! actionperformed 0)
;; Forcefully release all modifier keys!
(run-command "xdotool keyup ctrl keyup alt keyup shift keyup super&")

(first-binding)
(grab-all-keys))


(define (b9-second-binding)
"Front Shoulder Button Extra Functions"
(ungrab-all-keys)
(remove-all-keys)

;; Scroll Up
(xbindkey-function '("b:4")
                (lambda ()
;; Emulate Ctrl+Alt+Up (Workspace Up)
                (run-command "xdotool keydown ctrl keydown alt key Up keyup ctrl keyup alt&")
                (set! actionperformed 1)
))

;; Scroll Down
(xbindkey-function '("b:5")
                (lambda ()
;; Emulate Ctrl+Alt+Down (Workspace Down)
                (run-command "xdotool keydown ctrl keydown alt key Down keyup ctrl keyup alt&")
                (set! actionperformed 1)
))

(xbindkey-function '(release "b:9") (lambda ()
;; Perform Action if Button 8 is pressed and released by itself
(if (= actionperformed 0) (run-command "zenity --info --title=hi --text=Button9ReleaseEvent &"))
(reset-first-binding)))
(grab-all-keys))

(define (b8-second-binding)
"Rear Shoulder Button Extra Functions"
(ungrab-all-keys)
(remove-all-keys)

;; Scroll Up
(xbindkey-function '("b:4")
                (lambda ()
;; Emulate Alt+Shift+Tab (previous window)
                (run-command "xdotool keydown alt keydown shift key Tab keyup alt keyup shift")
                (set! actionperformed 1)
))

;; Scroll Down
(xbindkey-function '("b:5")
                (lambda ()
;; Emulate Alt+Tab (next window)
                (run-command "xdotool keydown alt key Tab keyup alt")
                (set! actionperformed 1)
))

(xbindkey-function '(release "b:8") (lambda ()
;; Perform Action if Button 8 is pressed and released by itself
    (if (= actionperformed 0) (run-command "zenity --info --title=hi --text=Button8ReleaseEvent &"))
(reset-first-binding)
))
(grab-all-keys)
)

;; (debug)
(first-binding)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; End of xbindkeys configuration ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


David the H. 09-23-2012 05:40 AM

Interesting. I use a big Kensington trackball myself and I've been wanting to do something similar for a long time. I even posted something a few years ago about it (since I don't have guile experience) but got no answers (possibly because I screwed up the title for it. :doh:)

I'm going to be too busy this week to try it out right away, but I'm certainly going to look into it when I have the time. Thanks.


All times are GMT -5. The time now is 05:04 PM.