Skip to content

Commit

Permalink
Enhance compatibility #7 + migrate to AHK v2.0
Browse files Browse the repository at this point in the history
Added support for Hyper-V Virtual Machine Connection (#7).
The new WinWaitActive name pattern is checked against the process name instead of the window name, so it should now work with any system language.
  • Loading branch information
neon-dev authored Jan 8, 2024
1 parent fc9fc0f commit c60f55b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
69 changes: 31 additions & 38 deletions RDPHotkeyHelper.ahk
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
;@Ahk2Exe-SetMainIcon rhh.ico
;@Ahk2Exe-Set FileVersion, 1.1.1.0
;@Ahk2Exe-Set ProductVersion, 1.1.1.0
;@Ahk2Exe-Set FileVersion, 1.2.0.0
;@Ahk2Exe-Set ProductVersion, 1.2.0.0
;@Ahk2Exe-PostExec "MPRESS.exe" "%A_WorkFileName%" -q -x, 0,, 1, 1

#SingleInstance Force
#NoEnv
#KeyHistory 0
UpdateTrayIcon()
Menu, Tray, Tip, RDP Hotkey Helper
Menu, Tray, NoStandard
Menu, Tray, Add, Exit

global RdpWindowTitle := % (SubStr(A_Language, -1) = "07" ? " - Remotedesktop" : " - Remote Desktop") ; matches both classic RDP client and the app from the Microsoft store
SetTitleMatchMode, 2
DetectHiddenWindows, On
KeyHistory(0)
If (!A_IsCompiled && FileExist("rhh.ico")) {
TraySetIcon("rhh.ico")
}
A_IconTip := "RDP Hotkey Helper"
Tray:= A_TrayMenu
Tray.Delete()
Tray.Add("Exit", Exit)
ExeNamesPattern := "i)\\(mstsc|ApplicationFrameHost|VMConnect)\.exe$"
SetTitleMatchMode("RegEx")
While true {
WinWaitActive, %RdpWindowTitle%
Suspend, On ; rebind all hotkeys, otherwise they won't work in RDP fullscreen mode
Suspend, Off
Sleep, 250
WinWaitNotActive, %RdpWindowTitle%
global RdpHwnd := 0
WinWaitActive("ahk_exe " ExeNamesPattern)
RdpHwnd := WinExist("A")
Suspend(true) ; rebind all hotkeys, otherwise they won't work in RDP fullscreen mode
Suspend(false)
Sleep(250)
WinWaitNotActive("ahk_id " RdpHwnd)
}

#UseHook
Expand All @@ -31,37 +34,27 @@ Media_Stop::PassToLocalMachine()
Media_Play_Pause::PassToLocalMachine()

PassToLocalMachine() {
WinGetText, ActiveWindowTitle, A
If Not InStr(ActiveWindowTitle, RdpWindowTitle) {
Send {%A_ThisHotKey%}
If (RdpHwnd == 0 || WinExist("A") != RdpHwnd) {
Send("{" A_ThisHotKey "}")
Return
}
WM_APPCOMMAND := 0x0319
If (A_ThisHotKey = "Volume_Mute")
PostMessage, WM_APPCOMMAND, 0, 8<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_VOLUME_MUTE
PostMessage(WM_APPCOMMAND, 0, 8<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_VOLUME_MUTE
Else If (A_ThisHotKey = "Volume_Down")
PostMessage, WM_APPCOMMAND, 0, 9<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_VOLUME_DOWN
PostMessage(WM_APPCOMMAND, 0, 9<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_VOLUME_DOWN
Else If (A_ThisHotKey = "Volume_Up")
PostMessage, WM_APPCOMMAND, 0, 10<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_VOLUME_UP
PostMessage(WM_APPCOMMAND, 0, 10<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_VOLUME_UP
Else If (A_ThisHotKey = "Media_Next")
PostMessage, WM_APPCOMMAND, 0, 11<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_MEDIA_NEXTTRACK
PostMessage(WM_APPCOMMAND, 0, 11<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_MEDIA_NEXTTRACK
Else If (A_ThisHotKey = "Media_Prev")
PostMessage, WM_APPCOMMAND, 0, 12<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_MEDIA_PREVIOUSTRACK
PostMessage(WM_APPCOMMAND, 0, 12<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_MEDIA_PREVIOUSTRACK
Else If (A_ThisHotKey = "Media_Stop")
PostMessage, WM_APPCOMMAND, 0, 13<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_MEDIA_STOP
PostMessage(WM_APPCOMMAND, 0, 13<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_MEDIA_STOP
Else If (A_ThisHotKey = "Media_Play_Pause")
PostMessage, WM_APPCOMMAND, 0, 14<<16,, ahk_class Shell_TrayWnd ; APPCOMMAND_MEDIA_PLAY_PAUSE
PostMessage(WM_APPCOMMAND, 0, 14<<16, , "ahk_class Shell_TrayWnd") ; APPCOMMAND_MEDIA_PLAY_PAUSE
}

UpdateTrayIcon() {
If Not (A_IsCompiled) {
try {
Menu, Tray, Icon, rhh.ico
}
} Else {
Menu, Tray, Icon, *
}
Exit(*) {
ExitApp()
}

Exit:
ExitApp
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# RDPHotkeyHelper
RDPHotkeyHelper is an AutoHotkey script which redirects media keys to the local machine while working within a remote desktop session. It works with Microsoft's standard remote desktop connection and the Microsoft store app.
Using media keys on the remote host is still possible by simply holding down ctrl or alt when pressing the media key.

### Note
Window detection is language dependent in order to also support the store app. If your client is not detected, replace [` - Remote Desktop`](https://github.com/neon-dev/RDPHotkeyHelper/blob/main/RDPHotkeyHelper.ahk#L13) with your localized window title suffix.
RDPHotkeyHelper is an AutoHotkey v2 script which redirects media keys to the local machine while working within a remote desktop session. It works with Microsoft's standard remote desktop connection, the Microsoft store app, as well as the Hyper-V Virtual Machine Connection.
Using media keys on the remote host is still possible by holding down `Ctrl` or `Alt` when pressing the media key.

0 comments on commit c60f55b

Please sign in to comment.