DirectX11 input device - DirectInput detects mouse and keyboard status

DirectX11 input device - DirectInput detects mouse and keyboard status

1 What is DirectInput

Due to the messaging mechanism of Windows applications, Windows has become an invisible wall between applications and hardware. The message queue records various events, such as mouse movement, keyboard input, or events from the operating system itself. This method of obtaining events is efficient enough for Windows applications, but not fast enough for games. Some developers also turn to using another Windows function, GetAsyncKeyState, to obtain the information they need, which is also possible
Another way to quickly obtain user input through clever use of blocking techniques or devices is to use the DirectInput component provided by the DirectX SDK, which provides a common layer to address the aforementioned issues. In this class, we will learn how to use DirectInput. The DirectInput object is used to provide an interface to access DirectInput devices. The DirectInput device you create allows you to access specified input devices, such as keyboards, joysticks, or other game input devices.

2 How to create a DirectInput object

Note that the first step in using DirectInput is to create a DirectInput object, and the function DirectInput8Create is used for this purpose. Its prototype is as follows:

HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
LPVOID *ppvOut,
LPUNKNOWN punkOuter );
  1. HInst - Application instance handle used to create DirectInput objects
  2. DwVersion - The version number of DirectInput required by the application, and the standard value used for this parameter is DICTINPUT_ VERSION
  3. Riidltf - Interface requires identification, default value IID can be used_ IDrectInput8 is used for this parameter
  4. PpvOut - holds a pointer to the DirectInput object after successful creation
  5. PunkOuter - This parameter is generally set to NULL

Here is a code snippet to create a DirectInput object:

HRESULT hr = DirectInput8Create( hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, ( void** )&directInputObject, 0 );
if FAILED( hr )
{
return false;
}
2 How to create a DirectInput device

Now that you have a valid DirectInput object, you can freely create devices by using the Create Device function:

HRESULT CreateDevice( REFGUID rguid, LPDIRECTINPUTDEVICE *lplpDirectInputDevice, LPUNKNOWN pUnkOuter );
  1. Rguid, using Guid_ SysKeyboard or Guid_ SysMouse is used to identify the use of keyboard or mouse devices.
  2. LplpDirectInputDevice, save the address of the successfully created DirectInput device object.
  3. The last parameter is used to handle the COM interface. Most applications set the last parameter to NULL
    The following code assumes that you want to create a DirectInput device for the keyboard:
LPDIRECTINPUTDEVICE8 keyboardDevice;
HRESULT hr = directInputObject ->CreateDevice( GUID_SysKeyboard, &keyboardDevice, 0 );
if FAILED(hr)
{
return false;
}
3 How to obtain keyboard/mouse information

Before reading input from the keyboard, you need to determine in a simple way which key on the keyboard has been pressed. Below is the macro KEYDOWN provided to check if the key you are viewing has been pressed and simply return true or false:


#define KEYDOWN( name, key ) ( name[key] & 0x80 )

An example of reading input from a keyboard is as follows:


#define KEYDOWN( name, key ) ( name[key] & 0x80 )
char buffer[256];
while ( 1 )
{
keyboardDevice->GetDeviceState( sizeof( buffer ), ( LPVOID )&buffer );
if( KEYDOWN( buffer, DIK_LEFT ) )
{
  // Do something with the left arrow
}
if( KEYDOWN( buffer, DIK_UP ) )
{
  // Do something with the up arrow
}
}

An example of reading button status from a mouse is as follows:


#define BUTTONDOWN(name, key) ( name.rgbButtons[key] & 0x80 )
curX = 320;
curY = 240;
while ( 1 )
{
mouseDevice->GetDeviceState( sizeof ( mouseState ), (LPVOID) &mouseState );
if( BUTTONDOWN( mouseState, 0 ) )
{
  // Do something with the first mouse button
}
if( BUTTONDOWN( mouseState, 1 ) )
{
  // Do something with the up arrow
}
curX += mouseState.lX;
curY += mouseState.lY;
}