   SDL Input Key Response Problem
  Ok, so I'm trying to make it so that when a key is held down that the player constantly moves in that direction, rather than having to press the key each time for him to move one incriment of distance.So if I held down the right key he would constantly move across the screen rather than move a little bit and stop.I have googled and searched for hours and tried:if(SDL_EnableKeyRepeat(1, SDL_DEFAULT_REPEAT_INTERVAL) == -1)    {   return false;    }but it didnt work.My code for key input response and moving is://Player Input Handling Functionvoid Player::inputHandler(){    //If a key was pressed    if( event.type == SDL_KEYDOWN )    {   //Adjust the velocity   switch( event.key.keysym.sym )   {  case SDLK_UP: yVel -= PLAYER_HEIGHT / 2; break;  case SDLK_DOWN: yVel += PLAYER_HEIGHT / 2; break;  case SDLK_LEFT: xVel -= PLAYER_WIDTH / 2; break;  case SDLK_RIGHT: xVel += PLAYER_WIDTH / 2; break;  }    }    //If a key was released    else if( event.type == SDL_KEYUP )    {   //Adjust the velocity   switch( event.key.keysym.sym )   {  case SDLK_UP: yVel += PLAYER_HEIGHT / 2; break;  case SDLK_DOWN: yVel -= PLAYER_HEIGHT / 2; break;  case SDLK_LEFT: xVel += PLAYER_WIDTH / 2; break;  case SDLK_RIGHT: xVel -= PLAYER_WIDTH / 2; break; }  }}//Player Moving Functionvoid Player::move(){    //Change the x position/offset of the player based on the change in velocity    x += xVel;   //If the player's x is about to leave the screen    if( ( x < 0 ) || ( x + PLAYER_WIDTH > SCREEN_WIDTH ) )    {   //move back   x -= xVel;   }   //Change the y position/offset of the player based on the change in velocity    y += yVel;   ////If the player's y is about to leave the screen    if( ( y < 0 ) || ( y + PLAYER_HEIGHT > SCREEN_HEIGHT ) )    {   //move back   y -= yVel;   }}and in the main loop:  //Handle input to the player  player.inputHandler();  //Move the player based on the input  player.move();Any help would be greatly appreciated![Edited by - spark1313 on August 1, 2009 11:54:31 PM]
  That's best handled by using something likeUint8 *keystates;//in inputHandlerkeystates = SDL_GetKeyState(NULL);...if (keystates[SDLK_UP]){    yVel -= PLAYER_HEIGHT / 2;}else if (...){...}That way you check whether a key is down or not directly, and do not have to touch key repeat   ;  Hmm, I tried it like that earlier but couldn't get it working either.Now that I know more than I did yesterday, once I get back from vacation today I'll try it out and let you know how it went.Thanks for the response, be back soon.   ;  Okay, so I just tried:void Player::inputHandler(){    if(keystate[ SDLK_UP ])    {   yVel -= PLAYER_HEIGHT/2;    }    if(keystate[ SDLK_DOWN ])    {   yVel += PLAYER_HEIGHT/2;    }    if(keystate[ SDLK_LEFT ])    {   xVel -= PLAYER_HEIGHT/2;    }    if(keystate[ SDLK_RIGHT ])    {   xVel += PLAYER_HEIGHT/2;    }}And the player blinked around the screen and landed in a different position on every arrow key press. Then I looked over my move function, and noticed that the velocity would never equal 0. So I changed this:void Player::move(){    //Change the x position/offset of the player based on the change in velocity    x += xVel;   //If the player's x is about to leave the screen    if( ( x < 0 ) || ( x + PLAYER_WIDTH > SCREEN_WIDTH ) )    {   //move back   x -= xVel;   }   xVel = 0;   //Change the y position/offset of the player based on the change in velocity    y += yVel;   ////If the player's y is about to leave the screen    if( ( y < 0 ) || ( y + PLAYER_HEIGHT > SCREEN_HEIGHT ) )    {   //move back   y -= yVel;   }   yVel = 0;}And it functioned exactly the same as the other one did, where I had to press it each time.Then, after that I began testing the program itself and made a major discovery. I don't think the game is being refreshed properly or there might be a fps issue or something, because whenever I move my mouse around while holding the arrow key, it works perfectly and the player moves properly and constantly as I am trying to achieve. But, when the mouse is not moving around constantly while I am holding down the arrow key, the player only moves one step.Anyone know what could possibly be causing this problem?   ;  AHA!!!!! I'm so happy, I found the error myself =)So, the error wasn't in the key response functions, it was in the structure of my main.In the event loop, aka:    while(SDL_PollEvent(&event))   { there are only two things that are supposed to take place: the input function and the quit functionI had my entire main loop and all of my functions inside of the pollevent loop, which made it so the screen did not update as it should and it was searching for events that shouldn't exist.To anyone with this error in the future, make sure that if you can't get your key responses to work correctly, make sure that it is the only event(aside from quit) in the loop. The idea for looking at this code-bit came from a previous post that is still around, so I'm posting my solution to help people in the future.   