I am finding that VBscript's SendKeys
does not support Unicode. It supports some like A-65, but not foreign letters like the letter Aleph (א) from the Hebrew alphabet. Prob outside its supported range. Could be for decimal values of 128+, it gives a "?", and it only supports the ASCII range.
I can type and see Hebrew letters on my computer using Windows XP. So the OS support for the characters is there and set up. My source code demonstrates that, since the line
msgbox Chrw(1488)
displays the Aleph character and I've displayed it in Notepad and MS Word.
It looks to me like it is sending a question mark for a character it doesn't recognize. I think MS Word or Notepad if they did have a problem displaying a character (e.g. when the font doesn't support a char), they would display a box rather than a question mark. Certainly in the case of Notepad anyway. So it looks like a SendKeys
issue. Any ideas? Any kind of workaround?
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad" ''#can change to winword
Wscript.Sleep 2000
msgbox Chrw(1488) ''#aleph
objShell.SendKeys ("abc" & ChrW(1488) & "abc") ''#bang, it displays a ? instead of an aleph
WScript.Quit
You're most likely right in your guess that VBscript's SendKeys
doesn't support Unicode.
Monitoring of Windows API function calls performed by SendKeys
using Blade API Monitor on Russian Windows XP with English US, Russian and Hebrew keyboards) shows that SendKeys
isn't Unicode aware. Specifically, SendKeys
does the following:
Calls the ANSI (not Unicode) version of the VkKeyScan
function — VkKeyScanA
— to get the virtual key code of the character to be sent. This function translates the character into VK_SHIFT
+ VK_OEM_2
, so it seems that somewhere before or in the process the Aleph character is converted into a different, ANSI character.
Calls the SendInput
function to send the VK_SHIFT
+ VK_OEM_2
keystrokes instead of the Aleph character.
The main problem here is that to send a Unicode character, SendInput
must be called with the KEYEVENTF_UNICODE
flag and the character in question must be passed via the function parameters — the experiment shows that none of this is the case. Also, VkKeyScan
isn't actually needed in case of a Unicode character, as SendInput
itself handles Unicode input.
Given this, the only way to send Unicode input to an application from VBScript is to write a custom utility or COM component that will utilize SendInput
properly and to call this utility/component from your script. (VBScript doesn't have any native means to access Windows API.)
Note added by barlop: While VBScript's obj.SendKeys(..)
isn't Unicode-aware, VB's SendKeys.Send(..)
would be.