Using SendInput to type unicode characters

I received a query from reader Partha D about generating unicode keystrokes using the SendInput function in Windows. As I understand it, Partha wants to generate one or more unicode keystrokes when a particular keyboard shortcut is pressed. The following example program illustrates the use of the SendInput function to generate keyboard events for unicode characters. It just generates a single keystroke (a Bengali character) after a 3-second delay.

//
// unicodeinput.c - sends a unicode character to whichever
// window is in focus after a delay of 3 seconds (to allow
// time to switch to e.g. Notepad from the command window).
//
// Written by Ted Burke - last updated 2-10-2014
//
// To compile with MinGW:
//
//      gcc unicodeinput.c -o unicodeinput.exe
//
// To run the program:
//
//      unicodeinput.exe
//

// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
#include <windows.h>

int main()
{
    // Create a keyboard event structure
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // 3 second delay to switch to another window
    Sleep(3000);

    // Press a unicode "key"
    ip.ki.dwFlags = KEYEVENTF_UNICODE;
    ip.ki.wVk = 0;
    ip.ki.wScan = 0x09A4; // This is a Bengali unicode character
    SendInput(1, &ip, sizeof(INPUT));

    // Release key
    ip.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    // The End
    return 0;
}

To test the program, I opened a command window and a Notepad window. I compiled and ran the program in the command window, as shown below:

Compiling and running my unicode input example program in a command window

Then I immediately switched the focus to a Notepad window. After three seconds, the following unicode character was typed (I’ve increased the font size to the maximum for clarity):

Screenshot of a Notepad window in which a unicode character has been "typed" by my unicodeinput example program

This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Using SendInput to type unicode characters

  1. Partha D says:

    Generating unicode character is not the problem. The problem is I want to generate a unicode character for key combinations like Alt+k or Control+k. I need to generate Unicode characters for key combinations like Alt+a, alt+b….. upto Alt+z. Can you modify the example given in https://batchloaf.wordpress.com/2014/10/02/using-sendinput-to-type-unicode-characters/ to work with Alt+K to generate ‘খ’

    • batchloaf says:

      Hi Partha,

      What I was suggesting is to set up a normal Windows keyboard shortcut to run a program (like my example program) that generates a single unicode character. Have you tried that?

      When I say “set up a normal Windows keyboard shortcut”, I mean like this:
      http://windows.microsoft.com/en-us/windows/create-keyboard-shortcuts-open-programs#1TC=windows-7

      If that can’t do what you want, then maybe you will need to hook the “Alt+key” combination within the program, which I don’t know how to do off the top of my head. Didn’t you say that you already worked out how to do the hook? If you can provide your code for that part, I might try combining it with the SendInput code to see if it works.

      Ted

  2. Kevin Connor Arpe says:

    This is a criminally underrated post! You have (accidentally?) solved the “secret” of multiple keystrokes of the same key. Example: If you wan to send “aaa”, then SendInput(3, …) will not work. It is better to send inputs one-by-one. Great blog!

  3. Kevin Connor Arpe says:

    Your blog post appears here: https://stackoverflow.com/a/71384213/257299

Leave a reply to batchloaf Cancel reply