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:
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.