Simulate the pressing of keyboard keys

xiaoxiao2021-03-06  38

1.

PostKeyex32

Function}

Procedure

PostKeyex32

(

Key

:

Word

;

Const

Shift

:

TshiftState

;

Specialkey

:

Boolean

);

{********************************************************** ***********

* Procedure PostKeyex32

*

* Parameters:

* Key: Virtual Keycode of the key to send. For printable

* Keys this is simply the ANSI Code (ORD (Character).

* Shift: State of The Modifier Keys. This is a set, so you

* Can set Several of these Keys (Shift, Control, Alt,

* Mouse buttons) In Tandem. The TshiftState Type IS

* Declared in the classes unit.

* Specialkey: NORMALLY THIS SHOULD BE FALSE. SET It To True To

* Specify a key on the numeric keypad, for example.

* DESCRIPTION:

* Uses keybd_event to manufacture a series of key events matching

* The Passed Parameters. The Events Go To The Control with Focus.

* Note That for Characters Key Is Always The Upper-Case Version of

* The Character. Sending WITHOUT ANY Modifier Keys Will Result in

* A Lower-Case Character, Sending It with [SSSHIFT] WILL RESULT

* In An Upper-Case Character!

// Code by P. Below

*********************************************************** **********}

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

;

i

:

Integer

;

Begin

for

i

: =

1

TO

3

DO

Begin

IF

ShiftKeys

[

i

].

Shift

in

Bshift

THEN

Keybd_Event

(

ShiftKeys [

i

].

Vkey

,

MapVirtualKey

(

ShiftKeys

[

i

].

Vkey

,

0

),

0

,

0

);

end

;

{For}

IF

Specialkey

THEN

Flag

: =

KeyEventf_extendedKey

Else

Flag

: =

0

;

Keybd_Event

(

Key

,

MapVirtualKey

(

Key

,

0

),

Flag

,

0

);

Flag

: =

Flag

oral

KeyEventf_Keyup

;

Keybd_Event

(

Key

,

MapVirtualKey

(

Key

,

0

),

Flag

,

0

);

for

i

: =

3

Downto

1

DO

Begin

IF

ShiftKeys

[

i

].

Shift

in

Bshift

THEN

Keybd_Event

(

ShiftKeys

[

i

].

Vkey

,

MapVirtualKey

(

ShiftKeys

[

i

].

Vkey

,

0

),

KeyEventf_Keyup

,

0

);

end

;

{For}

end

;

{PostKeyEx32}

// EXAMPLE:

Procedure

TForm1

.

Button1click

(

Sender

:

TOBJECT

);

Begin

// Pressing the Left Windows Key

PostKeyex32

(

VK_LWIN

,

[],

False

);

// Pressing the Letter D

PostKeyex32

(

ORD

(

'D'

),

[],

False

);

// Pressing Ctrl-Alt-C

PostKeyex32

(

ORD

(

'C'

),

[

Ssctrl

,

Ssalt

],

False

);

end

;

{********************************************************** ***********}

{2. beybd_event API}

Procedure

TForm1

.

Button1click

(

Sender

:

TOBJECT

);

Begin

{or you can also Try this Simple Example To Send Any

Amount of keyStrokes at the same time.}

{Pressing the a key and showing it in the edit1.text}

Edit1

.

SetFOCUS

;

Keybd_Event

(

VK_SHIFT

,

0

,

0

,

0

);

Keybd_Event

(

ORD

(

'A'

),

0

,

0

,

0

);

Keybd_Event

(

VK_SHIFT

,

0

,

KeyEventf_Keyup

,

0

);

{Presses the Left window key and starts the run}

Keybd_Event

(

VK_LWIN

,

0

,

0

,

0

);

Keybd_Event

(

ORD

(

'R'

),

0

,

0

,

0

);

Keybd_Event

(

VK_LWIN

,

0

,

KeyEventf_Keyup

,

0

);

end

;

{********************************************************** **********} {3. With keybd_event API}

Procedure

Postkeyexhwnd

(

Hwindow

:

HWnd

;

Key

:

Word

;

Const

Shift

:

TshiftState

;

Specialkey

:

Boolean

);

{********************************************************** ***********

* Procedure PostKeyex

*

* Parameters:

* Hwindow: target window to be send the keystroke

* Key: Virtual Keycode of the key to send. For printable

* Keys this is simply the ANSI Code (ORD (Character).

* Shift: State of The Modifier Keys. This is a set, so you

* Can set Several of these Keys (Shift, Control, Alt,

* Mouse buttons) In Tandem. The TshiftState Type IS

* Declared in the classes unit.

* Specialkey: NORMALLY THIS SHOULD BE FALSE. SET It To True To

* Specify a key on the numeric keypad, for example.

* If this parameter is true, bit 24 of the lparam for

* The Posted WM_Key * Messages Will Be Set.

* DESCRIPTION:

* This Procedure Sets Up Windows Key State Array To Correctly

* Reflect the Requested Pattern of Modifier Keys and Then Posts

* A wm_keydown / wm_keyup message pair to the target window..

* Application.ProcessMessages IS Called to Process The Messages

* Before the keyboard state is restored.

* Error Conditions:

* May Fail Due to Lack of Memory for the Two Key State BUFFERS.

* Will Raise an Exception in this case.

* Note:

* Setting The Keyboard State Will Not Work Across Applications

* Running in Different Memory Spaces on Win32 Unless ATCHTHREADINPUT

* Is used to connect to the target thread first.

* Created: 02/21/96 16:39:00 by P. Below

*********************************************************** **********}

Type

TBuffers

= array

[

0..1

]

Of

TKEYBOARDSTATE

;

VAR

PKEYBUFFERS

:

^

TBuffers

;

lparam

:

Longint

;

Begin

(* Check if the target window exists *)

IF

Iswindow

(

Hwindow

)

THEN

Begin

(* Set local variables to default value *)

PKEYBUFFERS

: =

NIL

;

lparam

: =

Makelong

(

0

,

MapVirtualKey

(

Key

,

0

));

(* Modify LParam if Special Key Requested *)

IF

Specialkey

THEN

lparam

: =

lparam

oral

$ 1000000

;

(* Allocate Space for the key state buffers *)

New

(

PKEYBUFFERS

);

Try

(* Fill Buffer 1 with Current State So We can Later Restore It.

Null Out Buffer 0 TO GET A "NO Key Pressed" State. *)

GetKeyboardState

(

PKEYBUFFERS

^ [

1

]);

Fillchar

(

PKEYBUFFERS

^ [

0

],

Sizeof

(

TKEYBOARDSTATE

),

0

);

(* Set the requested modifier keys to "down" state in buffer *)

IF

Ssshift

in

Shift

THEN

PKEYBUFFERS

^ [

0

] [

VK_SHIFT

]

: =

$ 80

;

IF

Ssalt

in

Shift

THEN

Begin

(* Alt Needs Special Treatment Since A Bit in LParam Needs Also Be set *)

PKEYBUFFERS

^ [

0

] [

VK_MENU

]

: =

$ 80

;

lparam

: =

lparam

oral

$ 20000000

;

end

;

IF

Ssctrl

in

Shift

THEN

PKEYBUFFERS

^ [

0

] [

VK_Control

]

: =

$ 80

;

IF

SSLEFT

in

Shift

THEN

PKEYBUFFERS

^ [

0

] [

VK_LButton

]

: =

$ 80

;

IF

Sright

in

Shift

THEN

PKEYBUFFERS

^ [

0

] [

VK_RBUTTON

]

: =

$ 80

;

IF

SSMIDDLE

in

Shift

THEN

PKEYBUFFERS

^ [

0

] [

VK_MButton

]

: =

$ 80

;

(* Make out new key state array the agent key state map *)

SetKeyboardState

(

PKEYBUFFERS

^ [

0

]);

(* Post the key message *)

IF

Ssalt

in

Shift

THEN

Begin

Postmessage

(

Hwindow

,

WM_SYSKEYDOWN

,

Key

,

lparam

);

Postmessage

(

Hwindow

,

WM_SYSKEYUP

,

Key

,

lparam

oral

$ C0000000

);

end

Else

Begin

Postmessage

(

Hwindow

,

WM_KEYDOWN

,

Key

,

lparam

);

Postmessage

(

Hwindow

,

WM_KEYUP

,

Key

,

lparam

oral

$ C0000000

);

end

;

(* Process the message *)

Application

.

ProcessMessages

;

(* Restore the old key state map *)

SetKeyboardState

(

PKEYBUFFERS

^ [

1

]);

Finally

(* Free the memory *)

IF

PKEYBUFFERS

<>

NIL

THEN

Dispose

(

PKEYBUFFERS

);

end

;

{If}

end

;

end

;

{PostKeyEx}

// EXAMPLE:

Procedure

TForm1

.

Button1click

(

Sender

:

TOBJECT

);

VAR

TargetWnd

:

HWnd

;

Begin

TargetWnd

: =

Findwindow

(

'Notepad'

,

NIL

)

IF

TargetWnd

<>

0

THEN

Begin

Postkeyexhwnd

(

TargetWnd

,

ORD

(

'I'

),

[

Ssalt

],

False

);

end

;

end

;

{********************************************************** **********}

{3. with sendInput api}

// EXAMPLE: Send Text

Procedure

TForm1

.

Button1click

(

Sender

:

TOBJECT

);

Const

Str

:

String

=

'Writing Writing Writing'

;

VAR

INP

:

TINPUT

;

I

:

Integer

;

Begin

Edit1

.

SetFOCUS

;

for

I

: =

1

TO

Length

(

Str

)

DO

Begin

// Press

INP

.

ITYPE

: =

INPUT_KEYBOARD

;

INP

.

ki

.

WVK

: =

ORD

(

Upcase

(

Str

[

i

]));

INP

.

ki

.

DWFlags

: =

0

;

Sendinput

(

1

,

INP

,

Sizeof

(

INP

));

// Release

INP

.

ITYPE

: =

INPUT_KEYBOARD

;

INP

.

ki

.

WVK

: =

ORD

(

Upcase

(

Str

[

i

]));

INP

.

ki

.

DWFlags

: =

KeyEventf_Keyup

;

Sendinput

(

1

,

INP

,

Sizeof

(

INP

));

Application

.

ProcessMessages

;

Sleep

(

80

);

end

;

end

;

// EXAMPLE: Simulate Alt Tab

Procedureesendalttab

;

VAR

Keyinputs

:

array

Of

TINPUT

;

KeyinputCount

:

Integer

;

Procedure

Keybdinput

(

Vkey

:

Byte

;

Flags

:

DWORD

);

Begin

Incc

(

KeyinputCount

);

SetLength

(

Keyinputs

,

KeyinputCount

);

Keyinputs

[

KeyinputCount

-

1

].

ITYPE

: =

INPUT_KEYBOARD

;

WITH

Keyinputs

[

KeyinputCount

-

1

].

ki

DO

Begin

WVK

: =

Vkey

;

WSCAN

: =

MapVirtualKey

(

WVK

,

0

);

DWFlags

: =

KeyEventf_extendedKey

;

DWFlags

: =

Flags

oral

DWFlags

;

Time

: =

0

;

DWEXTRAINFO

: =

0

;

end

;

end

;

Begin

Keybdinput

(

VK_MENU

,

0

);

// alt

Keybdinput

(

VK_TAB

,

0

);

// Tab

Keybdinput

(

VK_TAB

,

KeyEventf_Keyup

);

// Tab

Keybdinput

(

VK_MENU

,

KeyEventf_Keyup

);

// alt

Sendinput

(

KeyinputCount

,

Keyinputs

[

0

],

Sizeof

(

Keyinputs

[

0

]));

end

;

转载请注明原文地址:https://www.9cbs.com/read-65023.html

New Post(0)