   [.net] [XNA] keyboard input speed\twitchyness problems
  Well, after hours of StringBuilder studying and general string studying, as well, at times, char stuying while looking at the innards of Key, KeyboardState, Keyboard... etc... etc. I made a semi-functional text processor for user input.Small problem. It's twitchy and excessive. At first, hitting A once may produce "aaaa", sometimes, with a nice good quick, light tap I may get one or two a. So, I figured that it's updating too quickly. I ripped out a time lapse function from XNA Game Studio Creator's Guide. It helped, but it's still chaotic. Now, a single push of a may give me "aa" Which is an improvement from the four to six character spasm.Any ideas? I'm kind of at the end of the road right with my knowledge right now.Here's a portion of the code. WARNING: Some code is in prototype and unpolished. May cause mild blindness.   public void inputText(Microsoft.Xna.Framework.GameTime gameTime)   {  //Microsoft.Xna.Framework.Input.Keyboard keyboard;  Microsoft.Xna.Framework.Input.KeyboardState keyState = new Microsoft.Xna.Framework.Input.KeyboardState();  keyState = Microsoft.Xna.Framework.Input.Keyboard.GetState();  //Microsoft.Xna.Framework.Input.Keys keys;  foreach (var key in keyList)  { if (Timer(gameTime)) {if (keyState.IsKeyDown(key.Key)){    string test = key.Value;    if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.RightShift) || keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift))    {   test = test.ToUpper();    }    textString.Append(test);}if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Space)){    string test = " ";    textString.Append(test);}if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Back)){    if (textString.Length != 0)    {   textString.Remove((textString.Length - 1), 1);    }}if (keyState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter)){    //storedString = currentString;//stores the current for any use    //currentString = "";//cleans out the current memory    //addString = "";//same as above.} }   }  currentString = textString.ToString();   } //This was quickly thrown in for testing. Temporary.   bool resetInterval = false;   double intervalTime = 0;   double previousIntervalTime = 0;   int frameNum = 1;   int numFrames = 2;   bool Timer(Microsoft.Xna.Framework.GameTime gameTime)   {  bool resetInterval = false;  //add time lapse between frames and keep value between 0 & 500 ms  intervalTime += (double)gameTime.ElapsedGameTime.Milliseconds;  intervalTime = intervalTime % 50;    //IntervalTime has been reset so a new interval has started  if (intervalTime < previousIntervalTime) resetInterval = true;  previousIntervalTime = intervalTime;  return resetInterval; }  }
This codeintervalTime = intervalTime % 50;should amount to an interval period of 50ms, so you could still be recording 20 keystrokes per second. Increasing the number 50 would make it take longer for the intervalTime to reset and thus it would take longer for the function Timer to return true again, so you'd be sampling the keyboard less often and get less aaaa's as a result.Keep in mind that this will still somewhat depend on twitchiness of your keypress up to a certain point. You may only keep the key pressed for 100ms, but my grandmother might keep it down for 2000ms before letting go. Depending on your scenario that might not be a big deal, but you could also fashion a keystroke as being a press-and-release, which will work for everyone. Something like this:   // class member outside of function   KeyboardState lastKbs; protected override void Update(GameTime gameTime)   {  KeyboardState kbs = Keyboard.GetState();  if (lastKbs != null)  { // so if it was down and is up now, ie it has been pressed and released  if (lastKbs.IsKeyDown(Keys.B) && kbs.IsKeyUp(Keys.B)) {// do something }  }  lastKbs = kbs;   };  I have this book and I bought it reading good reviews. I think it is very good but would like to know if the solutions to the chapter exercises are available for download?I have downloaded the source code from www.xnachinchilla.com, but haven't found the download to the exercises.Thanks for any helpDon;  If you are doing keyboard for text input you handle your input on a key just up event (i.e. the singular frame where the key was just released) rather than a key down event.  You only respond to key down after some relatively long amount of time has passed (it's like 2sec on average it feels on windows)If XNA doesn't provide a keyJustUp function you just mirror the input state across frames.  So store the current state in a persistant var called oldState.  Each new frame get the current state and compare to the last frame state.  If a key was down but now up, you trigger a KeyJustUp event.  then copy curState to oldState.  repeat each frame-me   ;  If you're running on Windows, it may be easier to hook WH_GETMESSAGE and process WM_CHAR/WM_KEYDOWN/WM_KEYUP. This gives you event-driven keyboard input that follows the user's settings (e.g. with support for accented characters or custom key repeat rates).   ;  Remigius' post is pretty much how it's done. There's no events. The key is either up, or it's down. You just have to store the last keyboard state and then do a comparison.I usually just wrap all the keyboard functions into a handler type class, and then check to see if the key is down, or if it's a NewKey(key k), etc... It's much easier then doing 2 state checks for every key.;  indeed, remigius post is what you really want.what you want is this:is key down and was key NOT down in the last frame? if so, write key.and he shows how to do that in the most simple way.i wrapped these two states into a class myself, so i can quickly KeyPressDown and KeyHoldDown individually (and same for up, as most clicks happen on the Up actually)   