1

I have app that sends character input into another app. Everything goes fine, but if text contain repeating characters (for example "app") then only one letter 'p' will arrive into destination window.

When sending "app" i receive "ap". After searching the web i've found that this is because missing KEYUP event. But i definitely have one in the code below. What's wrong?

void SendText(string text)
{
    int len = text.Length;
    int inputsSize = len * 2;

    INPUT[] inputs = new INPUT[inputsSize];

    for (int i = 0; i < len; i++)
    {
        INPUT inp = new INPUT();
        inp.type = 1; //INPUT_KEYBOARD;
        inp.U.ki.dwFlags = KEYEVENTF.UNICODE;
        inp.U.ki.time = 0;
        inp.U.ki.wVk = 0;
        inp.U.ki.wScan = (short)text[i];
        inp.U.ki.dwExtraInfo = GetMessageExtraInfo();

        inputs[i] = inp;

        INPUT inpUp = inp;

        inpUp.U.ki.dwFlags = KEYEVENTF.UNICODE | KEYEVENTF.KEYUP;

        inputs[i + 1] = inpUp;

    }

    SendInput((uint)inputsSize, inputs, INPUT.Size);
}
4

1 に答える 1

0

うーん..私は自分で決定を見つけました。しかし、最初のコードが機能しなかった理由はまだわかりません。これが実際の例です。ご覧のとおり、各キーイベントは、1 つのキーイベント パッケージではなく、個別の SendInput によって送信されます。必要に応じて動作します。

void SendText(string text)
{
    int len = text.Length;
    int inputsSize = len * 2;

    //INPUT[] inputs = new INPUT[inputsSize];
    INPUT[] inputs = new INPUT[1];

    for (int i = 0; i < len; i++)
    {
        INPUT inp = new INPUT();
        inp.type = 1; //INPUT_KEYBOARD;
        inp.U.ki.dwFlags = KEYEVENTF.UNICODE;
        inp.U.ki.time = 0;
        inp.U.ki.wVk = 0;
        inp.U.ki.wScan = (short)text[i];
        inp.U.ki.dwExtraInfo = GetMessageExtraInfo();

        //inputs[i] = inp;
        inputs[0] = inp;
        SendInput(1, inputs, INPUT.Size);

        inp = new INPUT();
        inp.type = 1; //INPUT_KEYBOARD;
        inp.U.ki.dwFlags = KEYEVENTF.KEYUP | KEYEVENTF.UNICODE;
        inp.U.ki.time = 0;
        inp.U.ki.wVk = 0;
        inp.U.ki.wScan = (short)text[i];
        inp.U.ki.dwExtraInfo = GetMessageExtraInfo();

        //inputs[i + 1] = inp;
        inputs[0] = inp;
        SendInput(1, inputs, INPUT.Size);
    }

    //SendInput((uint)inputsSize, inputs, INPUT.Size);
}
于 2013-02-08T06:34:50.477 に答える