Msvm_Keyboard 类的 PressKey 方法
模拟按键。 成功后,密钥将处于关闭状态。
语法
uint32 PressKey(
[in] uint32 keyCode
);
参数
-
keyCode [in]
-
类型: uint32
要按下的键的虚拟键代码。 有关虚拟密钥代码的列表,请参阅 虚拟密钥代码。
返回值
类型: uint32
返回值零表示成功。 非零值指示修改密钥状态失败。
-
已完成,无错误 (0)
-
已检查方法参数 - 作业 启动 (4096)
-
(32768) 失败
-
访问被拒绝 (32769)
-
不支持 (32770)
-
状态未知 (32771)
-
超时 (32772)
-
参数 (32773) 无效
-
系统 ( 32774)
-
此操作的无效状态 (32775)
-
数据类型 (32776) 不正确
-
系统 (32777) 不可用
-
内存不足 (32778)
备注
PressKey 方法将引用映射到 VK_MENU (18) ,VK_CONTROL (17) , 和 VK_SHIFT (16 ) 分别VK_LMENU (164) 、VK_LCONTROL (162) 和 VK_LSHIFT (160) ,因为 VK_MENU、VK_CONTROL 和 VK_SHIFT 虚拟键代码不表示键盘上的实际键。
UAC 筛选可能会限制对 Msvm_Keyboard 类的访问。 有关详细信息,请参阅 用户帐户控制和 WMI。
示例
以下 C# 示例模拟按键。 可以在 虚拟化示例的常见实用工具中找到引用的实用工具, (V2) 。
using System;
using System.Management;
namespace HyperVSamples
{
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\v2", 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);
}
}
}
以下 Visual Basic Scripting Edition (VBScript) 示例模拟按键。
option explicit
dim objWMIService
dim fileSystem
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main routine
'-----------------------------------------------------------------
Sub Main()
dim computer, objArgs, vmName, computerSystem, keycode, keyboard
set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
computer = "."
set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization\v2")
set objArgs = WScript.Arguments
if WScript.Arguments.Count = 2 then
vmName= objArgs.Unnamed.Item(0)
keycode = objArgs.Unnamed.Item(1)
else
WScript.Echo "usage: cscript PressKey.vbs vmName keycode"
WScript.Quit
end if
set computerSystem = GetComputerSystem(vmName)
set keyboard = GetComputerKeyboard(computerSystem)
if PressKey(keyboard, keycode) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "PressKey failed"
WScript.Quit(1)
end if
End Sub
'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
'
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
dim query
On Error Resume Next
query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName)
set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Retrieve Msvm_Keyboard from given computer system
'
'-----------------------------------------------------------------
Function GetComputerKeyboard(computerSystem)
dim query
On Error Resume Next
query = Format1("ASSOCIATORS OF {{0}} WHERE resultClass = Msvm_Keyboard", computerSystem.Path_.Path)
set GetComputerKeyboard = objWMIService.ExecQuery(query).ItemIndex(0)
if (Err.Number <> 0) then
WriteLog Format1("Err.Number: {0}", Err.Number)
WriteLog Format1("Err.Description:{0}",Err.Description)
WScript.Quit(1)
end if
End Function
'-----------------------------------------------------------------
' Press the key with the given key code on the given keyboard
'-----------------------------------------------------------------
Function PressKey(keyboard, keyCode)
WriteLog Format2("PressKey({0}, {1})", keyboard.ElementName, keyCode)
dim objInParam, objOutParams
PressKey = false
set objInParam = keyboard.Methods_("PressKey").InParameters.SpawnInstance_()
objInParam.keyCode = keyCode
set objOutParams = keyboard.ExecMethod_("PressKey", objInParam)
if objOutParams.ReturnValue = wmiSuccessful then
WriteLog Format2("The key with code '{0}' is typed on {1}.", keyCode, keyboard.ElementName)
PressKey = true
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\PressKey.log", 8, true)
WScript.Echo line
fileStream.WriteLine line
fileStream.Close
End Sub
'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format2(myString, arg0, arg1)
Format2 = Format1(myString, arg0)
Format2 = Replace(Format2, "{1}", arg1)
End Function
'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
Format1 = Replace(myString, "{0}", arg0)
End Function
要求
要求 | 值 |
---|---|
最低受支持的客户端 |
Windows 8 [仅限桌面应用] |
最低受支持的服务器 |
Windows Server 2012 [仅限桌面应用] |
命名空间 |
Root\Virtualization\V2 |
MOF |
|
DLL |
|