   How to improve latency between Left and Right arrow key presses?
  Let's start with a character name Bobo, my self-created protagonist.I want Bobo to move left and right with my arrow keys, so I go ahead and create a script for it to move back and forth in C++ with success.While debugging for problems, I noticed that, if I were to simultaneously press left and right keys pretty fast, Bobo would stop and face in one direction without moving or anything.After multiple tries, I realized that the latency between the left key pressed down and the right key pressed up overlaps (same goes with vice versa, L key up, R key down).I wanted to know some hints/tips on how to reduce the latency or eliminate the overlapping issue. Which means, I wanted to make Bobo have quick reflexes with going left and right, nonstop.Awaiting your replies. :)   
  Before we can help you, you need to provide some more information:Which engine are you using to create, draw, move your character? What language are you programming/scripting in (C++ is not a scripting language). What system/OS are you on? How do you process the keyboard input?   ;  What latency?The "latency" is the amount of time you hold a key down. This doesn't have to do too much with the code.It can be solved of course, but I suggest you to give us a bit more info.What platform?Do you have an event system, or do you poll the keyboard state (if you use a game library/engine foe example)? Or worst: you have a getch/cin like stuff?The cause of your problem is pretty simple.I guess you have something like this in your game loop:if( KeyBoard )    pos--;if( KeyBoard )    pos++;So if the keys are pushed (held down) simultaneously, pos won't change after all.EDIT: yop, I guess you have the getch/cin stuff (so forget the code above). Well I don't know if you can (should) do anything with the latency. For a game, you should handle keyboard differently.   ;Quote:Which engine are you using to create, draw, move your character? What language are you programming/scripting in (C++ is not a scripting language). What system/OS are you on? How do you process the keyboard input?Engine to create, draw, move, etc.: SDLProgramming Language: C++ (By "script", I meant CPP files)OS: Windows Vista Processing Keyboard: Via SDL_Event, using if-else if conditions.Here's my code:It's made into a class, from top to bottom, so there's no main() function.Another thing: I split the frames up into True Frames and Run Frames, so as to control the correct sprites showing up while at constant FPS, making the animation look more slower, but having the gameplay constantly at 30 FPS.#include "Animate.h"#include <SDL.h>void Protagonist::ShowRun(SDL_Surface* object, SDL_Surface* screen){static int TrueFrame;if (status == 1){if (leftRight == 1){RunFrame++;if (RunFrame >= 1 && RunFrame <= 2)TrueFrame = 0;else if (RunFrame >= 3 && RunFrame <= 4)TrueFrame = 1;else if (RunFrame >= 5 && RunFrame <= 6)TrueFrame = 2;else if (RunFrame >= 7 && RunFrame <= 8)TrueFrame = 3;else if (RunFrame >= 9 && RunFrame <= 10)TrueFrame = 4;else if (RunFrame >= 11 && RunFrame <= 12)TrueFrame = 5;else if (RunFrame >= 13 && RunFrame <= 14)TrueFrame = 6;else if (RunFrame >= 15 && RunFrame <= 16)TrueFrame = 7;else if (RunFrame >= 17 && RunFrame <= 18)TrueFrame = 8;else if (RunFrame >= 19 && RunFrame <= 20)TrueFrame = 9;else if (RunFrame >= 21 && RunFrame <= 22)TrueFrame = 10;else if (RunFrame >= 23)RunFrame = 3;offset.x += Velocity.x;SDL_Delay(1000/30);SDL_BlitSurface(object,&Running_Clip[TrueFrame], screen, &offset);}else if (leftRight == 0){RunFrame++;if (RunFrame >= 1 && RunFrame <= 2)TrueFrame = 21;else if (RunFrame >= 3 && RunFrame <= 4)TrueFrame = 20;else if (RunFrame >= 5 && RunFrame <= 6)TrueFrame = 19;else if (RunFrame >= 7 && RunFrame <= 8)TrueFrame = 18;else if (RunFrame >= 9 && RunFrame <= 10)TrueFrame = 17;else if (RunFrame >= 11 && RunFrame <= 12)TrueFrame = 16;else if (RunFrame >= 13 && RunFrame <= 14)TrueFrame = 15;else if (RunFrame >= 15 && RunFrame <= 16)TrueFrame = 14;else if (RunFrame >= 17 && RunFrame <= 18)TrueFrame = 13;else if (RunFrame >= 19 && RunFrame <= 20)TrueFrame = 12;else if (RunFrame >= 21 && RunFrame <= 22)TrueFrame = 11;else if (RunFrame >= 23)RunFrame = 3;offset.x -= Velocity.x;SDL_Delay(1000/30);SDL_BlitSurface(object,&Running_Clip[TrueFrame], screen, &offset);}}}Because of the if-else if conditions, I can't press two keys at the same time, but my laptop can register only one of the keys when both of the keys are down together.I can't seem to test if it works or not.   ;  Sorry to say, but that is one ugly code there.All those fishy if-else stuff could be replaced with a TrueFrame = (RunFrame-1)/2;Any you didn't show the keyboard handling stuff. The thing you posted has nothing to do with that.   ;  Oops! But, about the TrueFrame and RunFrame, those are temporary. I'm not satisfied about the animation, as it currently doesn't look right. Other than using a simple function to solve the animation problem, I need to find a way how to fill up the gaps that were left behind.RunFrames (They are not constant): 1->3, 4->7, 8->9, 10->15, 16->26, 27->30. TrueFrames: 1, 2, 3, 4, 5, 6.Below is the real keyboard input code.#include <SDL.h>#include "Animate.h"void Protagonist::Direction(SDL_Event Event){if (Event.type == SDL_KEYDOWN){if (Event.key.keysym.sym == SDLK_LEFT){leftRight = 0;status = 1;Velocity.x = 5;}else if (Event.key.keysym.sym == SDLK_RIGHT){leftRight = 1;status = 1;Velocity.x = 5;}}else if (Event.type == SDL_KEYUP){if (Event.key.keysym.sym == SDLK_LEFT){status = 0;StandFrame = 0;RunFrame = 0;Velocity.x = 0;}else if (Event.key.keysym.sym == SDLK_RIGHT){status = 0;StandFrame = 0;RunFrame = 0;Velocity.x = 0;}}return;}int Protagonist::get_status(){return status;};  I guess you already know the problem: if you release the key, BOBO will stop (because you coded that way).How to overcome this?There are a lot of ways to achieve that, one way (more precisely hack) to do that, but we need to know what behavior do you expect:For example in this scenario:1. You push (key_down) left // BOBO turns/goes left2. You push right // BOBO turns/goes right3. You release (key_up) right // what should BOBO do? Either stop, or turn left (since left button is still held)?If you want BOBO to stop, the hack I can think of ATM:else if (Event.type == SDL_KEYUP){if (Event.key.keysym.sym == SDLK_LEFT){status = 0;// I don't know what these stuff are,StandFrame = 0;// maybe these should be moved intoRunFrame = 0;// the if() tooif( leftRight == 0 )// BOBO only stops, if (s)he was heading theVelocity.x = 0;// same direction, as the key// so (s)he won't stop, if the opposite key is released}else if (Event.key.keysym.sym == SDLK_RIGHT){status = 0;StandFrame = 0;RunFrame = 0;if( leftRight == 1 )Velocity.x = 0;}}I hope that helps (and certainly hope it works)   ;  Does it work now?   ;  Extensive testing and while having Calculus class...I discovered that, this only reduced the number of times it stops moving when only one key was pressed down. Somewhere by 10s of some folds.However, the problem is still there. I'm now thinking other causes, like it's because of the way keyboards were designed, or how quick my key presses shouldn't be having, or so on.   ;  Sorry, I don't know SDL, so I can only give you a hacky solution instead of a general one.The keyboard keeps sending SDL_KEYDOWN messages if a key is hold (with a particular rate, just like in text editors).This is very easy to track, because SDL_KEYUP messages aren't sent repeatedly. So all you have to to is store the states of the left and right buttons, and check if the button is already pushed:void Protagonist::Direction(SDL_Event Event){if (Event.type == SDL_KEYDOWN){if (Event.key.keysym.sym == SDLK_LEFT && LeftButtonState == 0 ){//button is not pushed yet, so you can do stuffleftRight = 0;status = 1;Velocity.x = 5;LeftButtonState = 1; // 0 means not pushed, 1 means pushed}else if (Event.key.keysym.sym == SDLK_RIGHT && RightButtonState == 0 ){leftRight = 1;status = 1;Velocity.x = 5;RightButtonState = 1;}}else if (Event.type == SDL_KEYUP){if (Event.key.keysym.sym == SDLK_LEFT){if( leftRight == 0 ){status = 0;StandFrame = 0;RunFrame = 0;Velocity.x = 0;}LeftButtonState = 0;}else if (Event.key.keysym.sym == SDLK_RIGHT){if( leftRight == 1 ){status = 0;StandFrame = 0;RunFrame = 0;Velocity.x = 0;}RightButtonState = 0;}}return;}LeftButtonState and RightButtonState has the same scope as status, StandFrame, RunFrame, etc. They need to be initialized with zero.This is a bit of a hack, it's better to store this ButtonState stuff for all buttons (keymap), and those if-else stuff would be better with switch statement.   