Jump to content
  • Advertisement
Sign in to follow this  
AKofSpades

User Typing

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

Im making a 2d rpg using DirectX and need the user to enter his name. I thought about doing this in a traditional style, where I have the keyboard drawn on the screen and the user selects each letter using the arrow keys. But, how would I just have a text box where the user enters his name using the keyboard?

Share this post


Link to post
Share on other sites
Advertisement
It will differ depending on what language you use.

I'm most comfortable with C# (XNA), and in that case during the Update() method, check each individual key pressed thought the Keyboard.GetState() method and add the letters that way. They are checked the same place the arrows would be checked.

I would also investigate as to whether I could use the ascii codes of pressed keys so I could write less code and for easy maintainability.

So something like this:
public void Update()
{
KeyboardState ks = Keyboard.getState();
// check what keys are being pressed in ks
//entryString += (char)key;
// Check for backspace
}


[Edited by - Thoughtless on July 29, 2008 10:33:33 PM]

Share this post


Link to post
Share on other sites
Probably also need some kind of timer to elapse before checking for a new key down. If you do it every update loop and the game is running at 60 FPS what seems like one key press to the user will fill the buffer with a bunch of the same key.

Share this post


Link to post
Share on other sites
to get a keypress 'event', just store the KeyboardState and compare the old with the actual one.


class Keyboard
{
KeyboardState LastState;
KeyboardState CurrentState;

public void Update(KeyboardState NewState)
{
LastState = CurrentState;
CurrentState = NewState;
}

bool KeyGetsDown(Key key)
{
return LastState.IsKeyDown(key) == false && CurrentState.IsKeyDown(key) == true);
}

bool KeyGetsUp(Key key)
{
return LastState.IsKeyDown(key) == true && CurrentState.IsKeyDown(key) == false);
}

bool KeyIsDown(Key key)
{
return CurrentState.IsKeyDown(key) == true);
}

bool KeyIsUp(Key key)
{
return CurrentState.IsKeyDown(key) == false);
}
}


void GameUpdate(...)
{
keyboard.Update(Keyboard.GetState());

if(keyboard.KeyGetsDown(Key.Enter))
{
LoadLevel(); // load level when user presses enter
}
}



be warned, this is pseudo-code. but i've encapsulated my keyboard, my mouse and keypad keys like that. helps a lot for ui-development.

this would not allow you to press a key for a long time that it continues to type the letter. but, besides that, it works very well.

would not work directly that well for a textbox, but the data is there, you now have to extract it.. (matching the pressed-keys array in both states against each-other to only get the new ones.. should be doable :) espencially with linq )

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!