ENTER键的虚拟键代码是什么
本文关键字:是什么 代码 ENTER 虚拟 | 更新日期: 2023-09-27 18:26:26
我已经通过windows WMI实现了一个远程hyper-v管理类,但ENTER键的键代码是什么
该方法是";按键";并且该类是Hyper-V WMI类中的Msvm_ Keyboard
示例代码在Msvm_Keyboard类的PressKey方法中
上面示例中ENTER键的第二个参数是什么
根据虚拟密钥代码表;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);
}
}
我发现了问题。输入键的代码"0x0D"是正确的,但Microsoft示例代码中存在错误。按第40行中Msvm_Keyboard类
的键方法:
if ((UInt16)outParams["ReturnValue"] == ReturnCode.Completed)
上面的铸造是不正确的,然后我认为虚拟钥匙代码是不正确。