Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emitting keyboard input using WM_CHAR message?

I'm working on a virtual keyboard for windows. I know i can emit keyboard events by using (for example) keybd_event() with the right virtual key code, but this method is totally unpractical, and doesn't allow me to output (for example) chinese or russian characters, or at least not easily.

Is it possible, on windows, to simulate a keyboard event by posting a WM_CHAR message ? That would be perfect if i could do it because i would simply have to retrieve the char code from a UTF-8 or UTF-16 encoded configuration file, and post a message.

If it is possible, how can i do it on Windows CE and Windows Mobile ? I need to support both desktop and mobile devices.

Thanks for your help ! :)

like image 605
Virus721 Avatar asked Sep 14 '13 10:09

Virus721


People also ask

What happens when any keyboard key is pressed about Wm_char message?

The WM_CHAR message contains the character code of the key that was pressed.

What type of message is generated by keyboard?

A window receives keyboard input in the form of keystroke messages and character messages.

Which of the following parameter contains more information about the keystroke that generated the message?

The lParam parameter of a keystroke message contains additional information about the keystroke that generated the message. This information includes the repeat count, the scan code, the extended-key flag, the context code, the previous key-state flag, and the transition-state flag.


1 Answers

Code demontrating how to simulate keyboard for unicode. Beware:

  1. you must activate the target application, since keyboard events are queued to the active app...
  2. the target application must be unicode aware
#include <Windows.h>
#include <tchar.h>

static void send_unicode( wchar_t ch ) {
    INPUT input;

    // init
    input.type = INPUT_KEYBOARD;
    input.ki.time = 0;
    input.ki.dwExtraInfo = 0;
    input.ki.wVk = 0;
    input.ki.dwFlags = KEYEVENTF_UNICODE;
    input.ki.wScan = ch;

    // down
    SendInput( 1, &input, sizeof( INPUT ) );

    // up
    input.ki.dwFlags |= KEYEVENTF_KEYUP;
    SendInput( 1, &input, sizeof( INPUT ) );
}

int _tmain(int argc, _TCHAR* argv[])
{
    _tprintf( TEXT( "you have 5 seconds to switch to an application which accepts unicode input (as Word)...\n" ) );
    Sleep( 5000);
    _tprintf( TEXT( "Sending...\n" ) );
    send_unicode( 946 );   // lowercase greek beta
    send_unicode( 269 );   // lowercase c-hachek
    send_unicode( 12449 ); // japanese katakana small a
    send_unicode( 4595 );  // korean hangul jongseong phieuph-pieup

    return 0;

}

like image 153
manuell Avatar answered Sep 28 '22 18:09

manuell