Does or Can VBscript's SendKeys support Unicode?
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开发者_如何学C 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 API Monitor and 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 intoVK_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 theVK_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.
I'm using Dragon Naturally Speaking with hebrew, and SendKeys indeed cannot send hebrew characters even though they show in the macro editor, however, what I did was set the clipboard with the hebrew text that I wanted, and then SendKeys with Ctrl-V for paste, and that does works, it's just SendKeys that messes with the encoding.
Clipboard("טקסט בעברית")
SendKeys("^V")
That would override the users clipboard and won't work for mixing command characters (Ctrl,Alt,Shift) with hebrew characters, but it's a work around.
You can get around this by using:
wshShell.SendKeys "1488+({LEFT}{LEFT}{LEFT}{LEFT})%(x)"
This uses a windows keyboard shortcut from here.
Try setting the font to Microsoft Sans Serif
in Notepad.
精彩评论