Jump to content
  • Advertisement

Archived

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

VisualLR

Slowing down kb input with dinput...

This topic is 6920 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, Ive been working on a dialog that requires some user input, but when I try to write something it''s too fast, if I press the "M" key for one second, four or five "M"s show up.. how can I slow this down without slowing down the whole game? And while Im at it, how do I get lowercase letters with DInput? the way Im getting my kb input is using the KEYDOWN(name, key) (name[key] & 0x80), but then I can only know whether a key is being pressed or not, but not whether it''s upper or lower case... thanks! Luis Sempe
visual@guate.net
http://www.geocities.com/SiliconValley/6276


Share this post


Link to post
Share on other sites
Advertisement
Keyboard input is a little tricky with DirectInput, because it intentionally does not do any of the overhead that Windows does (modifier key states, key repeat intervals, etc.). This means you need to keep track of things that you want yourself. For example, you can maintain a global modifier bitfield and turn bits on and off in response to key-ups and key-downs.

For example:
#define KEY_LSHIFT (1<<0)
#define KEY_RSHIFT (1<<1)
#define KEY_SHIFT (KEY_LSHIFT / KEY_RSHIFT)

unsigned char shiftState;

Now, if you want the shift state, just test (shiftState & KEY_SHIFT). This can obviously be extended to include the Alt and Ctrl keys as well. As for key repeats, you need to respond to CHANGES in key-state instead of just processing the state every time in your loop. You need to add a lastTimeProcessed or wasPressed variable to each element in your key array. Like so...

struct KeyData
{
char state;
bool wasPressed;
};

KeyData keys[256];

// In input processing loop...
for (int i = 0; i < 256; i++ )
{
if (keys.state & 0x80)
{
if (!keys[i].wasPressed)
{
// Process this character...
keys[i].wasPressed = true;
}
}
else
keys[i].wasPressed = false;
}

OR...

struct KeyData
{
char state;
unsigned _int32 lastTimeProcessed;
};

KeyData keys[256];
const unsigned _int32 repeatInterval = WHATEVER;

// In your input processing loop...
for (int i = 0; i < 256; i++)
{
if (keys[i].state & 0x80)
{
if (GetTickCount() - keys[i].lastTimeProcessed > repeatInterval)
{
// Process the key press...
keys[i].lastTimeProcessed = GetTickCount();
}
}
else
keys[i].lastTimeProcessed = 0;
}

Whew, that was a lot of stuff, and that wasn't even getting into buffered input mode. Yeesh. If you have any questions about any of this let me know...




Edited by - Heinzah on 5/4/00 1:44:45 PM

Edited by - Heinzah on 5/4/00 1:45:39 PM

Edited by - Heinzah on 5/4/00 1:46:33 PM

Share this post


Link to post
Share on other sites
Well, I tried your suggestion, but I had some problems, how do I use GetDeviceData(...) to get the keyboard state now? when I used it last time I just passed a char[256] and then I checked the state with it.. I think Im missing something...

thanks for your help



Luis Sempe
visual@guate.net
http://www.spheregames.com


Share this post


Link to post
Share on other sites
Oh, yeah. I forgot that GetDeviceData actually takes the array. Oops! Well, you can separate the array elements...

char keys[256];
bool wasPressed[256]; OR unsigned _int32 lastProcessed[256];

and use them that way. Sorry for that little oversight.

Share this post


Link to post
Share on other sites

  • 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!