Search code examples
windowswindows-7keyboard-shortcutsautohotkeyremote-desktop

Swap Ctrls and Capslock for remote access sessions using AHK for Windows 7


I am using computers campus wide to access a Windows 7 desktop machine in my office and I am having trouble with swapping CapsLock and Ctrl key.

The problem is that, I only have admin privilege on the machine in my office, and I have swapped CapsLock and Ctrl through editing the registry (regedit) by adding a proper "Scancode Map".

However, this does not help in any of the remote-access sessions, since CapsLock on the local machine remains as is.

I resorted to AHK, but setting CapsLock::LControl in scripts running on both machine does not solve the problem. Upon specifying the key-swap through AHK, pressing CapsLock does send a Ctrl-keystroke to the RDP session. However, at the local machine, the light for "CapsLock" shall also turn on (or turn off)! This indicates that, while AHK is converting a press of CapsLock to be Ctrl, the local machine takes it as an original pressing of CapsLock. Furthermore, pressing Ctrl several time will confuse AHK and end up with termination of the process.

I also tried to set the following in the script running on the local machine, but it does not help.

SetTitleMatchMode 2
#IfWinActive, Remote Desktop
    SetCapsLockState, off
#IfWinActive
; or
#IfWinActive, ahk_class TscShellContainerClass
    SetCapsLockState, off
#IfWinActive

What should I do to consistently specify the key-swap between CapsLock and Ctrl on the remotely-accessed machine, when I don't have admin-right at local machine? On my own laptop where I had also swapped CapsLock and Ctrl through registry, there is no problem with swapping those keys in remote-access sessions.

All the best,

-Linfeng


Solution

  • I've managed to use uncap together with AutoHotKey to fix this.

    uncap's GitHub: https://github.com/susam/uncap

    1. run AutoHotKey script on local machine
    2. download uncap on remote machine
    3. run on PowerShell

    path\to\uncap.exe 0x14:0x14 0x14:0xa2

    this will override the default behaviour so it doesn't render you Esc useless

    see here for detail


    More practically, one can put the following three files into a thumb drive and carry it around. (UCA not tested given the current StayHome order. Some school computer may ban *.exe files from running completely?)

    1. uncap.exe, downloaded from here. Or, refer to the Github repo for uncap an up-to-date installer.
    2. Bat file 1 to trigger the remapping. Call it Swap_Capslock_and_Ctrl.bat. And, put the following content:
    .\uncap.exe 0x14:0xa2 0xa2:0x14
    
    1. Bat file 2, to stop the script. Call it StopScript.bat, with contents:
    .\uncap.exe -k