0

My understanding is that TranslateMessage collates a sequence of key events and adds a WM_CHAR message to the queue if it results in a character. So on my computer what happens when I press Alt+1234 (6 relevant events/messages, including releasing the Alt) is the single character "Ê" comes out (in certain places).

Let's say I have a sequence of virtual key codes and related keypress data generated from the LL keyboard hook. Is there some way of using the Windows OS logic to translate this sequence into a real character? For example, could I construct contrived MSG structures, call TranslateMessage on them and then catch the WM_CHAR ensuing events? That seems very outside of Windows' expectations; haven't tried it yet but it seems like it could cause all kinds of subtle problems.

The cleanest solution I can think of so far is just to re-implement the logic myself to figure out the characters from the virtual codes. This is unfortunate of course since Windows internals already seem to know how to do this! Is there a better way?

I am aware of the existence of MapVirtualKeyA but this does not seem to handle a sequence of virtual key codes.

I am also aware that it is possible to do a hook on all GetMessage calls which could be used just to grab the WM_CHAR messages from every process. However this seems an extremely heavy solution: I need to make a separate DLL unlike for the WH_KEYBOARD_LL hook and then use some sort of IPC to send the characters back to my host process. Also MSDN explicitly says that you should avoid doing global hooks this for anything outside debugging and I need this to work on production machines.

I am also also aware of KeysConverter in .NET (I am fine to use .NET for this) but again this does not seem to deal with sequences of virtual keys like given above.

Patrick
  • 677
  • 5
  • 14
  • 2
    Converting keypresses to characters is a very complicated process. You haven't even begun to explore the complexity of input methods (IME), where on a Chinese keyboard you type "hao3" and out comes "好". – Raymond Chen May 18 '22 at 14:41
  • The low-level hooks are exactly that, low level. Windows has not even decided which window the input is going to yet. Why do you need to generate these messages based on input when Windows already does it for you? – Anders May 18 '22 at 19:11
  • @Anders I am trying to capture these messages globally, including foreign processes, hence the low-level hook. So Windows does not do it for me. – Patrick May 19 '22 at 01:54

0 Answers0