5

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
Helen
  • 87,344
  • 17
  • 243
  • 314
barlop
  • 12,887
  • 8
  • 80
  • 109
  • What keyboard layouts do you have installed and what's your **Languange for non-Unicode programs** setting in Regional and Language Options? – Helen Jul 07 '10 at 22:08
  • 1
    As far as languages and layouts. Language English UK with Layout English UK. Language English US with layout English US. Language hebrew with layout hebrew. As far as "language for non-unicode programs", English US. I tried Hebrew. Either way, msgbox displays the character-Aleph for msgbox chrw(1488). (it always has) But When that setting "language for non-unicode programs"(which requires a restart),is set to US,notepad displays ? for sendkeys chrw(1488). When set to Hebrew,notepad displays nothing for sendkeys chrw(1488).So still not displaying the character for sendkeys chrw(1488). – barlop Jul 08 '10 at 00:08
  • I've edited your post to improve the formatting, hope you don't mind. If you do mind, feel free to edit back. – Helen Jul 08 '10 at 08:35
  • The only advice I can give at the moment is to drop VBScript and rewrite the program in C/C++ using `SendInput`. – Philipp Jul 08 '10 at 08:52
  • An additional note regarding the question. In order that people can reproduce the problem(or a strongly connected one). One can change the aleph to pi. So, changing ChrW(1488) to ChrW(960). Here, Msgbox displays the symbol. sendkeys displays a P rather than the pi symbol. – barlop Jul 08 '10 at 18:04
  • a workaround would be to install visual studio! that's the visual basic of today. That has a MessageBox(C#) or msgbox(VB) that supports unicode. – barlop Apr 26 '16 at 09:27
  • note oddly enough, it may be that when specifying a char from another language in unicode e.g. Sendkeys.Send('\u05b0') (must check for 05d0, the visual studio sendkeys only seems to write/output in other languages when the language is set to english! – barlop May 23 '16 at 09:38

4 Answers4

6

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:

  1. 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.

  2. 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.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Nice research..interesting.. I've seen some of that kind of thing in Visual Basic years ago (VK_SHIFT), the constants for various keys. Is that and access to the windows API available to the programmer in vbscript too? I don't seem to have a book that goes into it. I did find this link, somebody claiming to have written something to solve what looks like the equivalent problem, in visual basic http://vb.mvps.org/samples/SendInput/ I don't know if it's transferable to vbscript. – barlop Jul 09 '10 at 21:16
  • 1
    @barlop: No, VBScript doesn't have access to Windows API. However, you can create a custom utility that will utilize the necessary WinAPI functions and call this utility from VBScript code. – Helen Jul 12 '10 at 13:32
3

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.

Miki Watts
  • 1,746
  • 1
  • 17
  • 27
  • 1
    Interesting.. and re "That would override the users clipboard", you could save the user's clipboard beforehand and do your two lines `Clipboard("טקסט בעברית")` and `SendKeys("^V")` and put it whatever was in the clipboard, back in the clipboard. So as to not override/lose what is in the user's clipboard. – barlop Jan 13 '18 at 07:47
  • 1
    You're right, I'm doing it for myself so I didn't care about the clipboard, but saving and restoring the clipboard is indeed better (though there is an issue when the clipboard content is not pure text) – Miki Watts Jan 14 '18 at 08:24
0

You can get around this by using:

wshShell.SendKeys "1488+({LEFT}{LEFT}{LEFT}{LEFT})%(x)"

This uses a windows keyboard shortcut from here.

-2

Try setting the font to Microsoft Sans Serif in Notepad.

Trefex
  • 2,290
  • 16
  • 18
  • 2
    No, see the original question: ”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.” – Philipp Jul 08 '10 at 08:51
  • 1
    right philipp. Also, arial, times new roman, or whatever font i'm using, certainly supports the aleph character.. and so naturally, changing to the ms sans serif font (another font that supports it) doesn't make it work. – barlop Jul 08 '10 at 09:09
  • Control Panel > Regional and Language Options > Advanced read: "This system setting enables non-Unicode programs to displays menus and dialogs in their native language. It does not affect Unicode programs, but it does apply to all users of this computer. Select a language to match the language version of the non-Unicode program you want to use." In the combo, select one of the Arabic language options. Reboot. What about this ? – Trefex Jul 08 '10 at 10:56
  • 1
    Trefix, I tried that, and I replied to helen's comment with what happened. It's an easy problem for you to reproduce and then you'd know it doesn't work! If you tried the code you'd see it won't work, and if by a change of language setting it suddenly worked, then you'd have the solution! But you don't. You'd see your suggestions aren't working if you tried them on your machine! but anyhow, as I said, Helen already inspired me then to try what you suggest now, and I had already replied with the results..before you suggested it, it didn't work. – barlop Jul 08 '10 at 11:33
  • Mhhh one of my comments are lost in translation apparently. I tried the code and I get a '?' both in the MsgBox and in Notepad. So I can't reproduce it at all. Now stop picking on me. I'm trying to help you out on a problem I don't have and won't require. I don't have to waste my time bothering to finding some way to make it work. I'm just trying to be nice. – Trefex Jul 08 '10 at 13:03
  • Trefex, i'm not picking on you, this is a technical discussion, so we both discuss things technically. You can criticise me technically, I can criticise your suggestions or ideas, technically. That is not a bad thing. And if you have big ears, i'm not bringing it up, that might be picking on you. – barlop Jul 08 '10 at 15:44
  • How dare you bring that up!! Well if I'd have the aleph char in the MsgBox I could actually try something. But for me I get ? no matter what I do. Sorry. – Trefex Jul 08 '10 at 15:54
  • Trefex. No need to apologize, you haven't done something bad. This is just a technical discussion. I'm not paying you to know something. – barlop Jul 08 '10 at 17:34