i need to find a simple direct input 9 tutorial , keyboard

Started by
4 comments, last by Evil Steve 16 years, 6 months ago
ive modified the tutorial of keyboard input from the directx 9 sdk, but i need a simpler tutorial on how to use direct input with the keyboard, to be able to use direct input in my game.
Advertisement
Quote:Original post by Amgatroid
...but i need a simpler tutorial on how to use direct input with the keyboard, to be able to use direct input in my game.


Using DirectInput for keyboard input is very simple:

// Include#define DIRECTINPUT_VERSION 0x0800#include <dinput.h>// global varsIDirectInput8 *di;IDirectInputDevice8 *diDevice;// Helper function, checks if key is pressed down.bool keyState(unsigned char *keyboard, int key){    if(keyboard[key] & 0x80) return true;    else return false;}// InitDirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**) &di, NULL);di->CreateDevice(GUID_SysKeyboard, &diDevice, NULL);diDevice->SetDataFormat(&c_dfDIKeyboard);diDevice->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);// hwnd is the window handle.// In the game loopunsigned char keyboard[256];HRESULT hr = diDevice->GetDeviceState(sizeof(keyboard), (void*) &keyboard);if(hr == DIERR_NOTACQUIRED || hr == DIERR_INPUTLOST) diDevice->Acquire();// Most of the keys have prefix "DIK_". (DIK_A for A)// Examples: Checks, if ECS, F1, 1, A is pressed downif(keyState(keyboard, DIK_ESCAPE)) fct0();if(keyState(keyboard, DIK_F1)) fct1();if(keyState(keyboard, DIK_1)) fct2();if(keyState(keyboard, DIK_A)) fct3();// ReleasediDevice->Unacquire();SAFE_RELEASE(diDevice);SAFE_RELEASE(di);


I hope, I've written everything correct.

Mr X

[Edited by - Mr X on October 21, 2007 3:16:01 PM]
if all you want is to detect keyboard input... direct input is depricated so you should use windows messages instead.

I can't find the right link right now but this is in msdn. I've seen it posted here before so maybe one of the mods can help me prove my statement with a link.
~Mark Schadler
Depends on the reasons for input. DInput works great for movement and shooting type controls.

For text input, use windows messages. This is important for non qwerty keyboards (dvorak, etc) or for international users. Diacriticals are multiple keypresses to make one letter, see here. Windows just handles this stuff for you if you let it.
This tutorial taught me direct input in 30 minutes:

http://www.directxtutorial.com/Tutorial9/E-DirectInput/dx9E1.aspx

It's very detailed and easy to understand.
Quote:Original post by Namethatnobodyelsetook
Depends on the reasons for input. DInput works great for movement and shooting type controls.
So do window messages.

Reasons against using DirectInput for keyboard input:
  • It creates a seperate thread to just read data from the keyboard using raw input (Which you can do yourself with Win32), meaning there's more overhead than just doing it yourself.
  • No support for keyboard repeat at the rate the user has set in the control pannel - you have to re-invent this yourself. Not too bad for game input, but a pain for GUI-style text input.
  • No support for capital letters and shifted characters - you have to detect caps lock / shift being held as well as your normal character (As Namethatnobodyelsetook was saying).
  • No support for caps lock on/off; it's handled by a higher layer than DirectInput, so if someone starts your game with caps lock on, it gets confused.
  • More code to get the same effect as Window Messages.
  • It's recommended that you DON'T use it by Microsoft; Link:
    Quote:Overall, using DirectInput offers no advantages when reading data from mouse or keyboard devices, and the use of DirectInput in these scenarios is discouraged.
  • This topic is closed to new replies.

    Advertisement