Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use sendinput function C++

Tags:

I have no idea about what parameters are inputted even though I saw the sendinput function from msdn.

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?

like image 736
user3422161 Avatar asked Mar 15 '14 03:03

user3422161


People also ask

How does the sendinput function work?

@JustinVăn how the sendInput function works is that it takes your input object (which is an INPUT structure) and inserts the event that it created in the if, or else statement into the keyboard input stream. ki represents the keyboard input event structure. Here is an explanation of each part of the keyboard input structure.

Is the sendinput function UIPI compliant?

This function is subject to UIPI. Applications are permitted to inject input only into applications that are at an equal or lesser integrity level. The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream.

What is the difference between SendMessage and sendinput?

SendInput does not have an HWND parameter as SendMessage, which means it delivers the events in the application’s (not a window’s) message queue. The SendInput function is a little hard to use. If you used SendMessage before but this is the fist time you use SendInput, you probably write the following codes in order to simulate a mouse click:

How to use sendinput to simulate a mouse click?

The SendInput function is a little hard to use. If you used SendMessage before but this is the fist time you use SendInput, you probably write the following codes in order to simulate a mouse click: You think Input.mi.dx and Input.mi.dy indicate the click point as one of the SendMessage parameters.


1 Answers

UINT is an unsigned integer type. _In_ means the parameter is an input parameter that you send into the function. This is opposed to an output parameter, which would be something you send in, and the function would fill in.

The LPINPUT structure is defined as follows:

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.h for more.

DWORD is a 32-bit unsigned integer (source):

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;

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)

Here's an example of using SendInput that can be easily found via Googling (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

I believe you're referring to this code from the MSDN page:

// 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)); 

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:

KEYBDINPUT      ki; 
like image 163
AndyG Avatar answered Sep 18 '22 18:09

AndyG