2 回答

TA贡献1813条经验 获得超2个赞
The KEYBDINPUT structure contains information about a simulated
keyboard event.
Syntax
typedef struct tagKEYBDINPUT {
WORD wVk;
WORD wScan;
DWORD dwFlags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KEYBDINPUT, *PKEYBDINPUT;
Members
wVk
Specifies a virtual-key code. The code must be a value in the range 1 to
254. The Winuser.h header file provides macro definitions (VK_*) for each value.
If the dwFlags member
specifies KEYEVENTF_UNICODE, wVk must be 0.
wScan
Specifies a hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character
which is to be sent to the foreground application.
dwFlags
Specifies various aspects of a keystroke. This member can be certain
combinations of the following values.
KEYEVENTF_EXTENDEDKEY
If specified, the scan code was preceded by a prefix byte that has the value
0xE0 (224).
KEYEVENTF_KEYUP
If specified, the key is being released. If not specified, the key is being
pressed.
KEYEVENTF_SCANCODE
If specified, wScan
identifies the key and wVk
is ignored.
KEYEVENTF_UNICODE
Windows 2000/XP: If specified, the system synthesizes a VK_PACKET
keystroke. The wVk parameter
must be zero. This flag can only be combined with the KEYEVENTF_KEYUP flag. For
more information, see the Remarks section.
time
Time stamp for the event, in milliseconds. If this parameter is zero, the
system will provide its own time stamp.
dwExtraInfo
Specifies an additional value associated with the keystroke. Use the
GetMessageExtraInfo
function to obtain this information.
函数说明如上:
INPUT input[2];
memset(input, 0, sizeof(input));
//设置模拟键盘输入
input[0].type =input[1].type= INPUT_KEYBOARD;
input[0].ki.wVk= VK_TAB;
input[0].ki.dwFlags=0;//先按下
// 释放按键
input[1].ki.dwFlags = KEYEVENTF_KEYUP;//放开
input[1].ki.wVk=VK_TAB;
SendInput(1, input, sizeof(INPUT));
Sleep(2000);
SendInput(1, input+1, sizeof(INPUT));
没试过,你试试。

TA贡献1775条经验 获得超11个赞
class SKeyboardInput { // KEYBDINPUT
private:
INPUT m_keyboard ;
public:
SKeyboardInput( int iScanCode, bool bDown = TRUE, int iTime = 0 ):m_keyboard() {
m_keyboard.type = INPUT_KEYBOARD ;
m_keyboard.ki.wScan = iScanCode ;
m_keyboard.ki.dwFlags = KEYEVENTF_SCANCODE | (bDown ? 0 : KEYEVENTF_KEYUP) ;
m_keyboard.ki.time = iTime ;
m_keyboard.ki.dwExtraInfo = 0 ;
}
public:
DWORD scan() const {
return m_keyboard.ki.wScan ;
}
DWORD time() const {
return m_keyboard.ki.time ;
}
public:
int Send( ) const {
if( m_keyboard.ki.time ) Sleep( m_keyboard.ki.time ) ;
SendInput( 1, const_cast<LPINPUT>( &m_keyboard ), sizeof(INPUT) ) ;
return 0 ;
}
} ;
摘自我以前写的一个程序片段
用类稍微的封装了下,
用法:
SKeyboardInput input( 15, TRUE, 100 ) ; // 扫描码 = 15(tab) 按下, 延时100
SKeyboardInput input2( 15, FALSE, 200 ) ; // 扫描码 = 15(tab) 松开, 延时200
input.Send() ;
input2.Send()
这里使用的是扫描码, 你可以用MapVirtualKey()来进行 扫描 虚拟码之间的转换
- 2 回答
- 0 关注
- 92 浏览
添加回答
举报