2004. október 4., hétfő

Send characters to another control (in any application)


Problem/Question/Abstract:

How can I e.g. "type" programmatically in another application?

Answer:

There are several methods to send keystrokes or characters to a WinControl. The SetKeyboardState requires the control to have the focus but can send more (esp. special) keys. The use of WM_CHAR message enables you to send characters even to a hidden control unless you have found out the handle of it (there are a couple of ways to find out a controls handle). Once you've got it, you can send messages to it.

I'm using this method for a little tool to "type" certain frequently used phrases while posting to newsgroups.

I hardcoded the handle of the edit control of my favorite newsreader and now I have a small and handy tool to type messages faster than ever.

procedure SendMsg(const h: HWND; const s: string);
var
  i: integer;
begin
  if h = 0 then
    Exit;
  if Length(s) = 0 then
    Exit;
  for i := 1 to Length(s) do
  begin
    if Ord(s[i]) in [9, 13, 32..254] then
      SendMessage(h, WM_CHAR, Ord(s[i]), 0);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése