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

3

5

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.

Saretta answered 12/3, 2012 at 19:21 Comment(1)
Does it have to be Delphi? Look at Autohotkey.Photoreceptor
N
9

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.

Nonagon answered 12/3, 2012 at 19:30 Comment(6)
Thaks so much! :) that worked! :) awesome man. but does anyone know how i would send the enter key?Saretta
sure just call TypeMessage( char(VK_RETURN) ); or add char(VK_RETURN) to your messageNonagon
this way doesnt type any characters like / . or : how could i get those to work?Saretta
ok connor editied to allow those types of chars, didn't need them in my app but it does them now.Nonagon
oops, 3rd time lucky, try thatNonagon
Thanks so much! it works! :) and haha i was working on a kind of a filter to check if it was a letter or not and to not cap it if its not a letter. (mine did not work) thanks! :)Saretta
N
6

See keybd_event function. You will need to perform translation between chars and keyboard scan codes, but internet is full of information on this.

Unless you need to emulate typing, it makes sense to send WM_SETTEXT to the edit box window and then send Enter as a keyboard. This will let you avoid dealing with scancodes.

Nolita answered 12/3, 2012 at 19:26 Comment(0)
E
0

Use SendKeys() from the unit SNDKEY32.PAS on the Delphi 7 installation CD. In case you cannot find your CD, look here. Works fine for me (Delphi7 on Windows 7).

Empennage answered 5/10, 2014 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.