如何使用sendinput函数C++

use*_*161 15 c++

即使我从msdn看到sendinput函数,我也不知道输入了什么参数.

UINT WINAPI SendInput(
_In_ UINT nInputs,
_In_ LPINPUT pInputs,
_In_ int cbSize
);
Run Code Online (Sandbox Code Playgroud)

上面的参数意味着什么,我需要为它们创建什么?另外,键入,ki.wScan,ki.time,ki.dwExtraInfo,ki.wVk,ki.dwFlags上面的对象是什么意思,还有其他可能经常使用的对象吗?

And*_*dyG 22

UINT是无符号整数类型._In_表示参数是您发送到函数的输入参数.这与输出参数相反,输出参数将是您发送的内容,并且函数将填充.

LPINPUT结构定义如下:

typedef struct tagINPUT {
    DWORD   type;

    union
    {
        MOUSEINPUT      mi;
        KEYBDINPUT      ki;
        HARDWAREINPUT   hi;
    };
} INPUT, *PINPUT, FAR* LPINPUT;
Run Code Online (Sandbox Code Playgroud)

所以它看起来像一个DWORD加上一些其他结构的联合.有关更多信息,请参阅WinUser.h.

DWORD是一个32位无符号整数():

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;
Run Code Online (Sandbox Code Playgroud)

KEYBDINPUT

typedef struct tagKEYBDINPUT {
    WORD    wVk;
    WORD    wScan;
    DWORD   dwFlags;
    DWORD   time;
    ULONG_PTR dwExtraInfo;
} KEYBDINPUT, *PKEYBDINPUT, FAR* LPKEYBDINPUT;
Run Code Online (Sandbox Code Playgroud)

HARDWAREINPUT

typedef struct tagHARDWAREINPUT {
    DWORD   uMsg;
    WORD    wParamL;
    WORD    wParamH;
} HARDWAREINPUT, *PHARDWAREINPUT, FAR* LPHARDWAREINPUT;
Run Code Online (Sandbox Code Playgroud)

WORD,, LONG'ULONG , andULONG_PTR`在MSDN页面上都有明确定义(参见右侧栏目)

这是一个使用的例子SendInput,可以通过谷歌搜索()轻松找到:

//
// 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;
}
Run Code Online (Sandbox Code Playgroud)

问题的另一部分:

另外,键入,ki.wScan,ki.time,ki.dwExtraInfo,ki.wVk,ki.dwFlags上面的对象是什么意思

我相信你是指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, &amp;keyEvent, sizeof(keyEvent));
Run Code Online (Sandbox Code Playgroud)

该代码是通过页面上的其他用途发布的; 它不是文档的一部分.用户只需创建一个LPINPUT名为的结构keyEvent,然后访问KEYBDEVENT结构的一部分:

KEYBDINPUT      ki;
Run Code Online (Sandbox Code Playgroud)

  • 如果您立即发送释放事件,使用轮询(例如某些游戏)来确定当前按下的键的应用程序将不会检测到"A"被按下.在我之间添加睡眠(30)解决了这个问题. (3认同)