Msvm_Keyboard 类的 TypeText 方法
模拟一系列类型化字符。 这等效于为字符串中的每个字符调用 PressKey ,后跟 ReleaseKey 。
语法
uint32 TypeText(
[in] string asciiText
);
parameters
-
asciiText [in]
-
类型: 字符串
要键入的 ASCII 或 Unicode 字符系列。 此字符串的最大长度取决于字符串中的字符类型。
字符串类型 最大字符数 ASCII 512 Unicode 256 到 512,具体取决于字符串中的代理项对数。
返回值
类型: uint32
返回值为零表示成功。 返回值 1 表示输入字符串中不可翻译字符导致的失败。 所有其他非零值表示修改密钥状态失败。
-
已完成,无错误 (0)
-
方法参数已选中 - 作业已启动 (4096)
-
( 32768) 失败
-
拒绝访问 (32769)
-
不支持 (32770)
-
状态未知 (32771)
-
超时 (32772)
-
参数 (32773) 无效
-
系统 ( 32774)
-
此操作的无效状态 (32775)
-
数据类型 (32776) 不正确
-
系统 ( 32777) 不可用
-
内存不足 (32778)
备注
UAC 筛选可能会限制对 Msvm_Keyboard 类的访问。 有关详细信息,请参阅 用户帐户控制和 WMI。
示例
以下 C# 示例模拟键入文本。 可以在 V2) (虚拟化示例的常用实用工具 中找到引用的实用工具。
using System;
using System.Management;
namespace HyperVSamples
{
class TypeTextClass
{
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 TypeText(string vmName, string text)
{
ManagementScope scope = new ManagementScope(@"root\virtualization\v2", null);
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
ManagementObject keyboard = GetComputerKeyboard(vm);
ManagementBaseObject inParams = keyboard.GetMethodParameters("TypeText");
inParams["asciiText"] = text;
ManagementBaseObject outParams = keyboard.InvokeMethod("TypeText", inParams, null);
if ((UInt16)outParams["ReturnValue"] == ReturnCode.Completed)
{
string.Format("Text {0} was typed on {1}", text, vm["ElementName"]);
}
else
{
string.Format("Unable to type '{0}' on {1}", text, vm["ElementName"]);
}
inParams.Dispose();
outParams.Dispose();
keyboard.Dispose();
vm.Dispose();
}
static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: TypeText vmName Text");
return;
}
TypeText(args[0], args[1]);
}
}
}
以下 Visual Basic Scripting Edition (VBScript) 示例模拟键入文本。
option explicit
dim objWMIService
dim fileSystem
const wmiSuccessful = 0
Main()
'-----------------------------------------------------------------
' Main routine
'-----------------------------------------------------------------
Sub Main()
dim computer, objArgs, vmName, computerSystem, text, 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)
text = objArgs.Unnamed.Item(1)
else
WScript.Echo "usage: cscript TypeText.vbs vmName text"
WScript.Quit
end if
set computerSystem = GetComputerSystem(vmName)
set keyboard = GetComputerKeyboard(computerSystem)
if TypeText(keyboard, text) then
WriteLog "Done"
WScript.Quit(0)
else
WriteLog "TypeText operation 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
'-----------------------------------------------------------------
' Type the given text to the given keyboard
'-----------------------------------------------------------------
Function TypeText(keyboard, text)
WriteLog Format2("TypeText({0}, {1})", keyboard.ElementName, text)
dim objInParam, objOutParams
TypeText = false
set objInParam = keyboard.Methods_("TypeText").InParameters.SpawnInstance_()
objInParam.asciiText = text
set objOutParams = keyboard.ExecMethod_("TypeText", objInParam)
if objOutParams.ReturnValue = wmiSuccessful then
WriteLog Format2("'{0}' was typed on {1}", text, keyboard.ElementName)
TypeText = true
end if
End Function
'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
dim fileStream
set fileStream = fileSystem.OpenTextFile(".\Typetext.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 |
|