Sign in to follow this  

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

This topic is 3723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 vars
IDirectInput8 *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;
}


// Init
DirectInput8Create(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 loop
unsigned 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 down
if(keyState(keyboard, DIK_ESCAPE)) fct0();
if(keyState(keyboard, DIK_F1)) fct1();
if(keyState(keyboard, DIK_1)) fct2();
if(keyState(keyboard, DIK_A)) fct3();


// Release
diDevice->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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

    Share this post


    Link to post
    Share on other sites

    This topic is 3723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

    If you intended to correct an error in the post then please contact us.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this