Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

DirectInput GetDeviceState problem

Started by nicoAnces Sep 27, 2005 at 3:02 PM 1 replies 1.4k views
Original Post
nicoAnces
nicoAnces
I have just started studying OpenGL and basic DirectInput with Astle's book (OpenGL game prog), and I have a problem with the GetDeviceState() function: it seems to always fail...:( Here's my code for the input system....hope someone can help out... thanks

// InpusSystem.h

#define DIRECTINPUT_VERSION 0x0800

#include <dinput.h>

class CKeyboard {
// ******** MEMBER VARIABLES ****************
private:
	LPDIRECTINPUTDEVICE8	m_pDIDev;
	char			m_keys[255];

// ******** METHODS *************************
public:
	CKeyboard (LPDIRECTINPUT8 pDI, HWND hWnd);
	~CKeyboard ();

	void Update ();
	bool KeyDown (char key) {return (m_keys[key] & 0x80) ? true : false;}
	bool KeyUp (char key) {return (m_keys[key] & 0x80) ? false : true;}
};

// **********************************************************************************************************************


class CInputSystem {
// ******** MEMBER VARIABLES ****************
private:
	LPDIRECTINPUT8	m_pDI;
	CKeyboard		*m_keyboard;

// ******** METHODS *************************
public:
	CInputSystem (HINSTANCE appInstance, HWND hWnd);
	~CInputSystem ();

	void Update ();
	bool KeyDown (char key) {return m_keyboard->KeyDown(key);}
	bool KeyUp (char key) {return m_keyboard->KeyUp(key);}
};

// Input System.cpp

#include <windows.h>
#include "inputSystem.h"

// ********************************************************************************
//											INPUT SYSTEM
// ****************************************************************************************************************
CInputSystem::CInputSystem (HINSTANCE appInstance, HWND hWnd) {
	DirectInput8Create(appInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **) &m_pDI, NULL);
	m_keyboard = new CKeyboard(m_pDI, hWnd);
}

CInputSystem::~CInputSystem () {
	delete m_keyboard;
	m_keyboard = NULL;

	m_pDI->Release();
}

void CInputSystem::Update () {
	m_keyboard->Update();
}

// ****************************************************************************************************************
//											KEYBOARD
// ****************************************************************************************************************
CKeyboard::CKeyboard (LPDIRECTINPUT8 pDI, HWND hWnd) {
	if (FAILED(pDI->CreateDevice(GUID_SysKeyboard, &m_pDIDev, NULL))) {
		MessageBox(NULL, "Error creating device.", NULL, NULL);
	}

	if (FAILED(m_pDIDev->SetDataFormat(&c_dfDIKeyboard))) {
		MessageBox(NULL, "Error setting Data Format.", NULL, NULL);
	}
	if (FAILED(m_pDIDev->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {
		MessageBox(NULL, "Error setting cooperative level.", NULL, NULL);
	}

	if (FAILED(m_pDIDev->Acquire())) { 
		MessageBox(NULL, "Error acquiring device.", NULL, NULL);
	}

	for (int i=0;i<255;i++)
		m_keys = 0x00;
}

CKeyboard::~CKeyboard () {
	m_pDIDev->Unacquire();
	m_pDIDev->Release();
}

void CKeyboard::Update () {
	if (FAILED(m_pDIDev->GetDeviceState(sizeof(m_keys), (LPVOID) m_keys))) {
		m_pDIDev->Acquire();
		if (FAILED(m_pDIDev->GetDeviceState(sizeof(m_keys), (LPVOID) m_keys))) {
			MessageBox(NULL, "Error getting Device State.", NULL, NULL);
		}
	}
}

NovaCaine
NovaCaine
Hey nicoAnces,

Is it working when the app first loads and has focus? if you try changing your cooperation levels from DISCL_FOREGROUND to DISCL_BACKGROUND does it work? what is the error returned from GetDeviceState?

If the DISCL_BACKGROUND flag worked, then your problem is in reaquiring your device. what the DISCL_BACKGROUND flag does is allow DirectInput to get information from the device even if the device does not have focus.

Hope this helps
nicoAnces
nicoAnces
I solved the problem (actually YOU did....:))
I did as you said, and the error returned was DIERR_INVALIDPARAMETER....I had passed to the function an array of 255 bytes while it required an array of 256 bytes....how dumd am I....
Thanks a lot for taking the time to answer,
nicoAnces.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.