I tried the existing solutions (like https://github.com/russelldavis/AutoHotkey-Scripts/blob/master/vmware.ahk) but didn't work for me.
My idea is that instead of letting the ahk script recapture the focus with tricky methods, it is better to let the guest or remote OS notify the host OS to call the ahk script when receiving the shortcut key.
The whole design is shown in the figure below, we need to implement 1. TCP client 2. TCP server 3. ahk wrapper. The TCP client runs on the guest or remote system. I bind the hotkey to the TCP client in the system settings of the client system, so by pressing the hotkey the TCP client sends a simple request to the TCP server running on the host. Then host runs the ahk wrapper to regain the focus (by sending a Ctrl-Alt escape key to VMware), and replay the shortcut keys, and finally the originally desired ahk script is executed.
Suppose the shortcut key we want to send is F12, my implemented code is as follows:
1. TCP client (python)
Code: Select all
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("hostip", 11655))
sock.send("Invoke".encode())
sock.close()
Code: Select all
$port = 11655
$endpoint = new-object System.Net.IPEndPoint ([system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()
while($true) {
$client = $listener.AcceptTcpClient()
$Stream = $client.GetStream()
$StreamReader=New-Object System.IO.StreamReader $stream
$what = $StreamReader.ReadLine()
$client.Close()
Write-Output "$what"
if($what -eq "Invoke") {
Write-Output "Invoking"
& "ahk\ahk_wrapper.exe"
} elseif($what -eq "Exit") {
$listener.Stop()
Write-Output "Exiting"
break
}
}
Code: Select all
SendMode Event
Send {Ctrl down}{Alt down}{Alt up}{Ctrl up}
Send {F12}
return