Home > Software engineering >  How to capture the keyboard and mouse input
How to capture the keyboard and mouse input

Time:10-17

Capture raw input device information, record the keyboard and the mouse operation,

//register first raw input devices.
 
RAWINPUTDEVICE raw;
Raw. HwndTarget=AfxGetMainWnd () - & gt; M_hWnd;
Raw. UsUsagePage=0 x01;
Raw. UsUsage=0 x02;//mouse raw. UsUsage=0 x06;//keyboard
Raw. DwFlags=RIDEV_INPUTSINK;
if(! RegisterRawInputDevices (& amp; Raw, 1, sizeof (RAWINPUTDEVICE)))
MessageBox (L "registered the keyboard equipment failure," L "registered raw input device");


//class wizard add message response function in the program
 
Void CmagickeyDlg: : OnRawInput (UINT nInputcode, HRAWINPUT HRAWINPUT)
{
If (RIM_INPUT==nInputcode)//what is the meaning of this sentence
{
Return CDialogEx: : OnRawInput (nInputcode hRawInput);
}

//the following obtain input data
LPBYTE LPB=nullptr;
UINT uint_size;
GetRawInputData (hRawInput, RID_INPUT nullptr, & amp; Uint_size, sizeof (the RAWINPUTHEADER));
LPB=new BYTE [uint_size];
If (nullptr==LPB)
{
MessageBox (L "not allocate memory," L "Raw Input Test");
The delete [] LPB;
Return CDialogEx: : OnRawInput (nInputcode hRawInput);
}
If (GetRawInputData (hRawInput, RID_INPUT LPB, & amp; Uint_size, sizeof (the RAWINPUTHEADER))!=uint_size)
{
MessageBox (L "GetRawInputDate dosen" t return correct size!" , L "Raw Input Test");
The delete [] LPB;
Return CDialogEx: : OnRawInput (nInputcode hRawInput);
}

The RAWINPUT raw=* (LPB) (the RAWINPUT *); The delete [] LPB;//delete first, afraid of forget

//here is to capture the data operation
}


Now this can only capture the keyboard or mouse, cannot capture the keyboard and mouse at the same time,
How to set up to capture at the same time the keyboard and mouse?

CodePudding user response:

VS the IDE in don't understand the symbol on the right mouse button, choose to define,

CodePudding user response:

 
RAWINPUTDEVICE raw [2].
Raw [0]. HwndTarget=AfxGetMainWnd () - & gt; M_hWnd;
Raw [0]. UsUsagePage=0 x01;
Raw [0]. UsUsage=0 x02;//mouse
Raw [0]. DwFlags=RIDEV_INPUTSINK;
Raw [1]. HwndTarget=AfxGetMainWnd () - & gt; M_hWnd;
Raw [1]. UsUsagePage=0 x01;
Raw [1]. UsUsage=0 x06;//keyboard
Raw [1]. DwFlags=RIDEV_INPUTSINK;
if(! RegisterRawInputDevices (& amp; Raw [0], 2, 2 * sizeof (RAWINPUTDEVICE)))
MessageBox (L "registered the keyboard equipment failure," L "registered raw input device");


Give it a try, not to,

CodePudding user response:

Ok, thank you zhao4zhong1,

CodePudding user response:

 
RAWINPUTDEVICE raw [2].
Raw [0]. HwndTarget=AfxGetMainWnd () - & gt; M_hWnd;
Raw [0]. UsUsagePage=0 x01;
Raw [0]. UsUsage=0 x02;//mouse
Raw [0]. DwFlags=RIDEV_INPUTSINK;
Raw [1]. HwndTarget=AfxGetMainWnd () - & gt; M_hWnd;
Raw [1]. UsUsagePage=0 x01;
Raw [1]. UsUsage=0 x06;//keyboard
Raw [1]. DwFlags=RIDEV_INPUTSINK;
if(! RegisterRawInputDevices (& amp; Raw [0], 2, 2 * sizeof (RAWINPUTDEVICE)))//cannot register, the array can only register
MessageBox (L "registered the keyboard equipment failure," L "registered raw input device");

CodePudding user response:

reference fanxlin reply: 3/f
, thank zhao4zhong1,

This is a "blind cat with dead mouse"?

CodePudding user response:

The plural Devices not Device?

RegisterRawInputDevices Function

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

The RegisterRawInputDevices function registers The devices that supply The raw input data.

Syntax

BOOL RegisterRawInputDevices (PCRAWINPUTDEVICE pRawInputDevices,
UINT uiNumDevices,
UINT cbSize
);
The Parameters

PRawInputDevices
[in] Pointer to an array of RAWINPUTDEVICE structures that represent the devices that supply the raw input.
UiNumDevices
[in] Number of RAWINPUTDEVICE structures pointed to by pRawInputDevices.
CbSize
[in] the Size, in bytes, of a RAWINPUTDEVICE structure.
The Return Value

TRUE if the function succeeds. Otherwise, FALSE. If the function fails, call GetLastError for more information.


Few

To receive WM_INPUT messages, an application must first register the raw input devices using RegisterRawInputDevices. By default, an application does not receive raw input.

The Function Information

Minimum DLL Version user32. DLL
The Header Declared in Winuser. J h, include Windows. H
The Import library User32. Lib
Minimum operating systems, Windows XP

See Also

Raw Input, RAWINPUTDEVICE WM_INPUT

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

CodePudding user response:

May have to
 if (! RegisterRawInputDevices (& amp; Raw [0], 2, sizeof (RAWINPUTDEVICE))) 

CodePudding user response:

Can, modification, thank you zhao great god,
 
RAWINPUTDEVICE raw [2].
Raw [0]. HwndTarget=AfxGetMainWnd () - & gt; M_hWnd;
nullnullnullnullnullnullnullnullnullnullnull
  • Related