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 ! :)
The WM_CHAR message contains the character code of the key that was pressed.
A window receives keyboard input in the form of keystroke messages and character messages.
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.
Code demontrating how to simulate keyboard for unicode. Beware:
#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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With