🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.nativeapi
Article #204

Re: press a key

#204
From: Eric Hill
Date: Fri, 23 Jan 2004 17:11
194 lines
5210 bytes
Here's a good start:
http://groups.google.com/groups?q=delphi+keybd_event

If you're trying to call something like print-screen...

keybd_event(VK_SNAPSHOT,1,0,0); {client window snapshot}
keybd_event(VK_SNAPSHOT,0,0,0); {desktop window snapshot}

Here's a darn good sample as well...


----------------------------------------
From: "Joe C. Hecht" <joehecht@gte.net>
Newsgroups: borland.public.delphi.objectpascal
Subject: Re: Visual Basic's  SENDKEY  Command

The following example demonstrates procedures that provide the
capability of sending keystrokes to any window control capable
of recieving keyboard input.

You may use the this technique to toggles the num lock, caps lock, and
scroll lock  keys under Windows NT. This same technique works for
toggling caps lock and  scroll lock under Windows 95, but it will not
work for num lock.

The SimulateKeyDown(), SimulateKeyUp(), and SimulateKeystroke()
procedures expect a virtural key code (like VK_F1).
The SimulateKeystroke() procedure accepts an extra parameter that
is useful when simulating the PrintScreen key.
When extra is set to zero, the entire screen will be captured to the
windows clipboard. When extra is set to one, only the active window
will be captured.

The four button click methods demonstrate the use of these functions:
ButtonClick1 -  Toggles the cap lock.
ButtonClick2 - Captures the entire screen to the clipboard.
ButtonClick3 - Capture the active window to the clipboard.
ButtonClick4 - Set the focus to an edit control and sends it a string.

Example:


procedure SimulateKeyDown(Key : byte);
begin
    keybd_event( Key, 0, 0, 0 );
end;


procedure SimulateKeyUp(Key : byte);
begin
    keybd_event( Key, 0, KEYEVENTF_KEYUP, 0 );
end;


procedure SimulateKeystroke
(
 Key : byte;
        extra : DWORD
);
begin

     keybd_event
 (
  Key,
               extra,
               0,
               0
 );

 keybd_event
 (
  Key,
               extra,
               KEYEVENTF_KEYUP,
               0
 );
end;


procedure SendKeys(s : string);
var
    i : integer;
    flag : bool;
    w : word;
begin

    (*
    ** Get the state of the caps lock key
    *)

    flag := not GetKeyState(VK_CAPITAL) and 1 = 0;

    (*
    ** If the caps lock key is on then turn it off
    *)

    if flag then
        SimulateKeystroke( VK_CAPITAL, 0 );

    for i := 1 to Length(s) do begin

        w := VkKeyScan(s[i]);

        (*
        ** If there is not an error in the key translation
        *)

        if ((HiByte(w) <> $FF) and (LoByte(w) <> $FF)) then begin

            (*
            ** If the key requires the shift key down - hold it down
            *)

            if HiByte(w) and 1 = 1 then
                SimulateKeyDown( VK_SHIFT );

            (*
            ** Send the VK_KEY
            *)

            SimulateKeystroke( LoByte(w), 0 );

            (*
            ** If the key required the shift key down - release it
            *)

            if HiByte(w) and 1 = 1 then
                SimulateKeyUp(VK_SHIFT);

        end;

    end;

    (*
    ** if the caps lock key was on at start, turn it back on
    *)

    if flag then
        SimulateKeystroke( VK_CAPITAL, 0 );

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

    (*
    ** Toggle the cap lock
    *)

    SimulateKeystroke( VK_CAPITAL, 0 );

end;

procedure TForm1.Button2Click(Sender: TObject);
begin

    (*
    ** Capture the entire screen to the clipboard
    ** by simulating pressing the PrintScreen key
    *)

    SimulateKeystroke( VK_SNAPSHOT, 0 );

end;

procedure TForm1.Button3Click(Sender: TObject);
begin

    (*
    ** Capture the active window to the clipboard
    ** by simulating pressing the PrintScreen key
    *)

    SimulateKeystroke( VK_SNAPSHOT, 1 );

end;

procedure TForm1.Button4Click(Sender: TObject);
begin

    (*
    ** Set the focus to a window (edit control) and send it a string
    *)

    Application.ProcessMessages;
    Edit1.SetFocus;
    SendKeys( 'Delphi Is RAD!' );

end;

------------------------------- 
Sample Provided by Joe C. Hecht
joehecht@gte.net
http://home1.gte.net/joehecht/index.htm
Technical Details
Message-ID: <401155ae@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <400fd49c$1@newsgroups.borland.com> <400ffa9e$1@newsgroups.borland.com> <401151d9$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f94dd,92b889d3d0078aa5
X-Google-Attributes: gidf94dd,public
X-Google-ArrivalTime: 2004-01-23 09:40:38 PST
From: "Eric Hill" <eric@ijack.net>
Newsgroups: borland.public.delphi.nativeapi
References: <400fd49c$1@newsgroups.borland.com> <400ffa9e$1@newsgroups.borland.com> <401151d9$1@newsgroups.borland.com>
Subject: Re: press a key
Date: Fri, 23 Jan 2004 11:11:48 -0600
Lines: 196
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 64.66.89.17
Message-ID: <401155ae@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1074877870 64.66.89.17 (23 Jan 2004 09:11:10 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail