0

I'm writing an application that performs keyboard hook for Internet Explorer and for other apps. Everything works great except one annoying problem.

In Internet explorer, I change the active language and write in text box of HTML document, say to Hebrew or Arabic, but I keep getting the characters in English in my hooking app.

In the Internet explorer HTML document characters are displayed in the correct language but in the app I keep getting only English.

for example:

When I press"abcאבג" in Internet Explorer, and I get "abctcd" in the hooking app.

When I tried to hook other programs like Word or Excel or Firefox or Chrome, everything worked as expected.

Eikey
  • 31
  • 5
  • 1
    Which kind of keyboard hook are you using? Most keyboard hooks (including `WH_KEYBOARD_LL`) work at the level of keys, not characters, and there is no trivial relation between those. So how do you obtain characters in those other applications? – CodesInChaos Apr 07 '13 at 08:11
  • I revised. What do you mean by **click** of "abcאבג"? Since you told that you are hooking **keyboard**. – Ken Kin Apr 07 '13 at 08:12
  • CodesInChaos - im using WM_KEYDOWN. – Eikey Apr 07 '13 at 08:18
  • Ken Kin - what i mean is i clicked 6 times, 3 times in english, 3 times in other language. in IE i get it right but in my app i get only english/ – Eikey Apr 07 '13 at 08:19
  • @ErezKeller: Well, and that may be said ***press***. – Ken Kin Apr 07 '13 at 08:24
  • How do you get a non ASCII character from WM_KEYDOWN? I can't find any field related to that. Please post a bit more about how your code works. – CodesInChaos Apr 07 '13 at 08:53

2 Answers2

1

I recently answer an question with some code for example:

However, that answer is not directly corresponding to yours, just for some sample code.

Something you need to be aware is that your hooking program might have no idea of the current IME unlike Work or Excel does. So you would first need to know that the user switched the IME, and specifying a corresponding keyboard layout. The API ActivateKeyboardLayout may do this.

After you knew the keyboard layout, you may get the corresponding character in your hooking program by invoking ToUnicodeEx or ToAsciiEx with the the HKL that the ActivateKeyboardLayout returns you which indicates the specific keyboard layout.

For more information of the overview, see About Keyboard Input on MSDN.

Community
  • 1
  • 1
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
  • Luckily, from the comment about `WM_KEYDOWN`, it sounds like he's using a `WH_KEYBOARD` hook, as opposed to a `WH_KEYBOARD_LL` hook. The critical difference is that the former type of hook is injected directly into the hooked process, which means it has access to the process's keyboard state. This allows him to use `ToUnicodeEx` (best to just forget that `ToAsciiEx` even exists anymore) reliably, just as you suggested. – Cody Gray - on strike Apr 07 '13 at 10:51
  • @CodyGray: Yep, thank you to mention that. And I think for most context-sensitive hooking, `WH_KEYBOARD_LL` appears not very useful, though it's quite simpler to implement. – Ken Kin Apr 07 '13 at 11:49
  • 1
    Yup. The reason you see low-level hooks implemented all over the place is they're the only thing you can create in managed code like C#. You need to create a *separate, native DLL* for `WH_KEYBOARD`, and there are all these C# programmers that inexplicably think they need global hooks to solve a problem. – Cody Gray - on strike Apr 07 '13 at 11:56
0

I suspect that the problem is deeper.

I wrote the following code:

IntPtr handle = new IntPtr(-1);
handle = GetForegroundWindow();
uint tpid = GetWindowThreadProcessId(handle, IntPtr.Zero);
IntPtr hKL = GetKeyboardLayout(tpid);
hKL = (IntPtr)(hKL.ToInt32() & 0x0000FFFF);
lbl1.Content = hKL.ToString();

The problem occurs only in IE (I check with version 7, 8, 9 and 10) and only in HTML TextBox control.

At the url everything works as expected (as in all other applications such as word excel firefox, chrome etc), if I write in Hebrew is showing me 1037 and shows the right character in Hebrew, If I write in English it shows me 1033 and shows the right character in English, if I start to write in Hebrew, passes English is still continuing to display the correct value of the hkl and the correct characters.

Which is fine so far.

The problem occurs only when just when I typing in the HTML text box Only then it does not recognize the correct language.

when I type in the text box of the HTML he recognize the correct language and correct hkl value of the first character, when I change the language he did not recognize the change and continues send me the character in which language the first character was typed.

Eikey
  • 31
  • 5