-5

I'm doing a project using key stimulation and I'm using two ways to display a key character on the screen. First I use Clipboard but using Clipboard sometimes is inconvenient so I try to use SendInput() to display the key character when user press any button on the keyboard. I follow the code tutorial but I didn't know how does the SendInput() function work ( I mean when I invoke SendInput() what will it work step by step?). Does anyone have experience about my case? Please explain clearly for me. Thanks all!

private void SendUnicode(string _text)
    {
        if (IntPtr.Size == 4)
        {
            ViKey.INPUT[] input = new ViKey.INPUT[_text.Length * 2];
            for (int i = 0; i < _text.Length; i++)
            {
                input[i * 2].type = 1;
                input[i * 2].ki.wVk = 0;
                input[i * 2].ki.wScan = (short)_text[i];
                input[i * 2].ki.dwFlags = 4;
                input[i * 2 + 1] = input[i * 2];
                input[i * 2 + 1].ki.dwFlags = 6;
            }
            ViKey.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
            return;
        }
        else
        {
            ViKey.INPUT64[] inputx64 = new ViKey.INPUT64[_text.Length * 2];
            for (int i = 0; i < _text.Length; i++)
            {
                inputx64[i * 2].type = 1;
                inputx64[i * 2].ki.wVk = 0;
                inputx64[i * 2].ki.wScan = (short)_text[i];
                inputx64[i * 2].ki.dwFlags = 4;
                inputx64[i * 2 + 1] = inputx64[i * 2];
                inputx64[i * 2 + 1].ki.dwFlags = 6;
            }
            ViKey.SendInput((uint)inputx64.Length, inputx64, Marshal.SizeOf(inputx64[0]));
        }
    }
Justin Văn
  • 41
  • 1
  • 7

1 Answers1

0

All relevant information can be found in the documentation about the function.

The function returns the number of events that it successfully inserted into the keyboard or mouse input stream. If the function returns zero, the input was already blocked by another thread.

So how it works is that it inserts the events in the INPUT structures (which are fed to the function through the parameters) serially into the keyboard or mouse input stream and then returns the number of those events that are successfully inserted..

Community
  • 1
  • 1
Olivier Poulin
  • 1,778
  • 8
  • 15
  • Err. But I still didn't know how the button that I have pressed display on the screen. As you told that SendInput() return the number of events inserted..and sorry for forgot to post my code. Can you see my question again and my code? – Justin Văn Jun 15 '15 at 14:51
  • @JustinVăn sure ill look at it again, what errors are you getting? – Olivier Poulin Jun 15 '15 at 14:54
  • it worked perfectly, just because I followed the tutorial thay only show the code and I didn't know how the code work step by step. the _text is the string that we type on keyboard, for example: "abcde" so how SendInput display "abcde" on the screen? – Justin Văn Jun 15 '15 at 15:00
  • @JustinVăn how the sendInput function works is that it takes your input object (which is an INPUT structure) and inserts the event that it created in the if, or else statement into the keyboard input stream. `ki` represents the keyboard input event structure. – Olivier Poulin Jun 15 '15 at 15:05
  • [Here](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646271(v=vs.85).aspx) is an explanation of each part of the keyboard input structure. – Olivier Poulin Jun 15 '15 at 15:08
  • ok, I will think what you explain carefully. Thank you very much. – Justin Văn Jun 15 '15 at 15:10
  • @JustinVăn no problem, if you have any more questions feel free to ask here. – Olivier Poulin Jun 15 '15 at 15:11