Skip to main content
gamedev.net
🔒 Locked

DirectInput Problem

Started by PixolMaster Apr 26, 2003 at 4:03 PM 13 replies 2k views
Original Post
PixolMaster
PixolMaster
Hi all, I''m having a bit of a problem... I''m using DirectX 8''s DirectInput, and I am unable to use the arrow keys (DIK_UP, DIK_DOWN, DIK_LEFT, and DIK_RIGHT). Other keys on the keyboard seem to work just fine, though. The arrow keys, on the other hand, are always "up." Thanks ahead of time for any help. --- Alex Broadwin --- Domini Nocti Games
Willbo
Willbo
Well... How does you check for key function look like and could you support an example where your doing this?
I have no clue at all, sounds very strange to me...

- Patrik Willbo

The Lord says He can get me out of this mess, but He''s pretty sure you''re fucked.
- Stephen (Braveheart)
PixolMaster
PixolMaster
InputSystem.h

    
/****************************************************************************
InputSystem.h

Declaration of a DirectInput class wrapper input system

Author : Dave Astle
Date : 3/5/2001

Written for OpenGL Game Programming
*****************************************************************************/
#ifndef __INPUT_SYSTEM_INCLUDED__
#define __INPUT_SYSTEM_INCLUDED__
#define DIRECTINPUT_VERSION 0x0800

/********************************* Includes *********************************/
#include <dinput.h>


/********************************* Constants ********************************/

#define IS_USEKEYBOARD 1
#define IS_USEMOUSE 2


class CKeyboard
{
public:
CKeyboard(LPDIRECTINPUT8 pDI, HWND hwnd);
~CKeyboard();

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

bool Update();

void Clear() { ZeroMemory(m_keys, 256 * sizeof(char)); }

bool Acquire();
bool Unacquire();

private:
LPDIRECTINPUTDEVICE8 m_pDIDev;

char m_keys[256];
};


class CMouse
{
public:
CMouse(LPDIRECTINPUT8 pDI, HWND hwnd, bool isExclusive = true);
~CMouse();

bool ButtonDown(int button) { return (m_state.rgbButtons[button] & 0x80) ? true : false; }
bool ButtonUp(int button) { return (m_state.rgbButtons[button] & 0x80) ? false : true; }
int GetWheelMovement() { return m_state.lZ; }
void GetMovement(int &dx, int &dy) { dx = m_state.lX; dy = m_state.lY; }

bool Update();

bool Acquire();
bool Unacquire();

private:
LPDIRECTINPUTDEVICE8 m_pDIDev;
DIMOUSESTATE m_state;
};


class CInputSystem
{
public:
bool Initialize(HWND hwnd, HINSTANCE appInstance, bool isExclusive, DWORD flags = 0);
bool Shutdown();

void AcquireAll();
void UnacquireAll();

CKeyboard *GetKeyboard() { return m_pKeyboard; }
CMouse *GetMouse() { return m_pMouse; }

bool Update();

bool KeyDown(char key) { return (m_pKeyboard && m_pKeyboard->KeyDown(key)); }
bool KeyUp(char key) { return (m_pKeyboard && m_pKeyboard->KeyUp(key)); }

bool ButtonDown(int button) { return (m_pMouse && m_pMouse->ButtonDown(button)); }
bool ButtonUp(int button) { return (m_pMouse && m_pMouse->ButtonUp(button)); }
void GetMouseMovement(int &dx, int &dy) { if (m_pMouse) m_pMouse->GetMovement(dx, dy); }
int GetMouseWheelMovement() { return (m_pMouse) ? m_pMouse->GetWheelMovement() : 0; }

private:
CKeyboard *m_pKeyboard;
CMouse *m_pMouse;

LPDIRECTINPUT8 m_pDI;
};

#include "InputSystem.cpp"
#endif //__INPUT_SYSTEM_INCLUDED__



InputSystem.cpp

  

/****************************************************************************
InputSystem.cpp

Implementation of a DirectInput class wrapper input system

Author : Dave Astle
Date : 3/5/2001

Written for OpenGL Game Programming
*****************************************************************************/
#include "InputSystem.h"


/*****************************************************************************
CKeyboard::Constructor

Initializes the DI device
*****************************************************************************/
CKeyboard::CKeyboard(LPDIRECTINPUT8 pDI, HWND hwnd)
{
if (FAILED(pDI->CreateDevice(GUID_SysKeyboard, &m_pDIDev, NULL)))
{
// error processing
}

if (FAILED(m_pDIDev->SetDataFormat(&c_dfDIKeyboard)))
{
// error processing
}

if (FAILED(m_pDIDev->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
{
// error processing
}

if (FAILED(m_pDIDev->Acquire()))
{
// error processing
}

Clear();
} // end CKeyboard::Constructor


/*****************************************************************************
CKeyboard::Destructor

Releases the DI device
*****************************************************************************/
CKeyboard::~CKeyboard()
{
if (m_pDIDev)
{
m_pDIDev->Unacquire();
m_pDIDev->Release();
}
} // end CKeyboard::Destructor


/*****************************************************************************
CKeyboard::Update()

Queries the current state of the keyboard and stores it in the member
variables.
*****************************************************************************/
bool CKeyboard::Update()
{
if (FAILED(m_pDIDev->GetDeviceState(sizeof(m_keys), (LPVOID)m_keys)))
{
if (FAILED(m_pDIDev->Acquire()))
{
return false;
}
if (FAILED(m_pDIDev->GetDeviceState(sizeof(m_keys), (LPVOID)m_keys)))
{
return false;
}
}
return true;
} // end CKeyboard::Update()


/*****************************************************************************
CKeyboard::Acquire()

Acquires the keyboard
*****************************************************************************/
bool CKeyboard::Acquire()
{
Clear();
return (!FAILED(m_pDIDev->Acquire()));
} // end CKeyboard::Acquire()


/*****************************************************************************
CKeyboard::Unacquire()

Unacquires the keyboard
*****************************************************************************/
bool CKeyboard::Unacquire()
{
Clear();
return (!FAILED(m_pDIDev->Unacquire()));
} // end CKeyboard::Unacquire()



/*****************************************************************************
CMouse::Constructor

Initializes the DI device
*****************************************************************************/
CMouse::CMouse(LPDIRECTINPUT8 pDI, HWND hwnd, bool isExclusive)
{
if (FAILED(pDI->CreateDevice(GUID_SysMouse, &m_pDIDev, NULL)))
{
// error processing
}

if (FAILED(m_pDIDev->SetDataFormat(&c_dfDIMouse)))
{
// error processing
}

DWORD flags;
if (isExclusive)
flags = DISCL_FOREGROUND | DISCL_EXCLUSIVE | DISCL_NOWINKEY;
else
flags = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE;


if (FAILED(m_pDIDev->SetCooperativeLevel(hwnd, flags)))
{
// error processing
}

if (FAILED(m_pDIDev->Acquire()))
{
// error processing
}

if (FAILED(m_pDIDev->GetDeviceState(sizeof(DIMOUSESTATE), &m_state)))
{
// error processing
}
} // end CMouse::Constructor


/*****************************************************************************
CMouse::Destructor

Releases the DI device
*****************************************************************************/
CMouse::~CMouse()
{
if (m_pDIDev)
{
m_pDIDev->Unacquire();
m_pDIDev->Release();
}
} // end CMouse::Destructor


/*****************************************************************************
CMouse::Update()

Queries the current state of the mouse and stores it in the member
variables.
*****************************************************************************/
bool CMouse::Update()
{
if (FAILED(m_pDIDev->GetDeviceState(sizeof(DIMOUSESTATE), &m_state)))
{
if (FAILED(m_pDIDev->Acquire()))
{
return false;
}
if (FAILED(m_pDIDev->GetDeviceState(sizeof(DIMOUSESTATE), &m_state)))
{
return false;
}
}

return true;
} // end CMouse::Update()


/*****************************************************************************
CMouse::Acquire()

Acquires the mouse
*****************************************************************************/
bool CMouse::Acquire()
{
return (!FAILED(m_pDIDev->Acquire()));
} // end CMouse::Acquire


/*****************************************************************************
CMouse::Unacquire()

Unacquires the keyboard
*****************************************************************************/
bool CMouse::Unacquire()
{
return (!FAILED(m_pDIDev->Unacquire()));
} // end CMouse::Unacquire()


/*****************************************************************************
CInputSystem::Initialize()

Initializes the input system. isExclusive should be set to true for exclusive
mouse access, false otherwise. Flags should be a combination of
IS_USEKEYBOARD and/or IS_USEMOUSE.
*****************************************************************************/
bool CInputSystem::Initialize(HWND hwnd, HINSTANCE appInstance, bool isExclusive, DWORD flags)
{

//Do something pointless to shut up the compiler (warning)
if(flags!=flags)
flags = flags;
// create the DI object
if (FAILED(DirectInput8Create(appInstance,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void **)&m_pDI,
NULL)))
return false;

if(true)
{
m_pKeyboard = new CKeyboard(m_pDI, hwnd);
if (m_pKeyboard == NULL)
return false;
}
if(true)
{
m_pMouse = new CMouse(m_pDI, hwnd, isExclusive);
if (m_pMouse == NULL)
return false;
}

return true;
} // end CInputSystem::Initialize()


/*****************************************************************************
CInputSystem::Shutdown()

Releases all objects and frees memory.
*****************************************************************************/
bool CInputSystem::Shutdown()
{
UnacquireAll();
if (m_pKeyboard)
{
delete m_pKeyboard;
m_pKeyboard = NULL;
}

if (m_pKeyboard)
{
delete m_pMouse;
m_pMouse = NULL;
}

if (FAILED(m_pDI->Release()))
return false;

return true;
} // end CInputSystem::Shutdown()


/*****************************************************************************
CInputSystem::Update()

Queries the current state of all devices.
*****************************************************************************/
bool CInputSystem::Update()
{
if (m_pKeyboard)
m_pKeyboard->Update();
if (m_pMouse)
m_pMouse->Update();

return true;
} // end CInputSystem::Update()


/*****************************************************************************
CInputSystem::AcquireAll()

Makes sure all input devices are acquired
*****************************************************************************/
void CInputSystem::AcquireAll()
{
if (m_pKeyboard)
m_pKeyboard->Acquire();
if (m_pMouse)
m_pMouse->Acquire();
} // end CInputSystem::AcquireAll()


/*****************************************************************************
CInputSystem::UnacquireAll()

Unacquires all devices
*****************************************************************************/

void CInputSystem::UnacquireAll()
{
if (m_pKeyboard)
m_pKeyboard->Unacquire();
if (m_pMouse)
m_pMouse->Unacquire();
} // end CInputSystem::UnacquireAll()




My use (after initialization and updates and such. Note, those work... I AM able to use this code properly to test for other keys):

  
// Check For Key Presses

if(inputSys->KeyDown(DIK_UP))
MoveEntity(player, BWM_UP);

if(inputSys->KeyDown(DIK_DOWN))
MoveEntity(player, BWM_UP);

if(inputSys->KeyDown(DIK_LEFT))
MoveEntity(player, BWM_UP);

if(inputSys->KeyDown(DIK_RIGHT))
MoveEntity(player, BWM_UP);


--- Alex Broadwin
--- Domini Nocti Games


[edited by - PixolMaster on April 26, 2003 5:46:22 PM]
andresvill
andresvill
I am having the same problem. Actually my code is quite similar to yours... if not the same. But I am using DirectX and OpenGL under MFC....
Willbo
Willbo
I see you are doing this after checking your keys:
MoveEntity(player, BWM_UP);

I hope, for the sake of yourself, that BWM_UP isn't moving something up!
If it does, you have to do what I always do when copieng and pasting the same function over and over again. Put your head in the trashcan, turn it upside down over yourself. Then take of your clothes and jump in an ice cold lake with -20 degrees in it!

Just kidding Check that line. Isn't it going to be something like BWM_UP, BWM_DOWN, BWM_LEFT and BWM_RIGHT? Is that your actual problem?

- Patrik Willbo

The Lord says He can get me out of this mess, but He's pretty sure you're fucked.
- Stephen (Braveheart)


[edited by - Willbo on April 26, 2003 8:20:31 PM]
PixolMaster
PixolMaster
Hehe, the move entity up function is just something that I know works, so I''m using it for debugging purposes. Thanks for the input, though ;-)

--- Alex Broadwin
--- Domini Nocti Games
Willbo
Willbo
Then, it beats me. I can''t seem to find any errors in your code when I''m having a closer look.
I''ll throw it in my compiler tomorow and check the code there if someone more awake hasn''t spotted the error.
But, how do you know that it''s always "up" ??


The Lord says He can get me out of this mess, but He''s pretty sure you''re fucked.
- Stephen (Braveheart)
PixolMaster
PixolMaster
Well, KeyDown always returns false, and there are two states: up and down, therefore I assume it''s always up.

--- Alex Broadwin
--- Domini Nocti Games
Willbo
Willbo
Hmm... I just can''t seem to find anything wrong with your code. It should work as far as I can see.
Try c´hecking the arrow keys by doing something like
if( inputSys->m_pKeyboard->KeyDown( DIK_UP )
MoveEntity...
if( inputSys->m_pKeyboard->KeyDown( DIK_DOWN )
MoveEntity...

Otherwise, I''m all out of ideas...
Maybe I should just throw the code into my compiler :/

The Lord says He can get me out of this mess, but He''s pretty sure you''re fucked.
- Stephen (Braveheart)
kapru
kapru
weird never had ne problem like that read throught your code carefully its probs someting small your not seeing
Dave Hunt
Dave Hunt
Try changing "KeyDown(char key)" to "KeyDown(unsigned char key)".

char is signed, so any key value greater than 127 will be negative...
RhoneRanger
RhoneRanger
Just for grins, try changing

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

to
#define KeyDown(name, key) (name[key] & 0x80)

and have a buffer with key input

char buffer[256];
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Corry
Corry
Well the one thing that strikes me, is this. You do a GetDeviceState with the first parameter of sizeof(yourarray). Now, I can't remember the last time I tried this, but I want to think that sizeof(anyarray) will return 4, since an array is just a pointer to a contiguous section of memory. I could be wrong, but I'd try just plugging in 256 there since you are using a character buffer, char = 1 byte, 256 of them is 256 bytes. Try that and see if it works. Other than that...I'm going to continue going through your code. Just to give you an idea, here is the same line from my code, its a little different because I manually buffer the input so I can do not only a keypressed function but a keyreleased function. Anyways, here is my UpdateKB function...
HRESULT CDirectInputKeyboard::UpdateKB()
{
if (++m_iCurrentBuffer >= BACK)
{
m_iCurrentBuffer = 0;
}
hResult=lpDID->GetDeviceState(256, (LPVOID)m_uiCurrentState[m_iCurrentBuffer]);
if (hResult != DI_OK)
{
if (hResult==DIERR_INPUTLOST)
{
hResult=lpDID->Acquire();
if (hResult != DI_OK)
{
return hResult;
}
hResult=lpDID->GetDeviceState(256, (LPVOID)m_uiCurrentState);
if (hResult != DI_OK)
{
DecodeDIMessage(hResult);
return hResult;
}
}
else
{
return hResult;
}
}
return hResult;
}
Bah, I gotta look up the way everyone posts code in here...

The only other thing I notice which shouldn't matter is your use of the char bool type, rather than the int BOOL type. Since Boolean values are either 0, or non-zero though that shouldn't matter. So, the last thing I would do is put a breakpoint at your inputsystem keydown function, that or do something like this:
char Debug[300];
sprintf(Debug, "To prove that my keyboard object is non-null, I'll print the value, it is %l \n", keyboardpointer);
OutputDebugString(Debug);
Since the key being down relies on the keyboard object being present, AND the key being down. I would bet your problem lies in 1 of those 2 places.


[edited by - Corry on April 28, 2003 9:50:46 AM]
phil_gwyn
phil_gwyn
Did anyone ever solve this problem? - I''m having the same thing using the CInputSystem class from OpenGL game programming.

Replacing sizeof() with 256 didn''t work. I''m a bit of newbie (and have stinking hangover) so I can''t really get my head around the second suggestion in Corry''s post. (for similar reasons I admit to not really understanding the CInputSystem class).

thanks

Topic Locked

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

Sign in to reply to this topic.