1

My ultimate goal is to setup a shortcut of some kind (probably keyboard shortcut) that will type a particular special character: è. I have to type a variety of special characters like this and would like to set something up where I can do a simple keyboard combo rather then consulting a list of Alt Codes every time.

My first thought was to create a script the bind it's execution to a keyboard shortcut. I tried writing a PowerShell script using SendKeys but I wasn't able to find a way to get SendKeys to send Alt Codes or anything not already on my American keyboard. I can also use SendKeys in Batch, but had the same problem. Running this on the CmdLine comes close, but just prints the numbers (likely because it doesn't differentiate between number keys and the numpad keys):

powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('%(0232)')

I was thinking there would be a straight forward way to do this but maybe not. Are there methods in other languages that I haven't found? Alternatives to SendKeys? Open to most anything.

user912447
  • 696
  • 1
  • 11
  • 31
  • 2
    If you're running `powershell` from a batch file, you'll need to escape the special `%` character. To do that you need to double it, i.e. `%%`. – Compo Aug 19 '22 at 16:09
  • @Compo The % is a special code to send the ALT key, I don't believe I want to escape it. I was trying to send ALT plus the number code in order to print è. – user912447 Aug 19 '22 at 16:35
  • For entering in a Windows command prompt window an [OEM](https://en.wikipedia.org/wiki/Original_equipment_manufacturer) encoded `è` using the [code page](https://en.wikipedia.org/wiki/Code_page) as defined by the region/country configured for the current user account there must be pressed just __SHIFT+`__ and next __E__ on the keyboard at least on a German keyboard. – Mofi Aug 19 '22 at 17:15
  • 2
    @user912447, whenever you use a batch file, any literal `%` character must be escaped. You might not believe it, but it is true! the literal character is entered by PowerShell, but to get there the batch file has to send it to powershell.exe first. – Compo Aug 19 '22 at 17:26
  • @Compo I see, thanks. Like I mentioned in the question I simply ran that on Command Line. Hoping to get it working there first, but I don't believe it can. – user912447 Aug 19 '22 at 18:49
  • 1
    @user912447, if you were running it on the command line, i.e. Windows Command Prompt, you should have used the [[tag:cmd]] tag, and perhaps even the [[tag:command-prompt]] tag, but definitely not the [[tag:batch-file]] tag. So do you see how easily a question can be misunderstood, and peoples' time and effort wasted, because of a simple lack of care and attention when submitting your question. Please try to be more careful in future, so as to negate such things! – Compo Aug 19 '22 at 19:37
  • @Compo My question is about a batch file, or some other script to accomplish this. In my question I stated that the example I posted was run on the Command Line, for testing. If you take care and attention while reading my question I'm sure that you won't misunderstand it. – user912447 Aug 23 '22 at 14:55
  • @user912447, you posted code you were running directly in cmd.exe for the purpose of your question, but used a [[tag:batch-file]] tag. That is not just my personal opinion you have admitted as much. Please do not complain about responses from persons following a specific tag, when you have used it improperly. Also, and this is just as important, my initial response was to inform you that the code you were using directly in the Command Prompt window needed modification when run from a batch file, and even provided that modification, so please don't tell me that I've failed to carefully read it. – Compo Aug 23 '22 at 15:30

1 Answers1

1

There is a suboptimal way to get this to work (see below), but on a general note:

  • A PowerShell CLI call takes time, because the startup cost is significant, so a solution based on it may make for an unsatisfying end-user experience.

  • Utilities such as AutoHotkey offer better performance and functionality with respect to keystroke emulation.


IF it is acceptable to overwrite the clipboard so as to store the desired characters, you can use the following:

powershell -noprofile -c "Set-Clipboard é; $wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^v')"
  • PowerShell, as a .NET-based language can represent any Unicode character and copy it as such to the clipboard with Set-Clipboard.

  • Using .SendKeys() to send '^v', i.e. Ctrl-V then pastes the characters from the clipboard.

Note: While you could make an effort to save and restore the clipboard state, doing so would only work in simple cases - see this answer for background information.


As for what you tried:

Leaving the potential issue with needing to escape a literal % as %% in a batch file aside (as opposed to directly from a cmd.exe command prompt):

Sending string '%(0232)' (i.e., holding down Alt while typing 0232) does not work, because the digits (which make up the decimal Unicode code point of your special character, è (LATIN SMALL LETTER E WITH GRAVE, U+00E8); 0xe8 == 232) would have to be typed on the numeric keypad, which .SendKeys() doesn't support, as far as I'm aware.[1]


[1] While you can send certain virtual key codes via .SendKeys(), they seem to be restricted to those in the 8-bit range (>= 128 - see this answer for an example), whereas the numeric keypad digit key codes are in the 7-bit range - see the list of virtual key codes.

mklement0
  • 382,024
  • 64
  • 607
  • 775