how to simulate key press delphi
سلام من که عاشق این کدهام شما رو نمی دونم
Simulating keystrokes from code
PROCEDURE PostKeyEx32(key: Word; const shift: TShiftState; specialkey: Boolean) ;
type
TShiftKeyInfo = record
shift: Byte ;
vkey: Byte ;
end;
ByteSet = set of 0..7 ;
const
shiftkeys: array [1..3] of TShiftKeyInfo =
((shift: Ord(ssCtrl) ; vkey: VK_CONTROL),
(shift: Ord(ssShift) ; vkey: VK_SHIFT),
(shift: Ord(ssAlt) ; vkey: VK_MENU)) ;
var
flag: DWORD;
bShift: ByteSet absolute shift;
j: Integer;
BEGIN
for j := 1 to 3 do
begin
if shiftkeys[j].shift in bShift then
keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0) ;
end;
if specialkey then
flag := KEYEVENTF_EXTENDEDKEY
else
flag := 0;
keybd_event(key, MapvirtualKey(key, 0), flag, 0) ;
flag := flag or KEYEVENTF_KEYUP;
keybd_event(key, MapvirtualKey(key, 0), flag, 0) ;
for j := 3 downto 1 do
begin
if shiftkeys[j].shift in bShift then
keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), KEYEVENTF_KEYUP, 0) ;
end;
END;
// Simulate PRINTSCREEN - snapshot of the full screen
PostKeyEx32(VK_SNAPSHOT, [], False) ;
// Simulate PRINTSCREEN - snapshot of the active window
PostKeyEx32(VK_SNAPSHOT, [ssAlt], False) ;
// Simulate left Windows key
PostKeyEx32(VK_LWIN, [], False) ;
// Simulate Alt+F4 - close active window
PostKeyEx32(VK_F4, [ssAlt], False) ;
Simulating keyboard input in Delphi using WinAPI
Press the F3 button.
Press the right-arrow key 3 times.
Press the space button.
Type letter 'E' on the keyboard.
Press the right-arrow key.
// Press F3 button
keybd_event(VK_F3, 0, 0, 0);
// Press right arrow key 3 times
keybd_event(VK_RIGHT, 0, 0, 0);
keybd_event(VK_RIGHT, 0, 0, 0);
keybd_event(VK_RIGHT, 0, 0, 0);
// Press the space button
keybd_event(VK_SPACE, 0, 0, 0);
// Type letter E
keybd_event(Ord('E'), 0, 0, 0);
// Move to the right
keybd_event(VK_RIGHT, 0, 0, 0);
Each key press is a key down and then a key up. So you need two calls to keybd_event per key press. For example, to press F3:
هر key press تشكيل شده از يك key down و یک key up که با هم اجرا شود . مثال برای فشردن کلید F3 شما نياز به صدا زدن دو keybd_event دارید
keybd_event(VK_F3, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(VK_F3, 0, KEYEVENTF_KEYUP, 0);
wnd := FindWindow('TfrmMain', nil);
if wnd <> 0 then
begin
PostMessage(wnd, WM_KEYDOWN, ord('P'),1);
PostMessage(wnd, WM_KEYUP, ord('P'),1);
label1.caption:='Yes1';
end