DXInput question

Started by
6 comments, last by balog 21 years, 5 months ago
I''m using the DXInput control in a game I''m writing. The problem i''m having is this. On the menu screen, I want the user to be able to use the up/down arrow keys to move the menu cursor over the menu options. I want to make it so the user has press the up/down key each time they want to move. In other words, I want each key press to only have one action. Does anyone know an easy way to do this? Now before someone says something like, use the Form KeyDown/Up event, I don''t have a form to do this on. Everything is created dynamically. I also don''t really want to use a timer for something as simple as this. I hope someone can help. Thanks, Balog.
Advertisement
I'm not exactly familiar with DXInput (I use my own implementation), but here's what you do...

Remember what keys are pressed...in an array or variable. Since you only care about Up/Down, you could make two Booleans...

private
UpKeyPressed, DownKeyPressed: Boolean;

then in your main timer event do:

if UpKeyPressed and not (isUp in DXInput.Keys) then
MoveToPreviousMenuItem
else if DownKeyPressed and not (isDown in DXInput.Keys) then
MoveToNextMenuItem;

UpKeyPressed := (isUp in DXInput.Keys);
DownKeyPressed := (isDown in DXInput.Keys);


And voila, you have created yourself an "onKeyUp" event (basically). So the user has to release the up/down keys to navigate.

I'm not sure if it's really DXInput.Keys, but I know that DelphiX' DirectInput component has a "set of" all keys that are currently down.


[edited by - Harry Hunt on November 4, 2002 2:27:38 AM]
Here''s how I do it: I have three keyboard input arrays:

char keyState[256];
char keyStatePrev[256];
char keyJustOn[256];

Now, keyState I use just like regular and is for the current frames input. keyStatePrev is the keyboard input state for the previous frame (just copies keyState before I get the new input each frame). Now that you have both the current frame''s input and the previous frame''s input, all you have to do is XOR them together to get the "just pressed" input which will only be on for one frame. It works like this: Say we are looking at the UP key. In both arrays the UP slot will be ''0'' until the UP key is pressed. It will then be ''1'' in the keyState array and ''0'' in the keyStatePrev. The next frame (assuming that the UP key is still being pressed) will result in both arrays having a ''1'' in the UP slot. The XOR function returns true if two values are different and false if they are the same. Also, you will want to make sure that the keyState array has a ''1'' in it, because when the user stops pressing the UP key, the keyState will have a ''0'' in it and the keyStatePrev will have a ''1'' in it for one frame. Here is my code for building the keyJustOn array:


  // Get the "just on" keystatefor(int i = 0; i < 256; i++){    keyJustOn[i] = ((keyState[i] ^ keyStatePrev[i]) && (keyState[i] & 0x80));}  


Now, if you want something to happen (in your case you when you want the user to move on a menu) all you have to do is only move when the UP or DOWN key of the keyJustOn array is set. Hope this helps.
Out of curiosity, how are you doing this? Since DirectInput requires the HWND of a window in order to get input, how are you managing to do it without a form?
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
I create a hidden window at runtime, but because of the way I have structured the game classes, I don''t have or want access to it.
Could you not use Application.Handle for it, even if you didn''t have any forms (assuming you''re still using the Forms unit, of course.)
Hi Alistair.

I haven''t tried it with Application.Handle, but i''m sure it would work.

I''m actually using the game state machine architecture that''s on your site. Works brilliantly. As well as the Game Screen singleton class, I have created a Game Input class as well.

Keep up the good work on your site.
quote:Original post by balog
Hi Alistair.

I haven''t tried it with Application.Handle, but i''m sure it would work.

I''m actually using the game state machine architecture that''s on your site. Works brilliantly. As well as the Game Screen singleton class, I have created a Game Input class as well.

Keep up the good work on your site.


Thanks! I''ll try to get more stuff for my site finished soon (I have several half-finished projects on the go). It''s nice to know that some of my stuff is proving useful.

This topic is closed to new replies.

Advertisement