Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send keys to another application using delphi 7?

Ok, so Pretty much i am trying to send keystrokes of a string from and edit box to the active window and the enter key after. does anyone here know a working method of doing this in delphi 7?

I have been searching for about an hour and a half for this now and i cant seem to find anything and the stuff I have found is ether for newer versions of delphi, or it just doesn't work. I have tried TTouchKeyboard but that's only for delphi 10 and newer.

like image 266
connorbp Avatar asked Mar 12 '12 19:03

connorbp


1 Answers

I've used this to send text to an annoying popup 3G application with no interface, its a hack be we wern't left with any option.

procedure TForm1.TypeMessage(Msg: string);
var
  CapsOn: boolean;
  i: integer;
  ch: char;
  shift: boolean;
  key: short;
begin
  CapsOn := (GetKeyState( VK_CAPITAL ) and $1) <> 0;

  for i:=1 to length(Msg) do
  begin
    ch := Msg[i];
    ch := UpCase(ch);

    if ch <> Msg[i] then
    begin
      if CapsOn then
      begin
        keybd_event( VK_SHIFT, 0, 0, 0 );
      end;
      keybd_event( ord(ch), 0, 0, 0 );
      keybd_event( ord(ch), 0, KEYEVENTF_KEYUP, 0 );
      if CapsOn then
      begin
        keybd_event( VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 );
      end;
    end
    else
    begin
      key := VKKeyScan( ch );
      // UpperCase
      if ((not CapsOn) and (ch>='A') and (ch <= 'Z')) or
         ((key and $100) > 0) then
      begin
        keybd_event( VK_SHIFT, 0, 0, 0 );
      end;
      keybd_event( key, 0, 0, 0 );
      keybd_event( key, 0, KEYEVENTF_KEYUP, 0 );
      if ((not CapsOn) and (ch>='A') and (ch <= 'Z')) or
         ((key and $100) > 0) then
      begin
        keybd_event( VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 );
      end;
    end;
  end;
end;

hope that helps

UPDATE

Edited to allow other characters (non alpha ) ie shifted numerals !"£$ etc.

like image 65
Dampsquid Avatar answered Sep 28 '22 04:09

Dampsquid