I'd like to be able to run a script in WSL2 on the host Windows machine which sends signals to a Hyper-V VM which will be interpreted as mouse clicks and key presses. I've already done this on my Linux machine with a KVM VM, is it possible to do the same on a Windows machine with a Hyper-V VM?
1 Answers
First up, there's no Linux software that you can run in WSL that is going to be able to send input events to Windows applications (such as Hyper-V). For instance, xdotool in WSL can only interact with WSL-launched GUI applications.
Second, it seems in my quick Google searches (and a quick test on my own) that normal "SendKeys" type behaviors don't work from a Host Windows to a Guest Hyper-VM VM.
However, Microsoft does provide special hooks into Hyper-V for this, and those hooks can be used from WSL, through the Windows Interop. However, in my testing, some of these are a bit challenging.
As an aside, the biggest obvious challenge in either Linux or Windows VMs is going to be that programmatically sending keystrokes/mouse-clicks from the Host to the Guest VM may be fairly non-deterministic. In other words, if you want to click an "Okay" button in the Guest VM, how does the Host know the XY coordinates of that button? Keystrokes aren't quite as bad since you can anticipate the "normal behavior" of the guest based on prior keystrokes. E.g.:
Win+R (wait 2 seconds to make sure the Run dialog has time to appear) notepadEnter.
Expected behavior, of course, for that would be start Notepad.
I'm assuming that you know this already and have some plan in place for it, since you've done this from Linux to a KVM Guest.
However, I would propose that, if at all possible, running the input process inside the VM would be more deterministic (and far easier) as well. It could still accept control from the host OS (and WSL) via some type of RPC (even if network-based).
But back to your core question, yes, if you need to send a keystroke or mouse click to a Hyper-V VM, you can use a combination of this Stack Overflow answer, a hint from the comment below it, and the ability of WSL to run powershell.exe through its Interop feature.
Note that, if you are using a Linux VM, then my experience is the same as that of the comment there from @mwfearnley:
Strangely I'm getting gibberish on my CentOS VM with TypeText. TypeKey (blogs.msdn.microsoft.com/virtual_pc_guy/2016/03/09/…) works OK for single keypresses though.
I can use TypeKey, but not TypeText. So the following in Bash in WSL will type the letter "a" in my Ubuntu 20.04 Hyper-V guest:
powershell.exe -c $'
$VMName = "Ubuntu 20.04 Server"
$VMCS = Get-WmiObject -Namespace root\\virtualization\\v2 -Class Msvm_ComputerSystem -Filter "ElementName=\'$($VMName)\'"
$keyboard = $VMCS.GetRelated("Msvm_Keyboard")
$keyboard.TypeKey(65)'
Not too useful on its own, I agree. You would have to build up your input character-by-character, which is why I used the word "challenging" above.
There's an example in the Stack Overflow post as well for mouse input, but I did not test it since I'm running Ubuntu Server with no mouse input.
As an alternative, you may want to consider AutoHotkeys, which is reported to work with Hyper-V (when running AutoHotkey as Administrator) in this question.
- 28,025