SendInput not working in certain apps - Windows with Delphi

后端 未结 1 1566
梦毁少年i
梦毁少年i 2021-01-15 03:46

Using Delphi, I\'m trying to find a way of sending a string/series of characters or keystrokes to the active window. Using SendInput I have the following code:



        
相关标签:
1条回答
  • 2021-01-15 04:26

    You are sending key down events, but not corresponding key up events. You typically need two input events per key stroke. One for key down, dwFlags = KEYEVENTF_UNICODE and one for key up, dwFlags = KEYEVENTF_UNICODE or KEYEVENTF_KEYUP.

    You could code it along these lines:

    procedure SendKeys(const Text: string);
    var
      C: Char;
      Input: TInput;
      InputList: TList<TInput>;
    begin
      InputList := TList<TInput>.Create;
      try
        for C in Text do begin
          if C = #10 then continue;
          Input := Default(TInput);
          Input.Itype := INPUT_KEYBOARD;
          Input.ki.dwFlags := KEYEVENTF_UNICODE;
          Input.ki.wScan := ord(C);
          InputList.Add(Input);
          Input.ki.dwFlags := KEYEVENTF_UNICODE or KEYEVENTF_KEYUP;
          InputList.Add(Input);
        end;
        SendInput(InputList.Count, InputList.List[0], SizeOf(TInput));
      finally
        InputList.Free;
      end;
    end;
    

    Finally, it's quite likely that you'd be able to do this using UI Automation without having to resort to input faking.

    0 讨论(0)
提交回复
热议问题