Monday, June 6, 2011

KDE 4.6: desktop switching with mouse wheel

The fastest way to switch desktops in system is with the mouse wheel. In compiz, you could assign shortcuts for mouse switching, but in KDE (kwin) this is not possible. I really liked the mouse switching, so I created a small script to handle this...


How it works:
This script will switch your desktop when your cursor is at the edge of screen and you push your mouse button to the side (left or right) according to which side you'd like to switch the desktop. This means you need to have 4-direction mouse wheel (you can map the shortcut also to some other button).

For capturing the signals, I use xbindkeys, which is automatically started at the login (added to autostart) and xdotool which can get the cursor coordinates.

xbindkeys setup:
-xbindkeys is by default using configuration file ~/.xbindkeysrc
The config looks like this: 
# Switch desktop
"kwindesktopswitch 6"
b:6
"kwindesktopswitch 7"
b:7

NOTE: kwindesktopswitch is a script, which handles desktop switching and b:6 and b:7 are the mapped mouse buttons. You can find out the correct mouse button numbers with program xev.

The script kwindesktopswitch:
#!/bin/bash
##################################
# Author: KrisKo
# Year: 2011
# Description: this script uses xbindkeys to switch KDE desktops
# with mouse wheel (btn 6 and 7)
# Usage: <scriptname> [6|7]
#
# Known bugs: the script scrolls only the focused window,
# not windows under mouse cursor
#
#################################

#get mouse coordinates and store them in variables (x, y, screen)
 eval $(xdotool getmouselocation | sed -e 's/:/=/g');

#if the mouse cursor is on the left or right of the screen, move to next/prev screen
 if [ $x -eq 0 ] && [ $1 -eq 6 ]; then
 `qdbus org.kde.kwin /KWin org.kde.KWin.previousDesktop`
 exit 0

else if [ $x -eq 1279 ] && [ $1 -eq 7 ]; then
 `qdbus org.kde.kwin /KWin org.kde.KWin.nextDesktop`
 exit 0

fi
 fi

 #the parameter from xbindkeys is 6 or 7 according to direction to scroll
 #it is used to scroll when the mouse cursor is not on the right or left edge of the screen
 if [ $1 -eq 6 ]; then
 `xvkbd -xsendevent -text "\[Left]" 2>/dev/null`
 exit 0

else if [ $1 -eq 7 ]; then
 `xvkbd -xsendevent -text "\[Right]" 2>/dev/null`
 exit 0

fi
 fi

exit 1

The script is simple: first it gets mouse cursor coordinates, where the x-coordinate is compared. If it equals to 0 and the signal 6 is sent the desktop will switch to previous. Second coordinate is on the right edge, it equals 1279 (this coordinate is from program getmouselocation) and the signal 7 comes, the desktop will switch.

If the cursor is not at the 0 or 1279 x coordinate the signal Left or Right is send, which will scroll in the active window.

No comments:

Post a Comment