Archived

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

Quitting Application with DirectInput

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

Hi All! I used the DX wizard to start my program, but I disabled the part that exits the program when the user presses ESC. I''ve set it up so that when the user is at the title screen, if they press the space bar, it starts the game. If they''re in the game, the ESC key should take them back to the title screen. If they''re at the title screen, pressing ESC should exit. But here''s the problem: When the user presses ESC in the game, it exits to the title screen and then closes the program. I''m pretty sure what''s happening is that it detects the ESC key as being down and exits to the title screen, but it all happens so fast, that when it gets to the title screen, the ESC key is still down so it exits out of the program... Any suggestions as to how I can prevent this from happening? I''ve tried manually setting the state of that keypress to false when I tell it to exit to the title screen, but that didn''t make any difference... Rattlehead

Share this post


Link to post
Share on other sites
You can use an extra bool variable called eg. escdown
Every time you scan the keystate you set escdown at the end of your loop, if state[DIK_ESC] > 0, if it''s 0 then they key is not pressed and you set it to false.
Then in your title screen you only exit when the ESC is pressed and escdown is false.

Share this post


Link to post
Share on other sites
Keep 2 arrays for the keystates, one current, and one for the last frame. Before you get the new state, copy the current to the last one. Then just make a few functions like
KeyPressed(), if CurrentState[ESC] && !LastState[ESC] then return true;
KeyStillDown(), if CurrentState[ESC] && LastState[ESC] return true;
KeyUp(), if !CurrentState[ESC] && LastState[ESC] return true;

and so on.

Share this post


Link to post
Share on other sites
Definitely do the last way in a DirectInput keyboard wrapper. It''s really easy to implement (just a memcpy) and works perfectly for checking "KeyDown" (state) as well as "KeyPressed" (change up->down, or even vice versa) type events.

Share this post


Link to post
Share on other sites
Well, while I do like the way it makes it similar to windows keydown, keyup and keypress events, I''m working on a super simple game (really just to get familiar with DX) and I don''t want to add more complexity than I absolutely have to.

At present, i''m hardcoding the input and I only expect to handle less than 20 keys. Actually, closer to 12...

Rattlehead

Share this post


Link to post
Share on other sites
You could use buffered keyboard data (from IDirectInputDevice8::GetDeviceData) rather than immediate data (from IDirectInputDevice8::GetDeviceState). Immediate data only gives you the current state of each key, but buffered data gives you a buffer filled with all key state changes since you last checked. You would then exit (or do whatever) when you get data that says "Esc key has been released".

Buffered data also prevents the problem of missing very brief keypresses that happen between state checks of immediate data. In your current system, if you check the state of a key, then the user very quickly presses and releases a key before you get a chance to check again, you miss the keypress.

Look at the code in the "keyboard" sample that comes with the SDK for an example of how to do both immediate and buffered data.

Share this post


Link to post
Share on other sites