1

My Problem is following procedure that shows this string 'a-bb-ccc-dddd' in outpot like this 'a-b-c-d'. How can I fix it?

procedure SendText(const Value: WideString);
var
  i: Integer;
  S: WideString;
  TI, TYY222: TInput;
  ki: TKeybdInput;
  MyWND: HWND;
const
  KEYEVENTF_UNICODE = $0004;
begin
  S := (Value);
  TI.Itype := INPUT_KEYBOARD;
  for i := 1 to Length(S) do
  begin
    ki.wVk := 0;
    ki.dwFlags := KEYEVENTF_UNICODE;
    ki.wScan := Ord(S[i]);
    TI.ki := ki;
    SendInput(1, TI, SizeOf(TI));
  end;
end;
Cœur
  • 37,241
  • 25
  • 195
  • 267
M.MARAMI
  • 95
  • 9

1 Answers1

4

It is invariably a mistake to call SendInput multiple times in a loop in that way. The whole point of SendInput is that it batches up a series of related input events, and sends them as an atomic group. This is stated quite explicitly in the documentation for SendInput and I recommend that you re-read that.

The first thing to change therefore is to use your loop to build an array of input events, and send that array in its entirety with a single call to SendInput, made after your loop completes.

Another problem is that your code currently fakes the key down events, but omits to fake the key up events. Each character that you type involves the key going down, and then coming back up. So, your array needs to be sized to contain twice as many items as characters in the string. And for each character you need to include both key down and key up. Include KEYEVENTF_KEYUP in dwFlags to indicate a key up event.

Yet another problem that I can see is that you are working with uninitialised variables. You set some but not all fields of ki. You need to make sure that the entire record is initialized.

There seems little reason for you to make a copy of the input string. You can work with the input string directly. There is nothing to be gained from making a copy of it.

Finally, did you consider using UI Automation instead of faking input?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thank you ,yes i used UI Automation. I find this sample [link](http://stackoverflow.com/questions/25823685/sendinput-not-working-in-certain-apps-windows-with-delphi) but it do'nt know why it dos'nt work for me – M.MARAMI Jan 03 '17 at 12:37
  • 2
    I find it a little astounding that you make the exact same mistake as made by the asker of that question, and that you ignore the code from the answer. – David Heffernan Jan 03 '17 at 12:39
  • It is because that before i ask this question tested your answer of that question. But i received multiple errors. Are you sure that your answer dos'nt have any mistake for that question? – M.MARAMI Jan 03 '17 at 12:48
  • Yes I am quite sure that the code there is correct. I find it astonishing that the code here is your attempt to "fix" it. How can you have failed to include both key down and key up if you read that answer and the code carefully. I suspect that you are making the common mistake of copying code blindly without attempting to understand. Does that often work for you? – David Heffernan Jan 03 '17 at 12:49
  • And once more, why did you reject UI Automation? – David Heffernan Jan 03 '17 at 12:50
  • Is that ready code for to use or i have to change or add something to it? I get this error Undeclared identififier 'TList' – M.MARAMI Jan 03 '17 at 13:03
  • I already answered that. The code there is correct. You probably have a very old version of Delphi with no generics. Use a dynamic array instead. But stop using code that you don't understand. That never works. Stop guessing. Stop using trial and error. Understand. – David Heffernan Jan 03 '17 at 13:04
  • Surry if i take your time .I tested in delphi XE7 and get this error `Undeclared identififier 'TList'` – M.MARAMI Jan 03 '17 at 13:16
  • It's defined in `System.Generics.Collections`. Stop using `WideString` also. Plain old `string` is UTF-16. I'm happy to help but I think you need to make more effort trying to understand. – David Heffernan Jan 03 '17 at 13:18
  • sorry and very sorry. Know I understand it works perfect. I was so stupid these days. Thank you – M.MARAMI Jan 03 '17 at 13:26
  • It's fine. No need to be sorry. But I hope you heed my advice and seek to understand. – David Heffernan Jan 03 '17 at 13:29
  • Yeah, sure, sure I will do your advice . – M.MARAMI Jan 03 '17 at 13:39