Platform SDK: DirectX

Immediate Keyboard Data

[C++]

To retrieve the current state of the keyboard, call the IDirectInputDevice7::GetDeviceState method with a pointer to an array of 256 bytes to hold the returned data.

The GetDeviceState method behaves in the same way as the Win32 GetKeyboardState function, returning a snapshot of the current state of the keyboard. Each key is represented by a byte in the array of 256 bytes whose address was passed as the lpvData parameter. If the high bit of the byte is set, the key is down. The array is most conveniently indexed with the DirectInput Keyboard Device Constants. (See also Interpreting Keyboard Data.)

The following code example does something in response to the fact that the ESC key is down:

// LPDIRECTINPUTDEVICE  lpdiKeyboard;  // previously initialized
                                       // and acquired 
 
BYTE  diKeys[256]; 
if (lpdiKeyboard->GetDeviceState(256, diKeys) == DI_OK) 
{ 
  if (diKeys[DIK_ESCAPE] & 0x80) DoSomething(); 
} 
[Visual Basic]

To retrieve the current state of the keyboard, call the DirectInputDevice.GetDeviceStateKeyboard method, passing a DIKEYBOARDSTATE type.

The GetDeviceState method returns a snapshot of the current state of the keyboard. Each key is represented by an element in the array of 256 bytes that makes up the DIKEYBOARDSTATE type. If the high bit of the byte is set, the key is down. The array is most conveniently indexed with the members of the CONST_DIKEYFLAGS enumeration. (See also Interpreting Keyboard Data.)

The following code example determines whether the ESC key is currently being pressed:

' objDIDev is a DirectInputDevice object.
Dim dev As DirectInputDevice
Dim KeyState As DIKEYBOARDSTATE
 
Call objDIDev.GetDeviceStateKeyboard(KeyState)
If (KeyState.Key(DIK_ESCAPE) And &H80) Then
    ' Key is down.
End If