简体   繁体   中英

How to simulate entering a special character in Windows

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.

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' , ie 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)' (ie, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM