怎样在程序中模拟按下了键盘上的某个键 ?(100分)

  • 主题发起人 主题发起人 蓝天
  • 开始时间 开始时间

蓝天

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样在程序中模拟按下了键盘上的某个键 ? 例如虽然用户实际上并没有按F2键,但是程序能够模拟按下了F2键,并且得到和用户实际按下F2键相同的效果.
 
试一下SendMessage()函数
SendMessage(Handle,Msg,0,0)
Msg代表了具体的击键信息,WINDOWS对不同功能键豆油定义.
 
用 keybd_event(虚拟键值,扫描码,标志);

The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.

VOID keybd_event(

BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);


Parameters

bVk

Specifies a virtual-key code. The code must be a value in the range 1 to 254.

bScan

Specifies a hardware scan code for the key.

dwFlags

A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags:

Value Meaning
KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed.


dwExtraInfo

Specifies an additional 32-bit value associated with the key stroke.



Return Values

This function has no return value.

Remarks

Although keybd_event passes an OEM-dependent hardware scan code to Windows, applications should not use the scan code. Windows converts scan codes to virtual-key codes internally and clears the up/down bit in the scan code before passing it to applications.
An application can simulate a press of the PRINTSCREEN key in order to obtain a screen snapshot and save it to the Windows clipboard. To do this, call keybd_event with the bVk parameter set to VK_SNAPSHOT, and the bScan parameter set to 0 for a snapshot of the full screen or set bScan to 1 for a snapshot of the active window.
 
keybd_event(VK_F2,1,0,0); //F2键按下
keybd_event(VK_F2,1,KEYEVENTF_KEYUP,0); //F2键释放
 
postmessage(handle,wm_keydown,VK_F2,0);
大概能達到你的要求.
 
像这样:
keybd_event(VK_F2, MapVirtualKey(VK_F2, 0), 0, 0);
keybd_event(VK_F2, MapVirtualKey(VK_F2, 0), KEYEVENTF_KEYUP, 0);

beta(mophy@188.net)
 
PopEye的E文我看不大明白,能给我每个参数的含义或具体用法吗?
 
多人接受答案了。
 
后退
顶部