Skip to content

Commit 65c95a1

Browse files
committed
VMKeyStrokes: Add MODS for support SpecialKey Input
You can push SpecialKeyInput like F1... F12, TAB, ESX, BACKSPACE, ENTER, UP, DOWN, Left, Right From David Rodriguez (www.sysadmintutorials.com) @SysTutorials
1 parent 3290db1 commit 65c95a1

File tree

1 file changed

+149
-84
lines changed

1 file changed

+149
-84
lines changed

powershell/VMKeystrokes.ps1

Lines changed: 149 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,85 +21,125 @@
2121
Set-VMKeystrokes -VMName $VM -StringInput "root" -ReturnCarriage $true
2222
.EXAMPLE
2323
Set-VMKeystrokes -VMName $VM -StringInput "root" -DebugOn $true
24+
===========================================================================
25+
Modified by: David Rodriguez
26+
Organization: Sysadmintutorials
27+
Blog: www.sysadmintutorials.com
28+
Twitter: @systutorials
29+
===========================================================================
30+
.MODS
31+
Made $StringInput Optional
32+
Added a $SpecialKeyInput - See PARAMETER SpecialKeyInput below
33+
Added description to write-hosts [SCRIPTINPUT] OR [SPECIALKEYINPUT]
34+
.PARAMETER StringInput
35+
The string of single characters to send to the VM
36+
.PARAMETER SpecialKeyInput
37+
All Function Keys i.e. F1 - F12
38+
Keyboard TAB, ESC, BACKSPACE, ENTER
39+
Keyboard Up, Down, Left Right
40+
.EXAMPLE
41+
Set-VMKeystrokes -VMName $VM -SpecialKeyInput "F2"
42+
2443
#>
2544
param(
2645
[Parameter(Mandatory = $true)][String]$VMName,
27-
[Parameter(Mandatory = $true)][String]$StringInput,
46+
[Parameter(Mandatory = $false)][String]$StringInput,
47+
[Parameter(Mandatory = $false)][String]$SpecialKeyInput,
2848
[Parameter(Mandatory = $false)][Boolean]$ReturnCarriage,
2949
[Parameter(Mandatory = $false)][Boolean]$DebugOn
3050
)
3151

3252
# Map subset of USB HID keyboard scancodes
3353
# https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2
3454
$hidCharacterMap = @{
35-
"a" = "0x04";
36-
"b" = "0x05";
37-
"c" = "0x06";
38-
"d" = "0x07";
39-
"e" = "0x08";
40-
"f" = "0x09";
41-
"g" = "0x0a";
42-
"h" = "0x0b";
43-
"i" = "0x0c";
44-
"j" = "0x0d";
45-
"k" = "0x0e";
46-
"l" = "0x0f";
47-
"m" = "0x10";
48-
"n" = "0x11";
49-
"o" = "0x12";
50-
"p" = "0x13";
51-
"q" = "0x14";
52-
"r" = "0x15";
53-
"s" = "0x16";
54-
"t" = "0x17";
55-
"u" = "0x18";
56-
"v" = "0x19";
57-
"w" = "0x1a";
58-
"x" = "0x1b";
59-
"y" = "0x1c";
60-
"z" = "0x1d";
61-
"1" = "0x1e";
62-
"2" = "0x1f";
63-
"3" = "0x20";
64-
"4" = "0x21";
65-
"5" = "0x22";
66-
"6" = "0x23";
67-
"7" = "0x24";
68-
"8" = "0x25";
69-
"9" = "0x26";
70-
"0" = "0x27";
71-
"!" = "0x1e";
72-
"@" = "0x1f";
73-
"#" = "0x20";
74-
"$" = "0x21";
75-
"%" = "0x22";
76-
"^" = "0x23";
77-
"&" = "0x24";
78-
"*" = "0x25";
79-
"(" = "0x26";
80-
")" = "0x27";
81-
"_" = "0x2d";
82-
"+" = "0x2e";
83-
"{" = "0x2f";
84-
"}" = "0x30";
85-
"|" = "0x31";
86-
":" = "0x33";
87-
"`"" = "0x34";
88-
"~" = "0x35";
89-
"<" = "0x36";
90-
">" = "0x37";
91-
"?" = "0x38";
92-
"-" = "0x2d";
93-
"=" = "0x2e";
94-
"[" = "0x2f";
95-
"]" = "0x30";
96-
"\" = "0x31";
97-
"`;" = "0x33";
98-
"`'" = "0x34";
99-
"," = "0x36";
100-
"." = "0x37";
101-
"/" = "0x38";
102-
" " = "0x2c";
55+
"a" = "0x04";
56+
"b" = "0x05";
57+
"c" = "0x06";
58+
"d" = "0x07";
59+
"e" = "0x08";
60+
"f" = "0x09";
61+
"g" = "0x0a";
62+
"h" = "0x0b";
63+
"i" = "0x0c";
64+
"j" = "0x0d";
65+
"k" = "0x0e";
66+
"l" = "0x0f";
67+
"m" = "0x10";
68+
"n" = "0x11";
69+
"o" = "0x12";
70+
"p" = "0x13";
71+
"q" = "0x14";
72+
"r" = "0x15";
73+
"s" = "0x16";
74+
"t" = "0x17";
75+
"u" = "0x18";
76+
"v" = "0x19";
77+
"w" = "0x1a";
78+
"x" = "0x1b";
79+
"y" = "0x1c";
80+
"z" = "0x1d";
81+
"1" = "0x1e";
82+
"2" = "0x1f";
83+
"3" = "0x20";
84+
"4" = "0x21";
85+
"5" = "0x22";
86+
"6" = "0x23";
87+
"7" = "0x24";
88+
"8" = "0x25";
89+
"9" = "0x26";
90+
"0" = "0x27";
91+
"!" = "0x1e";
92+
"@" = "0x1f";
93+
"#" = "0x20";
94+
"$" = "0x21";
95+
"%" = "0x22";
96+
"^" = "0x23";
97+
"&" = "0x24";
98+
"*" = "0x25";
99+
"(" = "0x26";
100+
")" = "0x27";
101+
"_" = "0x2d";
102+
"+" = "0x2e";
103+
"{" = "0x2f";
104+
"}" = "0x30";
105+
"|" = "0x31";
106+
":" = "0x33";
107+
"`"" = "0x34";
108+
"~" = "0x35";
109+
"<" = "0x36";
110+
">" = "0x37";
111+
"?" = "0x38";
112+
"-" = "0x2d";
113+
"=" = "0x2e";
114+
"[" = "0x2f";
115+
"]" = "0x30";
116+
"\" = "0x31";
117+
"`;" = "0x33";
118+
"`'" = "0x34";
119+
"," = "0x36";
120+
"." = "0x37";
121+
"/" = "0x38";
122+
" " = "0x2c";
123+
"F1" = "0x3a";
124+
"F2" = "0x3b";
125+
"F3" = "0x3c";
126+
"F4" = "0x3d";
127+
"F5" = "0x3e";
128+
"F6" = "0x3f";
129+
"F7" = "0x40";
130+
"F8" = "0x41";
131+
"F9" = "0x42";
132+
"F10" = "0x43";
133+
"F11" = "0x44";
134+
"F12" = "0x45";
135+
"TAB" = "0x2b";
136+
"KeyUp" = "0x52";
137+
"KeyDown" = "0x51";
138+
"KeyLeft" = "0x50";
139+
"KeyRight" = "0x4f";
140+
"KeyESC" = "0x29";
141+
"KeyBackSpace" = "0x2a";
142+
"KeyEnter" = "0x28";
103143
}
104144

105145
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($VMName)$" }
@@ -110,34 +150,59 @@
110150
return
111151
}
112152

113-
$hidCodesEvents = @()
114-
foreach ($character in $StringInput.ToCharArray()) {
115-
# Check to see if we've mapped the character to HID code
116-
if ($hidCharacterMap.ContainsKey([string]$character)) {
117-
$hidCode = $hidCharacterMap[[string]$character]
153+
#Code for -StringInput
154+
if ($StringInput) {
155+
$hidCodesEvents = @()
156+
foreach ($character in $StringInput.ToCharArray()) {
157+
# Check to see if we've mapped the character to HID code
158+
if ($hidCharacterMap.ContainsKey([string]$character)) {
159+
$hidCode = $hidCharacterMap[[string]$character]
118160

119-
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
161+
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
162+
163+
# Add leftShift modifer for capital letters and/or special characters
164+
if ( ($character -cmatch "[A-Z]") -or ($character -match "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) {
165+
$modifer = New-Object Vmware.Vim.UsbScanCodeSpecModifierType
166+
$modifer.LeftShift = $true
167+
$tmp.Modifiers = $modifer
168+
}
120169

121-
# Add leftShift modifer for capital letters and/or special characters
122-
if ( ($character -cmatch "[A-Z]") -or ($character -match "[!|@|#|$|%|^|&|(|)|_|+|{|}|||:|~|<|>|?|*]") ) {
123-
$modifer = New-Object Vmware.Vim.UsbScanCodeSpecModifierType
124-
$modifer.LeftShift = $true
125-
$tmp.Modifiers = $modifer
170+
# Convert to expected HID code format
171+
$hidCodeHexToInt = [Convert]::ToInt64($hidCode, "16")
172+
$hidCodeValue = ($hidCodeHexToInt -shl 16) -bor 0007
173+
174+
$tmp.UsbHidCode = $hidCodeValue
175+
$hidCodesEvents += $tmp
176+
177+
if ($DebugOn) {
178+
Write-Host "[StringInput] Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
179+
}
180+
}
181+
else {
182+
Write-Host "[StringInput] The following character `"$character`" has not been mapped, you will need to manually process this character"
183+
break
126184
}
127185

128-
# Convert to expected HID code format
186+
}
187+
}
188+
189+
#Code for -SpecialKeyInput
190+
if ($SpecialKeyInput) {
191+
if ($hidCharacterMap.ContainsKey([string]$SpecialKeyInput)) {
192+
$hidCode = $hidCharacterMap[[string]$SpecialKeyInput]
193+
$tmp = New-Object VMware.Vim.UsbScanCodeSpecKeyEvent
129194
$hidCodeHexToInt = [Convert]::ToInt64($hidCode, "16")
130195
$hidCodeValue = ($hidCodeHexToInt -shl 16) -bor 0007
131196

132197
$tmp.UsbHidCode = $hidCodeValue
133198
$hidCodesEvents += $tmp
134199

135200
if ($DebugOn) {
136-
Write-Host "Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
201+
Write-Host "[SpecialKeyInput] Character: $character -> HIDCode: $hidCode -> HIDCodeValue: $hidCodeValue"
137202
}
138203
}
139204
else {
140-
Write-Host "The following character `"$character`" has not been mapped, you will need to manually process this character"
205+
Write-Host "[SpecialKeyInput] The following character `"$character`" has not been mapped, you will need to manually process this character"
141206
break
142207
}
143208
}
@@ -158,4 +223,4 @@
158223
$spec.KeyEvents = $hidCodesEvents
159224
Write-Host "Sending keystrokes to $VMName ...`n"
160225
$results = $vm.PutUsbScanCodes($spec)
161-
}
226+
}

0 commit comments

Comments
 (0)