≡ Menu

Howto Use Alt-Tab on Remote and Local Desktop with AutoHotKey Script

AutoHotKey LogoOn Windows platform, most remote desktop application does not behave kindly for special keys.

Microsoft’s mstsc forwards special keys to the remote machine when window is maximized.

Realvnc does a little better job, where you can configured it to leave special keys to local machine.

So, the main problem is that we can use Alt-Tab either on local machine or remote machine, but not on both!

This can be very frustrating if you are constantly working on multiple system using remote desktop application.

If you are new to AutoHotKey, read this first: Reduce Keystrokes using AutoHotKey for Windows

Hotkey Translation

The most needed hotkey translation feature is still not well supported by remote login applications.

Reserving a hotkey and translate/remap it to Alt-Tab will make both client and remote machine happy.

We have two options here: local operations remapped Alt-Tab, or remote operations remapped Alt-Tab.

Alt-Tab is a little different from normal hotkeys, because Alt key can be in hold-position while Tab key can be pressed and release for window navigation.

The sequence should be well traced during the remapping process.

It is difficult to trace the sequence and translate it into remote machine, so local machine would better use the remapped Alt-Tab.

The following steps explain how we can solve this problem by writing a AutoHotKey script.

Re-register HotKeys on Focus Switch

First, we should re-register hotkeys when focus is switched between local and remote machine.

Most remote logic application will grab the keyboard input and let previous registered hotkey invalided. So we need to check which window is active and register proper hotkeys periodically.

loop
{
  gosub WindowWatch
  sleep 500
}
return

Avoid Multiple Registration

As we talked above, Alt-Tab has internal state, and multi registration will break the internal state.

So we need to track the last active window, and do hotkey register only when active window is switched.

Lose Focus From Remote Machine

Lose focus from remote machine by hit LWin & Tab.

I still have not figured out which window of local machine actually handles/response the Alt-Tab keystroke, so I wasted one hotkey stroke for local machine gaining focus from remote login application.

Hotkey, LWin & Tab, ShowTaskBar, ON
ShowTaskBar:
  IfWinNotExist ahk_class Shell_TrayWnd
  	return
  WinActivate
return

IfWinNotExist will found the handler of taskbar, and it should not expected to fail. This function also sets the founded taskbar as default window. So the WinActivate will let the taskbar grab focus from remote login application.

Remap LWin, Tab, and CapsLock Key

Remap LWin & Tab to Alt-Tab, LWin & CapsLock to Shift-Alt-Tab.

After local machine get the focus, just enable the remap function provided by AutoHotkey.

Hotkey, LWin & Tab, AltTab, ON
Hotkey, LWin & CapsLock, ShiftAltTab, ON

At this stage, as remote login application has released the keyboard, original Alt-Tab key still available in local machine.

Final AutoHotkey Script for Alt-Tab

Putting it all together, we will have the following code for AutoHotKey that will solve the Alt-Tab problem when you are remote desktop-ed to another machine.

#usehook

loop
{
  gosub WindowWatch
  sleep 500
}
return

WindowWatch:
  SetTitleMatchMode RegEx
  IfWinActive ahk_class .*HELLWND
  {
  	if last = "win"
  	{
  		Hotkey, LWin & Tab, AltTab, OFF
  		Hotkey, LWin & CapsLock, ShiftAltTab, OFF
  	}
  	if last <> "mstsc"
  	{
  		Hotkey, LWin & Tab, ShowTaskBar, ON
  		last = "mstsc"
  	}
  	return
  }
  IfWinActive ahk_class .*CDesktopWin
  {
  	if last = "win"
  	{
  		Hotkey, LWin & Tab, AltTab, OFF
  		Hotkey, LWin & CapsLock, ShiftAltTab, OFF
  	}
  	if last <> "vnc"
  	{
  		Hotkey, LWin & Tab, ShowTaskBar, ON
  		last = "vnc"
  	}
  	return
  }
  if (last = "win")
  {
  	return
  }
  Hotkey, LWin & Tab, AltTab, ON
  Hotkey, LWin & CapsLock, ShiftAltTab, ON
  last = "win"
  return

ShowTaskBar:
  IfWinNotExist ahk_class Shell_TrayWnd
  	return
  WinActivate
return
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book