我已经通过Windows WMI实现了远程hyper-v管理类,但是对于ENTER键, keycode 是什么。
方法是“PressKey”,类是Hyper-V WMI类中的Msvm_Keyboard类。
示例代码在PressKey method of the Msvm_Keyboard class中
上面示例中ENTER键的第二个参数是什么?
根据{{3}},它必须是“0”或“0x0D”,但这个样本都不能接受它们!
示例代码:
class PressKeyClass
{
static ManagementObject GetComputerKeyboard(ManagementObject vm)
{
ManagementObjectCollection keyboardCollection = vm.GetRelated
(
"Msvm_Keyboard",
"Msvm_SystemDevice",
null,
null,
"PartComponent",
"GroupComponent",
false,
null
);
ManagementObject keyboard = null;
foreach (ManagementObject instance in keyboardCollection)
{
keyboard = instance;
break;
}
return keyboard;
}
static void PressKey(string vmName, int keyCode)
{
ManagementScope scope = new ManagementScope(@"root\virtualization", null);
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
ManagementObject keyboard = GetComputerKeyboard(vm);
ManagementBaseObject inParams = keyboard.GetMethodParameters("PressKey");
inParams["keyCode"] = keyCode;
ManagementBaseObject outParams = keyboard.InvokeMethod("PressKey", inParams, null);
if ((UInt16)outParams["ReturnValue"] == ReturnCode.Completed)
{
string.Format("Key {0} was pressed on {1}", keyCode, vm["ElementName"]);
}
else
{
string.Format("Unable to press key {0}' on {1}", keyCode, vm["ElementName"]);
}
inParams.Dispose();
outParams.Dispose();
keyboard.Dispose();
vm.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: PressKey vmName keyCode");
return;
}
string vmName = args[0];
int keyCode = int.Parse(args[1]);
PressKey(args[0], keyCode);
}
}
答案 0 :(得分:0)
我发现了这个问题。代码“0x0D”对于输入密钥是正确的,但Microsoft示例代码中存在错误。 PressKey method of the Msvm_Keyboard class
在第40行:
if ((UInt16)outParams["ReturnValue"] == ReturnCode.Completed)
上面的演员是不正确的,然后我认为虚拟键码不正确。