微软远程桌面下Jetbrains ide返回与前进快捷键失效的解决方案

in 默认分类 with 0 comment

问题

在使用微软的远程桌面客户端连接到远程计算机使用jetbrains家的ide时,发现返回(Ctrl+Alt+Left Arrow )与前进(Ctrl+Alt+Right Arrow )这两个快捷键竟然失效了,这是什么原因导致的呢?

相关资料

后面查阅资料发现,其主要原因是微软远程桌面客户端拦截了这两个快捷键,导致服务端无法收到相应的按键序列。

此问题在jetbrains的支持中也有过提问,问题如下:
In my Remote Desktop, ctrl+alt+left is not working for navigation

根据superuser中一条帖子(Remote Desktop: Sending Ctrl-Alt-Left Arrow/Ctrl-Alt-Right Arrow to the remote PC)中的回答,主要的办法有两个:

解决办法

体验了一下Microsoft Store版本的RDP,感觉就是个半成品,体验很差,pass。

那可靠的方案则只剩使用AutoHotkey这个强大的工具进行快捷键的转换了,原理就是在客户端将快捷键转换为一个别的组合键,然后再在服务端将快捷键转换回来,达到我们正常使用这两个快捷键的目的,具体的内容在上面superuser中的帖子里有说,我这里也引用记录一下:

  1. “rdp hotkeys_slave.ahk” :
#SingleInstance Force
#IfWinActive, ahk_exe mstsc.exe
;Send Alt+Win+Left when user types Ctrl+Alt+Left
^!Left::
send !#{Left}
return

;Send Alt+Win+Right when user types Ctrl+Alt+Right
^!Right::
send !#{Right}
return

这个脚本是为了将本地的快捷键转换为另一组按键,这里是将Ctrl+Alt+Left/Right 转换为了Ctrl+Win+Left/Right,注意这里只针对mstsc.exe(即远程桌面客户端)生效,避免影响其它程序。

  1. “rdp hotkeys_master.ahk” :
#Persistent
SetTimer, ReloadOnRDPMaximized, 500
return

ReloadOnRDPMaximized:
If WinActive("ahk_class TscShellContainerClass")
{
    WinGet, maxOrMin, MinMax, ahk_class TscShellContainerClass

    if (maxOrMin = 0) {
        WinGetPos, PosX, PosY, WinWidth, WinHeight, ahk_class TscShellContainerClass

        if (PosY = 0) {
            ; it is fully maximized therefore reload "script.ahk"
            Run "autohotkey" "%A_ScriptDir%\rdp hotkeys_slave.ahk"

            ; wait until window gets deactivated so you don't reload it again.
            WinWaitNotActive, ahk_class TscShellContainerClass

        }
    }
}

这个脚本是为了解决在远程桌面全屏后会失效的问题。

  1. “rdp hotkeys_server.ahk” :
;Send Ctrl+Alt+Left when user types Ctrl+Win+Left
!#Left::
send !^{Left}
return

;Send Ctrl+Alt+Right when user types Ctrl+Win+Right
!#Right::
send !^{Right}
return

这个脚本是将Ctrl+Win+Left/Right 转换回Ctrl+Alt+Left/Right,这样即可正确的触发ide中的Back/Forward操作。

这三个脚本中,前两个脚本(“rdp hotkeys_master.ahk” 、“rdp hotkeys_slave.ahk”)需要在客户端启动最后那个脚本(“rdp hotkeys_server.ahk” )需要在服务端启动,如果需要在开机时自动启动,可以将对应的脚本放入windows的启动目录中(按Win+R后输入shell:startup,回车即可打开启动目录)。

结束

现在,Back/Forward快捷键在远程桌面中已经可以正常使用了。当然,你也可以根据上面的ahk脚本,将其中的快捷键换成你想要的,不过AutoHotkey的组合键定义只支持最多三键,四键及以上的组合键是不受支持的,这点需要注意。

参考