0

Answers have been given on using powershell to control Windows settings (such as speaker volume control) using the following method:

$obj = new-object -com wscript.shell 
$obj.SendKeys([char]173)

However I cannot find any information on where those numbers (e.g. 173 above, or 174/175 in the link) are coming from how they are defined and what other number definitions are & can do.

Can you direct me to a listing of where those numbers are given as defined actions? For example how would I know what 172 would do? or 200? etc?

Thanks, Lawrence

Lawrence
  • 43
  • 5
  • 1
    173 is hyphen `-`. Just type `[char]number` in PowerShell terminal – Nico Nekoru Jul 14 '20 at 16:45
  • 2
    @NekoMusume, Note that the OP isn't interested in what `[char] 173` means _on the PowerShell side_, but in the special meaning these characters have as Windows _virtual-key codes_ (`173` == `0xAD` == `VK_VOLUME_MUTE`) when interpreted by the `.SendKeys()` method. – mklement0 Jul 14 '20 at 19:10

1 Answers1

4

Even though technically, from PowerShell's perspective, you're passing a specific Unicode character - with code point 173 == 0xAD == SOFT HYPHEN, U+00AD - .SendKeys() seemingly interprets that character's code point (the underlying number) as a Windows virtual-key code, representing a key on the keyboard, namely the VK_VOLUME_MUTE key (0xAD) in this case.

The list of all virtual-key codes is here, though I'm unclear what subset of them are actually usable with .SendKeys().

To recap the ones from the linked questions:

  • [char] 173 is VK_VOLUME_MUTE(0xAD)
  • [char] 174 is VK_VOLUME_DOWN(0xAE)
  • [char] 175 is VK_VOLUME_UP(0xAF)

As an aside: In Windows PowerShell (but no longer in PowerShell [Core] v6+) it seems that an additional cast to [string] is necessary; e.g. (mutes / unmutes the volume):

# Extra [string] cast is *not* necessary anymore in PowerShell [Core], v6+
(New-Object -ComObject Wscript.Shell).SendKeys([string] [char] 173)
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Great answer. For anyone trying to make this work with the next keys in the series, (VK_MEDIA_NEXT_TRACK, VK_MEDIA_PREV_TRACK) it likely won't work. See here for an example to try: https://stackoverflow.com/a/21504831/162215 – Iain Nov 04 '22 at 07:52