2

I'm trying to create a virtual keyboard, which simulates keyboard using the SendInput method like this:

        public static void SendKeyDown(System.Windows.Forms.Keys key)
        {
            INPUT k = new INPUT();
            k.type = (int)InputType.INPUT_KEYBOARD;
            k.ki.wVk = (short)key;
            k.ki.dwFlags = (int)KEYEVENTF.KEYDOWN;
            k.ki.dwExtraInfo = GetMessageExtraInfo();

            SendInput(1, new INPUT[] { k }, Marshal.SizeOf(k));
        }

But I cannot find the scandinavian letters Ä,Ö and Å from the Keys -enumeration. How can I send these letters using the SendInput method?

svick
  • 236,525
  • 50
  • 385
  • 514
Jaska
  • 1,412
  • 1
  • 18
  • 39
  • 2
    You're a computer programmer: *write a program* to answer your question. Write a form that handles the KeyDown event and displays the KeyEventArgs.KeyCode property, press the key, see what happens. – Eric Lippert Mar 15 '13 at 22:00
  • Yeah! Why didn't I thought of that!?! Thanks! :) – Jaska Mar 15 '13 at 22:02
  • And the right answer was: Oem3 = ö, Oem7 = ä, Oem6 = å – Jaska Mar 15 '13 at 22:04
  • 2
    Now, remember, those codes are only valid if your customer has the same keyboard as you. "OEM" means "Original Equipment Manufacturer"; the maker of the keyboard gets to decide what the key bindings are. – Eric Lippert Mar 15 '13 at 22:57

2 Answers2

4

You can send Unicode characters using KEYEVENTF_UNICODE.

k.type = (int)InputType.INPUT_KEYBOARD;
k.ki.wScan = 'ö';
k.ki.wVk = 0;
k.ki.dwFlags = (int)KEYEVENTF.UNICODE | (int)KEYEVENTF.KEYDOWN;
k.ki.dwExtraInfo = GetMessageExtraInfo();

This is more portable than your solution of using Oem3 et al, whose assigned character would vary according to the culture of the platform on which your application is executing.

(Rest of P/Invoke signatures can be found in my other answer.)

Community
  • 1
  • 1
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • 1
    Just note that when using `KEYEVENTF_UNICODE`, `wScan` is a UTF-16 codeunit. If you want to send a Unicode codepoint that requires a UTF-16 surrogate pair (`ö` does not), you have to send two events, one for each codeunit of the pair. – Remy Lebeau Jul 28 '16 at 00:50
0

Found solution myself:

Oem3 = ö, Oem7 = ä, Oem6 = å

Jaska
  • 1,412
  • 1
  • 18
  • 39