Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keyboard Shortcut problem...

Status
Not open for further replies.

FifthFreedom

Programmer
Joined
May 25, 2009
Messages
2
Location
GB
Hi, I've been struggling with this for the last few hours wondering if there is a simple soloution I have missed.

What I'm trying to achieve is a script which sends a combination of keyboard presses to the window (ANY window) which was in focus before the script was run.

Any ideas?
Thanks.
 
you mean like windows from another application?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yup precisely.

Sorry that wasn't particularly clear...
 
dinner is server!
to send a key to a known windows handle:

Code:
procedure SendKeysTohWnd(hWnd : LongWord; Text : string);
// send keystrokes to a specific window handle
var i         : integer;
    c         : char;
    wparam,
    lparam    : longword;
    scancode  : byte;
    oemScan   : word;
    prevchar  : word;
    Len       : word;

begin
 if Text <> '' then
  begin
   Len:=length(Text);
   i:=1;
   while i <= Len do
   begin
    c:=Text[i];
    wparam:=ord(c);
    // if character is #0 then next chars will be only keydown
    // if char is #1 then next chars will only be keyup
    if wparam < 2 then
     begin
      // #0 or #1
      Inc(i);
      if i > Len then Exit; // prevent critical error
      c:=Text[i];
      prevchar:=wparam;
      wparam:=ord(c);
     end
    else prevchar:=wparam;
    if (c > #31) and (PrevChar > 1) then // only do wm_char if keyup,keydown control chars are not used
     begin
      // normal chars, use WM_CHAR message to simulate keystroke
      oemScan:=Lobyte(vkKeyScan(c));
      scancode:=MapVirtualKey(oemScan,0);
      lparam:=1+(scancode shl 16);
      postmessage(hwnd,WM_CHAR,wparam,lparam);
     end
    else
     begin
      // system codes, use WM_KEYDOWN and WM_KEYUP to simulate keystroke
      scancode:=MapVirtualKey(wparam,0);
      if prevchar <> 1 then
       begin
        lparam:=1 or (scancode shl 16) or $40000000; // (1 shl 30);
        postmessage(hwnd,WM_KEYDOWN,wparam,lparam);
       end;
      if prevchar <> 0 then
       begin
        lparam:=1 or (scancode shl 16) or $C0000000; // 3 shl 30
        postmessage(hwnd,WM_KEYUP,wparam,lparam);
       end;
     end;
    Inc(i);
   end;
  end;
end;

for non owned windows, a little more work is involved :

Code:
procedure TForm1.Button1Click(Sender: TObject);

var tid, tidto : cardinal;
    hwnd : integer;
    hwnd_edbox : integer;

begin
 Tid := GetCurrentThreadId;
 hwnd := findwindow(nil, pchar('window title here'));
 hwnd_edbox := findwindowex(hwnd,0,'Edit',nil);
 TidTo := GetWindowThreadProcessId(hwnd);
 AttachThreadInput(TidTo,Tid,True);
 SendKeysToHwnd(hwnd_edbox,'1322'#13);
 AttachThreadInput(TidTo,Tid,False);
end;

have fun...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top