description | ms.assetid | title | ms.topic | ms.date | topic_type | api_name | api_type | api_location | |||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Simulates a series of typed characters. |
5D4C9F27-84AA-4131-A9A3-2C72DB2E8909 |
TypeText method of the Msvm_Keyboard class |
reference |
05/31/2018 |
|
|
|
|
Simulates a series of typed characters. This is equivalent to calling PressKey followed by ReleaseKey for each character in the string.
uint32 TypeText(
[in] string asciiText
);
-
asciiText [in]
-
Type: string
The series of ASCII or Unicode characters to type. The maximum length of this string depends on the type of characters in the string.
String type Maximum characters ASCII 512 Unicode 256 to 512, depending on the number of surrogate pairs in the string.
Type: uint32
A return value of zero indicates success. A return value of one indicates a failure caused by nontranslatable characters in the input string. All other nonzero values indicate a failure to modify the key state.
-
Completed with No Error (0)
-
Method Parameters Checked - Job Started (4096)
-
Failed (32768)
-
Access Denied (32769)
-
Not Supported (32770)
-
Status is unknown (32771)
-
Timeout (32772)
-
Invalid parameter (32773)
-
System is in used (32774)
-
Invalid state for this operation (32775)
-
Incorrect data type (32776)
-
System is not available (32777)
-
Out of memory (32778)
Access to the Msvm_Keyboard class might be restricted by UAC Filtering. For more information, see User Account Control and WMI.
The following C# sample simulates typing text. The referenced utilities can be found in Common utilities for the virtualization samples (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]);
}
}
}
The following Visual Basic Scripting Edition (VBScript) sample simulates typing text.
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
Requirement | Value |
---|---|
Minimum supported client |
Windows 8 [desktop apps only] |
Minimum supported server |
Windows Server 2012 [desktop apps only] |
Namespace |
Root\Virtualization\V2 |
MOF |
|
DLL |
|