Skip to main content
gamedev.net
🔒 Locked

Keyboard Direct Input

Started by Danny Gilbert Aug 16, 2006 at 8:30 AM 2 replies 450+ views
Original Post
Danny Gilbert
Danny Gilbert
Hi ! For my game, the player can use the keyboard. I use Direct Input for the controls of keyboard and joystick. To know if the user press (let's say ESCAPE KEY), I do the following... if( KEYDOWN( DIK_ESCAPE )) { // Escape is down, do something... } to check if the high-bit is = 1. KEYDOWN is a macro define like.... #define KEYDOWN(key) (KeyboardState[key] & 0x80) My problem is that after doing something and going elsewhere in the process, the key is still pressed (bit 8 is still to 1) !!! So my patch is to clear by myself this bit. It works well if I wait some milliseconds. KeyboardState[DIK_ESCAPE] = 0; Sleep(50); Every sample on the web doesn't need to clear this bit !!! I suppose that it clears by itself when the key is up ! Can somebody can explain why I need my patch ? Thank.
dmail
dmail
Quote:
My problem is that after doing something and going elsewhere in the process, the key is still pressed (bit 8 is still to 1) !!!

A keypress lasts longer than a frame (generally). So you will see the key is pressed for multiple frames. This is due to the human reaction time it takes to release the key.
Limitz
Limitz
Yeah,

Direct Input Keyboard only gives you the state of the keyboard, if you want to make it that a keypress is only handled once, you should either go with regular win messages (or build a switch between the two), of build your own queue/buffer. The way you do it right now, dissallows you to use for instance the arrow keys for movement, like: as long as arrow up is pressed move forward. You will only move forward during the first frame it is pressed.

Greetings.
Danny Gilbert
Danny Gilbert
Thanks you all. I will do some other tests and maybe use the WM_KEYDOWN message.

Topic Locked

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

Sign in to reply to this topic.