i need to find a simple direct input 9 tutorial , keyboard
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.
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.
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.
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.
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.
http://www.directxtutorial.com/Tutorial9/E-DirectInput/dx9E1.aspx
It's very detailed and easy to understand.
Quote:Original post by NamethatnobodyelsetookSo do window messages.
Depends on the reasons for input. DInput works great for movement and shooting type controls.
Reasons against using DirectInput for keyboard input:
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
Popular Topics
Advertisement