GameInput.
Keyboard input
The GetKeyState method retrieves scan code and virtual key data for each pressed key on a keyboard. This input data is then returned as a variable-length array of GameInputKeyState structs. A pressed key is one that’s held down at the time that the keyboard reported an input. Keyboards vary in how frequently they report input, typically ranging from 125 Hz to 1,000 Hz. Keyboard state is reported separately for each individual keyboard that’s connected to the system. Rudimentary keyboard state is meant for mapping keyboard input to game actions, such as movement or firing a weapon. It’s not meant for regular text input and other APIs that are used for that purpose.Reading key state
Use the following code to implementGameInput for keyboards and mice in your title.
stateArray passed in to GetKeyState is larger than the number of pressed keys, the surplus array elements aren’t written out or zeroed out. Conversely, if stateArray is smaller than the number of pressed keys, the array is filled and the surplus pressed keys are missed.
GetKeyCount keeps the maximum size of the array small. The maximum size can be queried via the IGameInputDevice interface.
Note that the codePoint member specifies the glyph that the system believes is printed on the associated keycap, based on the current keyboard layout. It’s intended for displaying a representation of the key in an input mapping interface, not for building strings of text input.
Mouse input
The GetMouseState method retrieves movement and button state for a mouse, which is returned in a single GameInputMouseState struct as shown in the following code. Like keyboard state, mouse state is reported separately for each individual mouse that’s connected to the system. <!—As with keyboard input, the CreateAggregateDevice method on theIGameInput interface is used to get the combined system mouse state.> —>
Reading mouse movements
Mouse movement is reported natively as a series of delta values, rather than as an absolute position. This isn’t compatible with the polling nature ofGetCurrentReading, as periodically asking for the current mouse reading is likely to miss several intermediate deltas.
Rather than using GetNextReading to walk through potentially dozens of historical mouse readings and adding up the deltas, they’re accumulated into a virtual positionX and positionY value. From there, deltas are calculated by subtracting the positionX and positionY value from the previous readings that were obtained. Individual deltas are accessible when iterating through all intermediate readings, or through the accumulated deltas when intermediate readings have been skipped.
The positionX and positionY values are the sum of all movement deltas. These values don’t correlate with screen-space coordinates in any way. The accumulated delta value is only for the mouse events that a process receives while it has input focus.
