Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sirSolarius

DirectInput problems

This topic is 5221 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

For some reason, DirectInput isn''t responding to me pressing keys. I''m trying to trap the user pressing the escape key, but in my program the conditional never returns true, even when I''m holding down the key. Here is my code:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	////////////////// Enable memory leak detection ///////////

	#ifdef _DEBUG
		_CrtDumpMemoryLeaks();
		_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif
	//////////////////////////////////////////////////////////


	// Start by creating my class and window

	WindowManager *wnd=WindowManager::getInstance();
	wnd->CreateClass("Kutatas", WindowsMsgHandler, hInstance);
	wnd->SpawnWindow("Kutatas Engine", 800, 600);
	wnd->SetAsGLWindow();
	wnd->setFullScreen(true);

	InputManager *mainInput=InputManager::getInstance();

	g_state=RUNNING;

	MSG msg;		// Message for recieving Windows messages


	while(g_state == RUNNING)
	{
		mainInput->Update();
		
		if(mainInput->keyDown(VK_ESCAPE))
			ExitProgram();

		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if(g_state==PAUSED)
			g_state=RUNNING;
	}
	
	return 1;	
}
And then the InputManager code:
// InputManager.cpp: implementation of the InputManager class.

//

//////////////////////////////////////////////////////////////////////


#include "InputManager.h"
#include "WindowManager.h"
#include "ErrorManager.h"

InputManager *InputManager::instance;

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


InputManager::InputManager()
:m_mouseSensitivity(500), m_invertedMouseAxis(0)
{
	if (FAILED(DirectInput8Create(WindowManager::getInstance()->getHInstance(), 
								DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_lpdi, NULL)))
	{
		ErrorManager::SendError("Could not create DInput system!", true);
	}

	// create the keyboard

	if (FAILED(m_lpdi->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL)))
	{ 
		ErrorManager::SendError("Could not create keyboard device!", true); 
	}

	if (FAILED(m_keyboard->SetDataFormat(&c_dfDIKeyboard)))
	{ 
		ErrorManager::SendError("Could not set the data format of the keyboard!", true); 
	}

	if (FAILED(m_keyboard->SetCooperativeLevel(WindowManager::getInstance()->getWindow(), 
												DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
	{ 
		ErrorManager::SendError("Could not set the cooperative level of the keyboard!", true); 
	}

	if (FAILED(m_keyboard->Acquire()))
	{ 
		ErrorManager::SendError("Could not acquire the keyboard!", true); 
	}


	/// now create the mouse


	if (FAILED(m_lpdi->CreateDevice(GUID_SysMouse, &m_mouse, NULL)))
	{ 
		ErrorManager::SendError("Could not create mouse device!", true); 
	}

	if (FAILED(m_mouse->SetDataFormat(&c_dfDIMouse)))
	{ 
		ErrorManager::SendError("Could not set the data format of the mouse!", true); 
	}

	if (FAILED(m_mouse->SetCooperativeLevel(WindowManager::getInstance()->getWindow(), 
												DISCL_FOREGROUND | DISCL_EXCLUSIVE)))
	{ 
		ErrorManager::SendError("Could not set the cooperative level of the mouse!", true); 
	}

	if (FAILED(m_mouse->Acquire()))
	{ 
		ErrorManager::SendError("Could not acquire the mouse!", true); 
	}
}

InputManager::~InputManager()
{

}

InputManager *InputManager::getInstance()
{
	if(!instance)
		instance=new InputManager();
	return instance;
}

bool InputManager::keyDown(int key)
{
	if(key>255 || key < 0)
		return false;
	return (m_keyBuffer[key] & 0x80);
}

void InputManager::Update()
{
	if(m_keyboard->GetDeviceState(sizeof(m_keyBuffer),(LPVOID)&m_keyBuffer) == DIERR_INPUTLOST)
	{
		m_keyboard->Acquire();
	}

	if(m_mouse->GetDeviceState(sizeof(m_mouseState),(LPVOID)&m_mouseState) == DIERR_INPUTLOST)
	{
		m_mouse->Acquire();

	}
}

bool InputManager::mouseButtonDown(eMouseButton button)
{
	return (m_mouseState.rgbButtons[button] & 0x80);
}

float InputManager::getMouseX()
{
	if(m_invertedMouseAxis)
		return(-(m_mouseState.lX/m_mouseSensitivity));
	return (m_mouseState.lX/m_mouseSensitivity);
}

float InputManager::getMouseY()
{
	if(m_invertedMouseAxis)
		return(-(m_mouseState.lY/m_mouseSensitivity));
	return(m_mouseState.lY/m_mouseSensitivity);
}

float InputManager::getMouseZ()
{
	return(m_mouseState.lZ);
} 

void InputManager::Acquire()
{
	m_mouse->Acquire();
	m_keyboard->Acquire();
}

void InputManager::Unacquire()
{
	m_mouse->Unacquire();
	m_keyboard->Unacquire();
}

void InputManager::Destroy()
{
	if(m_mouse)
	{
		// unacquire the devices

		m_mouse->Unacquire();
		m_mouse->Release();
		m_mouse=0;
	}

	if(m_keyboard)
	{
		m_keyboard->Unacquire();
		m_keyboard->Release();
		m_keyboard=0;
	}
	if(m_lpdi)
	{
		m_lpdi->Release();
		m_lpdi=0;
	}

	delete instance;
	instance=0;
}
Here is the .h for the InputManager, btw:
/////////////////////////////////////////////////////////////////////////////

//	INPUTMANAGER

/////////////////////////////////////////////////////////////////////////////

//

// AUTHOR: ERIK GOLDMAN

// DATE:  6-3-04

// DESC: Uses DirectInput to read keyboard and mouse data- storing it or passing it to functions

// NOTES:

// TOFIX:

/////////////////////////////////////////////////////////////////////////////


#if !defined(AFX_INPUTMANAGER_H__3F2C2AA0_4FD0_4B65_A9CB_0B1958D48E57__INCLUDED_)
#define AFX_INPUTMANAGER_H__3F2C2AA0_4FD0_4B65_A9CB_0B1958D48E57__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <DX/dinput.h>

enum eMouseButton
{
	LEFT_BUTTON=0,
	RIGHT_BUTTON,
	MIDDLE_BUTTON
};

class InputManager  
{
protected:
	InputManager();
	~InputManager();
	static InputManager *instance;

	LPDIRECTINPUTDEVICE m_keyboard, m_mouse;
	LPDIRECTINPUT m_lpdi;

	char m_keyBuffer[256];

	DIMOUSESTATE2 m_mouseState;

public:
	static InputManager *getInstance();

	void Update();

	void Destroy();

	bool keyDown(int key);
	bool mouseButtonDown(eMouseButton button);

	float getMouseX();
	float getMouseY();
	float getMouseZ();

	void Acquire();
	void Unacquire();

	bool m_invertedMouseAxis;
	float m_mouseSensitivity;
};

#endif // !defined(AFX_INPUTMANAGER_H__3F2C2AA0_4FD0_4B65_A9CB_0B1958D48E57__INCLUDED_)


Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Couple of questions.

1. Have you validated that the buffer is actually getting filled with "anything"
2. Have you validated that the device is even working?
3. Can you validate that any other key works or just not escape?
4. have you tried it in foreground,exclusive mode?
5. is it just returning false then?

Sorry its really late and my brain is too tired to think atm to read too much into the code. But if you can answer those things and validate that at least then, if still doesn''t work, tomorrow I will be happy to figure out what is wrong.

Sorry, I promise to look at it more closely tomorrow.

Share this post


Link to post
Share on other sites
One thing that jumps out at me is that you''re using VK_ESCAPE. That''s only meant for Win32 messages. For DirectInput, you''re supposed to use DIK_ESCAPE.

For a complete list of all the keys that DirectInput supports, do a search for "Keyboard Device Enumerated Type" in the DX docs. It will be the first topic that shows up.

Also, if you use the DirectInput enumerated keys, there''s no reason to check whether the value of the key press is in the right range as you currently do in your "keyDown" method. Hard coding those values could be a problem if Microsoft later on decides to change the values of those constants.

neneboricua

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!