C++ 如何使用sendinput函数C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22419038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use sendinput function C++
提问by user3422161
I have no idea about what parameters are inputted even though I saw the sendinput function from msdn.
即使我从 msdn 看到了 sendinput 函数,我也不知道输入了哪些参数。
UINT WINAPI SendInput(
_In_ UINT nInputs,
_In_ LPINPUT pInputs,
_In_ int cbSize
);
What do parameters above mean and what do I need to create for them? Also, type, ki.wScan, ki.time, ki.dwExtraInfo, ki.wVk, ki.dwFlags What do objects above mean and is there any other objects that may be frequently used?
上面的参数是什么意思,我需要为它们创建什么?还有 type, ki.wScan, ki.time, ki.dwExtraInfo, ki.wVk, ki.dwFlags 上面的对象是什么意思,有没有其他可能经常用到的对象?
回答by AndyG
UINT
is an unsigned integer type. _In_
means the parameter is an inputparameter that you send into the function. This is opposed to an outputparameter, which would be something you send in, and the function would fill in.
UINT
是无符号整数类型。_In_
表示参数是您发送到函数中的输入参数。这与输出参数相反,输出参数是您发送的内容,函数将填写。
The LPINPUT
structure is defined as follows:
的LPINPUT
结构定义如下:
typedef struct tagINPUT {
DWORD type;
union
{
MOUSEINPUT mi;
KEYBDINPUT ki;
HARDWAREINPUT hi;
};
} INPUT, *PINPUT, FAR* LPINPUT;
So it looks like a DWORD coupled with a union of some other structures. Refer to WinUser.hfor more.
所以它看起来像一个 DWORD 加上一些其他结构的联合。有关更多信息,请参阅WinUser.h。
DWORD
is a 32-bit unsigned integer (source):
DWORD
是一个 32 位无符号整数(源):
A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal). Because a DWORD is unsigned, its first bit (Most Significant Bit (MSB)) is not reserved for signing. This type is declared as follows: typedef unsigned long DWORD, *PDWORD, *LPDWORD;
DWORD 是 32 位无符号整数(范围:0 到 4294967295 十进制)。因为 DWORD 是无符号的,所以它的第一位(最高有效位 (MSB))不保留用于签名。该类型声明如下: typedef unsigned long DWORD, *PDWORD, *LPDWORD;
MOUSEINPUT
鼠标输入
typedef struct tagMOUSEINPUT {
LONG dx;
LONG dy;
DWORD mouseData;
DWORD dwFlags;
DWORD time;
ULONG_PTR dwExtraInfo;
} MOUSEINPUT, *PMOUSEINPUT, FAR* LPMOUSEINPUT;
KEYBDINPUT
键盘输入
typedef struct tagKEYBDINPUT {
WORD wVk;
WORD wScan;
DWORD dwFlags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KEYBDINPUT, *PKEYBDINPUT, FAR* LPKEYBDINPUT;
HARDWAREINPUT
硬件输入
typedef struct tagHARDWAREINPUT {
DWORD uMsg;
WORD wParamL;
WORD wParamH;
} HARDWAREINPUT, *PHARDWAREINPUT, FAR* LPHARDWAREINPUT;
WORD
, LONG
, ULONG
, and ULONG_PTR
are all well-defined on the MSDN page(See the column on the right)
WORD
、LONG
、ULONG
、 和MSDN 页面ULONG_PTR
上都有明确定义(见右栏)
Here's an example of using SendInput
that can be easily found via Googling (Source):
这是一个使用示例SendInput
,可以通过谷歌搜索(Source)轻松找到:
//
// keystroke.c - Pauses, then simulates a key press
// and release of the "A" key.
//
// Written by Ted Burke - last updated 17-4-2012
//
// To compile with MinGW:
//
// gcc -o keystroke.exe keystroke.c
//
// To run the program:
//
// keystroke.exe
//
// ...then switch to e.g. a Notepad window and wait
// 5 seconds for the A key to be magically pressed.
//
// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
#include <windows.h>
int main()
{
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Pause for 5 seconds.
Sleep(5000);
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press the "A" key
ip.ki.wVk = 0x41; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "A" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
// Exit normally
return 0;
}
The other part of your question:
你问题的另一部分:
Also, type, ki.wScan, ki.time, ki.dwExtraInfo, ki.wVk, ki.dwFlags What do objects above mean
还有,输入ki.wScan, ki.time, ki.dwExtraInfo, ki.wVk, ki.dwFlags 上面的对象是什么意思
I believe you're referring to this code from the MSDN page:
我相信您指的是MSDN 页面中的此代码:
// IMPORTANT: Current keyboard layout 0xf0010413 (Netherland with USA kbd)!!!!!!!
WORD vkCode = 0x36; // '6'
INPUT keyEvent = {0};
keyEvent.type = INPUT_KEYBOARD;
keyEvent.ki.wVk = vkCode;
keyEvent.ki.wScan = MapVirtualKeyEx(vkCode, 0, (HKL)0xf0010413);
SendInput(1, &keyEvent, sizeof(keyEvent));
That code was posted by another use on the page; it's not part of the documentation. The user simply created an LPINPUT
structure named keyEvent
, and then accessed the KEYBDEVENT
part of the structure:
该代码是由页面上的另一个用途发布的;它不是文档的一部分。用户只需创建一个LPINPUT
名为的结构keyEvent
,然后访问KEYBDEVENT
该结构的部分:
KEYBDINPUT ki;