[Tutor] simulate a long key press

Alan Gauld alan.gauld at btinternet.com
Mon Oct 16 18:40:24 CEST 2006


"Jack" <aguffabuff at hotmail.com> wrote in message
> I know about using sendkeys to simulate typing on the keyboard,
> but how can I simulate holding down a key for several seconds?

I think you need to use ctypes or pythonwin to send a
key_down/key_up sequence using the Windows
Post/SendMessage API function.

I don't know of any other way. The help info below might be useful.

Alan G

############ From the Win32 help file #############
The SendMessage function sends the specified message to a window or 
windows. The function calls the window procedure for the specified 
window and does not return until the window procedure has processed 
the message. The PostMessage function, in contrast, posts a message to 
a thread's message queue and returns immediately.

LRESULT SendMessage(

    HWND  hwnd, // handle of destination window
    UINT  uMsg, // message to send
    WPARAM  wParam, // first message parameter
    LPARAM  lParam  // second message parameter
   );
Parameters

hwnd

Identifies the window whose window procedure will receive the message. 
If this parameter is HWND_BROADCAST, the message is sent to all 
top-level windows in the system, including disabled or invisible 
unowned windows, overlapped windows, and pop-up windows; but the 
message is not sent to child windows.

uMsg

Specifies the message to be sent.

wParam

Specifies additional message-specific information.

lParam

Specifies additional message-specific information.

Return Value

The return value specifies the result of the message processing and 
depends on the message sent.

########################
WM_KEYDOWN

nVirtKey = (int) wParam;    // virtual-key code
lKeyData = lParam;          // key data


The WM_KEYDOWN message is posted to the window with the keyboard focus 
when a nonsystem key is pressed. A nonsystem key is a key that is 
pressed when the ALT key is not pressed.

Parameters

nVirtKey

Value of wParam. Specifies the virtual-key code of the nonsystem key.

lKeyData

Value of lParam. Specifies the repeat count, scan code, extended-key 
flag, context code, previous key-state flag, and transition-state 
flag, as shown in the following table:

Value Description
0-15 Specifies the repeat count. The value is the number of times the 
keystroke is repeated as a result of the user holding down the key.
16-23 Specifies the scan code. The value depends on the original 
equipment manufacturer (OEM).
24 Specifies whether the key is an extended key, such as the 
right-hand ALT and CTRL keys that appear on an enhanced 101- or 
102-key keyboard. The value is 1 if it is an extended key; otherwise, 
it is 0.
25-28 Reserved; do not use.
29 Specifies the context code. The value is always 0 for a WM_KEYDOWN 
message.
30 Specifies the previous key state. The value is 1 if the key is down 
before the message is sent, or it is 0 if the key is up.
31 Specifies the transition state. The value is always 0 for a 
WM_KEYDOWN message.
Return Value

An application should return zero if it processes this message.
###################################
WM_KEYUP

nVirtKey = (int) wParam;    // virtual-key code
lKeyData = lParam;          // key data


The WM_KEYUP message is posted to the window with the keyboard focus 
when a nonsystem key is released. A nonsystem key is a key that is 
pressed when the ALT key is not pressed, or a keyboard key that is 
pressed when a window has the keyboard focus.

Parameters

nVirtKey

Value of wParam. Specifies the virtual-key code of the nonsystem key.

lKeyData

Value of lParam. Specifies the repeat count, scan code, extended-key 
flag, context code, previous key-state flag, and transition-state 
flag, as shown in the following table:

Value Description
0-15 Specifies the repeat count. The value is the number of times the 
keystroke is repeated as a result of the user holding down the key. 
The repeat count is always one for a WM_KEYUP message.
16-23 Specifies the scan code. The value depends on the original 
equipment manufacturer (OEM).
24 Specifies whether the key is an extended key, such as the 
right-hand ALT and CTRL keys that appear on an enhanced 101- or 
102-key keyboard. The value is 1 if it is an extended key; otherwise, 
it is 0.
25-28 Reserved; do not use.
29 Specifies the context code. The value is always 0 for a WM_KEYUP 
message.
30 Specifies the previous key state. The value is always 1 for a 
WM_KEYUP message.
31 Specifies the transition state. The value is always 1 for a 
WM_KEYUP message.
Return Value

An application should return zero if it processes this message.




More information about the Tutor mailing list