Jump to content
  • Advertisement
Sign in to follow this  
rider_eragon

DirectInput Key Press/Key Release

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

Hey all,

Building myself a nice little wrapper for the DirectX SDK & FMOD SDK and would like to add key press and release functions to my DirectInput class. Currently, I've only got a key down function (i.e. if key is down, execute processes).

I've modified my class to include:

class Input
{
BYTE keys[256];
bool pressed[256]; // All set to false in the constructor
public:
int KeyDown(BYTE key);
int KeyPressed(BYTE key);
int KeyReleased(BYTE key);
};

int Input::KeyDown(BYTE key) // This one works without a problem
{
return (keys[key] & 0x80);
}
int Input::KeyPressed(BYTE key)
{
if (!pressed[key])
{
pressed[key] = true;
return (keys[key] & 0x80);
}
return false;
}

int Input::KeyReleased(BYTE key)
{
if (pressed[key])
{
pressed[key] = false;
return (keys[key] & 0x80);
}
return false;
}

void GameUpdate(void)
{
diInput.CheckKeyboardFidelity(); // Checks that the keyboard is working fine and resets the keys
if (diInput.KeyPressed(DIK_SPACE))
{
MessageBox(NULL, NULL, NULL, NULL);
}
}


By my logic, the message box should be shown once when I press the spacebar, yet it isn't shown at all. The same goes for the KeyReleased function too. Is there any way I can modify this so that it does work?

Cheers =] Edited by rider_eragon

Share this post


Link to post
Share on other sites
Advertisement
This doesn't answer your question, but you really shouldn't be using DirectInput for keyboard or mouse input. It's slow, buggy, hard to use, and doesn't work for anyone with a non-English keyboard layout. Using window messages (WM_KEYUP / WM_KEYDOWN / WM_CHAR) is a much better solution.

Share this post


Link to post
Share on other sites

This doesn't answer your question, but you really shouldn't be using DirectInput for keyboard or mouse input. It's slow, buggy, hard to use, and doesn't work for anyone with a non-English keyboard layout. Using window messages (WM_KEYUP / WM_KEYDOWN / WM_CHAR) is a much better solution.


That's not a better solution. I wouldn't recommend using those Windows messages to retrieve keyboard messages. Use RawInput APIs instead, Microsoft recommends using RawInput APIs too

Share this post


Link to post
Share on other sites
Hidden

[quote name='Evil Steve' timestamp='1322827945' post='4889744']
This doesn't answer your question, but you really shouldn't be using DirectInput for keyboard or mouse input. It's slow, buggy, hard to use, and doesn't work for anyone with a non-English keyboard layout. Using window messages (WM_KEYUP / WM_KEYDOWN / WM_CHAR) is a much better solution.


That's not a better solution. I wouldn't recommend using those Windows messages to retrieve keyboard messages. Use RawInput APIs instead, Microsoft recommends using RawInput APIs too
[/quote]

AFAIK Microsoft recommends Windows messages for keyboard and mouse.

Share this post


Link to post
Microsoft recommends WM_MOUSEMOVE for pointer control (cursor) and WM_INPUT when better resolution is needed (such as in FPS games).
http://msdn.microsof...v=vs.85%29.aspx
DirectInput isn't recommended for either (it says it in fact also uses WM_INPUT internally, but in a "worse" way), only for joysticks/gamepads.

Share this post


Link to post
Share on other sites
this might help, my code is a little different from yours so I'll just paste the class. just call the Update function inside your GameUpdate function and use WndProc to get the last character pressed.


private:
IDirectInput8* p_Object;
IDirectInputDevice8* p_KeyboardDevice;
unsigned char p_KeyStates[2][256];
unsigned char *p_CurKeyStates;
unsigned char *p_PrevKeyStates;
char p_LastChar;

public:
char GetLastChar(void) const
{
return p_LastChar;
};

bool IsKeyDown(Keys key) const
{
return (p_CurKeyStates[key] & 0x80) ? true : false;
};

bool IsKeyUp(Keys key) const
{
return (p_CurKeyStates[key] & 0x80) ? false : true;
};

bool IsKeyPressed(Keys key) const
{
return ((p_CurKeyStates[key] & 0x80) && !(p_PrevKeyStates[key] & 0x80)) ? true : false;
};

bool Update(void)
{
if (!p_KeyboardDevice)
{
if (FAILED(p_Object->CreateDevice(GUID_SysKeyboard, &p_KeyboardDevice, 0))) return false;
if (FAILED(p_KeyboardDevice->SetDataFormat(&c_dfDIKeyboard))) return false;
if (FAILED(p_KeyboardDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) return false;
if (FAILED(p_KeyboardDevice->Acquire())) return false;
}

unsigned char *tempKeyStates = p_PrevKeyStates;
p_PrevKeyStates = p_CurKeyStates;
p_CurKeyStates = tempKeyStates;

if (FAILED(p_KeyboardDevice->GetDeviceState(256, p_CurKeyStates)))
{
if (FAILED(p_KeyboardDevice->Acquire())) return false;
}

return true;
};

UINT WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CHAR) p_LastChar = static_cast<char>(wParam);
return uMsg;
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!