1

I have a string variable. Now i want to store a string value into a control in another application without using the clipboard. I wanna do it manually.

I think i should use SendMessage(WM_SETTEXT). Which way do you suggest (with an example please)?

Warren P
  • 65,725
  • 40
  • 181
  • 316
Kermia
  • 4,171
  • 13
  • 64
  • 105
  • 1
    Depends on the other application. Which application is it? Which control in that application are you targeting? – David Heffernan Sep 17 '11 at 11:40
  • 1
    OK, so you do not actually want to *paste* anything in the strict sense, that is, using the clipboard? – Andreas Rejbrand Sep 17 '11 at 11:40
  • First and foremost, you need the window handle of the target edit control. – Andreas Rejbrand Sep 17 '11 at 11:41
  • @David I don't know. It can be any standard windows application. – Kermia Sep 17 '11 at 11:42
  • 1
    Every app could have a different solution. Each control in that app could need different treatment. – David Heffernan Sep 17 '11 at 11:43
  • 1
    @Andreas A great many apps don't use windowed edit controls for their text boxes – David Heffernan Sep 17 '11 at 11:44
  • @David Despite having different class names, When you press Ctrl+V , Ms Windows will paste strings anywhere you want. how is it? – Kermia Sep 17 '11 at 11:56
  • 4
    @Kermia No, the way it works is this. You press CTRL+V and the system synthesise keyboard input messages which are placed in the input queue of the foreground thread. The message pump attached to that thread then deals with those keypresses. That will often involve delivering a WM_PASTE message to a windowed control. Windows does not paste. The application with the input focus gets a message that the user has pressed CTRL+V and then chooses to read from the clipboard, interpret its contents, and respond accordingly. – David Heffernan Sep 17 '11 at 12:00
  • So i have no way unless using Send-Keys. correct? – Kermia Sep 17 '11 at 12:03
  • 8
    Send Keys is the most reliable approach, in my view. – David Heffernan Sep 17 '11 at 12:04
  • Which application has the focus? yours or the one where you wanna "paste" the string? – Whiler Sep 17 '11 at 12:09
  • 1
    Title has nothing to do with question. If you want to put keys into an edit box, then the clipboard, and paste have NOTHING to do with it. – Warren P Sep 17 '11 at 16:52
  • 1
    The title is edited. Thank you for your down votes :) – Kermia Sep 18 '11 at 06:40
  • 1
    Upvoted because you edited the question, which makes it more useful for other people who come along later. I fixed up your question a bit to match the new title. Hope it's okay. – Warren P Sep 18 '11 at 21:41

2 Answers2

3

As your application knows the string it has to send...

You can set the focus to the target window/application if needed.

Then you process each char contained in your string to simulate their key strokes. Something like that (too basic to work exactly as you expect, but the idea is here... ;o)):

for i := 1 to Length(yourstring) do
begin
  keybd_event(Ord(yourstring[i]), 0, 0, 0);  // key down
  Sleep(10);
  keybd_event(Ord(yourstring[i]), 0, 0 or KEYEVENTF_KEYUP, 0); / key up
  Sleep(10);
end;

If your string uppercase, ..., you need to simulate the shift, ctrl, ...

Whiler
  • 7,998
  • 4
  • 32
  • 56
0

Input multiple byte characters with keybd_event:

procedure InsertText(text:string);
var i:integer;
    j:integer;
    ch:byte;
    str:string;
begin
  i:=1;
  while i<=Length(text) do
  begin
    ch:=byte(text[i]);
    if Windows.IsDBCSLeadByte(ch) then
       begin
         Inc(i);
         str:=inttostr(MakeWord(byte(text[i]), ch));
         keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
         j:=1;
         while j<=Length(str) do
         begin
               keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 0, 0);
               keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 2, 0);
               j:=j+1;
         end;
         keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
       end
    else begin
           keybd_event(VkKeyScan(text[i]),0,0,0);
           keybd_event(VkKeyScan(text[i]),0,2,0);
         end;
    Inc(i);
  end;
end;
diyism
  • 12,477
  • 5
  • 46
  • 46