• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Send keystrokes to an application (from a variable)

Hi.

How can i send keystrokes to a focused window ?
Those keystrokes should come from a variable !

For example :

Var  TheWord:String;

TheWord:='abcdefgh';

    keybd_event(ord('a'),0,0,0);
    keybd_event(ord('a),0,KEYEVENTF_KEYUP,0);
    keybd_event(ord('b'),0,0,0);
    keybd_event(ord('b),0,KEYEVENTF_KEYUP,0);

Thanks in advance.
0
CodedK
Asked:
CodedK
  • 2
1 Solution
 
ZhaawZSoftware DeveloperCommented:
Try this. It's similar to what I'm using to show winamp now-playing info in any window, so I think it should work (I can't check it because I just reinstalled system and do not have Delphi yet, heh).

  procedure SendInfo;
  var
    pid1 : cardinal;
    pid2 : cardinal;
    wind : cardinal;
    num  : integer;
    text : string;
  begin
  pid1 := GetWindowThreadProcessId(GetForegroundWindow);
  pid2 := GetWindowThreadProcessId(wnd);
  AttachThreadInput(pid1, pid2, true);
  wind := GetFocus;
  AttachThreadInput(pid1, pid2, false);
  wsprintfA(@text, '·· %s ·· %ukbps ·· %u:%02u ··'#13, tmp, kbps, mins, secs);
  text := 'some string';
  for num := 1 to length(text) do PostMessage(wind, wm_char, byte(text[num]), 0);
  end;
0
 
CodedKAuthor Commented:
Thang you ZhaawZ :)
0
 
ZhaawZSoftware DeveloperCommented:
glad to help ;)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now