rdesktop swallows keyboard events

July 06, 2011 [GNOME, Tech]

The Linux remote desktop client rdesktop grabs all keyboard events by default, which is good, because it allows e.g. you to Alt-Tab between applications and Ctrl-Alt-Del as if you were in front of the remote machine.

However, it is also bad because you can't use the standard keyboard shortcuts to switch between virtual desktops. Varius Googling had convinced me that this problem was not soluble, since X only provides a global keyboard-grabbing function XGrabKeyboard, and once the event has been grabbed and fed to rdesktop, there doesn't appear to be a way to re-emit it back up to the window manager so that it can be used to switch desktops.

Kudos to Sunner, therefore, for coming up with a workaround. It's really simple: you have to press the key twice, and the first time, rdesktop simply ungrabs the keyboard. The second time, the window manager receives the event as normal.

Here's my version of Sunner's patch, against rdesktop 1.6.0. It removes Alt-Tab support since I don't need that, and adds Ctrl-Alt-Up and -Down, because I do need those:

--- rdesktop-1.6.0/xkeymap.c	2011-07-06 12:49:01.000000000 +0100
+++ rdesktop-1.6.0-mod/xkeymap.c	2011-07-06 11:40:24.000000000 +0100
@@ -598,6 +598,25 @@
 				ui_seamless_toggle();
 			break;

+		case XK_Left:
+		case XK_Right:
+		case XK_Up:
+		case XK_Down:
+			if ((get_key_state(state, XK_Alt_L) || get_key_state(state, XK_Alt_R))
+					&& (get_key_state(state, XK_Control_L)
+						|| get_key_state(state, XK_Control_R)))
+			{
+				/* Ctrl-Alt-Left/Right/Up/Down:
+				 * Ungrab the keyboard so that user can use Windows manager's hot keys */
+				extern RD_BOOL g_fullscreen;
+				if (g_fullscreen) { /* Turn to normal window. Otherwise, rdesktop will be always on top */
+					xwin_toggle_fullscreen();
+				}
+
+				XUngrabKeyboard(g_display, CurrentTime);
+				return True;
+			}
+			break;
 	}
 	return False;
 }

(Download it here: rdesktop-1.6.0-ungrab-on-ctrl-alt-direction.patch)

And as usual here is how to patch your Ubuntu system to include this patch:

sudo apt-get install build-essential fakeroot dpkg-dev
sudo apt-get build-dep rdesktop
mkdir tmp
cd tmp
apt-get source rdesktop
cd rdesktop-*
wget http://www.artificialworlds.net/blog/wp-content/uploads/rdesktop-1.6.0-ungrab-on-ctrl-alt-direction.patch
patch -p1 < rdesktop-1.6.0-ungrab-on-ctrl-alt-direction.patch
dpkg-buildpackage -rfakeroot -b
cd ..
sudo dpkg -i rdesktop*.deb

Now someone just needs to add support for reading your window manager settings to determine automatically what shortcuts you use to switch desktops, and/or support for a config file listing keys that make rdesktop ungrab the keyboard.