The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Issues with Keyboard Events
104809


Joined: 08 May 2015
Posts: 7
Hey Guys,

Im a beginner in C++ and SDL and im writing a game.
Im quite done, but my problem are the Keyboard Events, when i press a key, it sometimes gets not even noticed, sometimes i have to hold it down to get it noticed as a keypress and stuff like that,
so my game isn`t working that good. I don't know whats wrong, so i would be very happy if you guys could help me out;
The "couts" "0 was pressed" and "w was pressed" are not for the game, i just put them in there to see whats wrong, why it won't shoot and so on^^.


Here is the Code of the Function that "recognizes events":
Code:
void CGame::ProcessEvents()
{
   SDL_Event Event;
   if (SDL_PollEvent(&Event))
   {
      if (Event.type == SDL_QUIT)
            m_bGameIsRunning = false;
      if (Event.type == SDL_KEYDOWN)
      {
         switch (Event.key.keysym.sym)
         {
         case(SDLK_SPACE) :
            cout << "0 was pressed" << endl;
            break;
         case(SDLK_w) :
            cout << "w was pressed" << endl;
         }
      }
   }
}


The SDL_PumpEvents() Function is located in g_pFramework-Update() (I think it's needed, pls tell me if its unnecessary^^)

The SDL_QUIT part works pretty smooth, i press the "X" and the window gets closed.
As I said, I am pretty new to SDL please tell me if something could get done better in an other way.

Here is The function where its used in even if i think the problem is in the function:
Code:
void CGame::Run()
{
   while (m_bGameIsRunning == true)
   {
      ProcessEvents();

      g_pFramework->Update();
      g_pFramework->Clear();

      m_pSpriteBackground->Render();

      m_pPlayer->Update();
      m_pPlayer->Render();

      SpawnAsteroids();

      CheckCollisions();

      RenderAsteroids();

      g_pFramework->UpdateWindow();
   }
}


And here is the main- function, but I think the problem is in the function ProcessEvents:
Code:
int main(int argc, char *args[])
{
   if (g_pFramework->Init(1520, 180) == false)
      return 0;

   CGame Game;
   Game.Init();
    Game.Run();

   Game.Quit();

   g_pFramework->Quit();
   g_pFramework->Del();

   return 0;
}


I really hope you guys can help me!
Greetings Maxlof
Christian Knudsen


Joined: 14 Nov 2009
Posts: 37
On a quick glance you should have

Code:
while (SDL_PollEvent(&Event))


instead of 'if', since 'if' will only handle the first event on the queue.
Naith


Joined: 03 Jul 2014
Posts: 158
One thing to note, besides what Christian has written, is to always break out of every switch case, to avoid weird bugs in the switch block. So add 'break;' after/under 'cout << "w was pressed" << endl;' in the SDLK_w case.

Also, if you don't wanna write 'if(Event.type ==' all the time, you can change the Event.type into a switch case aswell.

The fixed event code would then look like this:

Code:

void CGame::ProcessEvents()
{
   SDL_Event Event;

   while(SDL_PollEvent(&Event))
   {
      switch(Event.type)
      {
      case SDL_QUIT:
         {
            m_bGameIsRunning = false;
            break;
         }

      case SDL_KEYDOWN:
         {
            switch(Event.key.keysym.sym)
            {
            case(SDLK_SPACE):
               {
                  cout << "0 was pressed" << endl;
                  break;
               }

            case(SDLK_w):
               {
                  cout << "w was pressed" << endl;
                  break;
               }
            }
         }
      }

      case SDL_KEYUP:
         {

         }

      // Other event types
      // case OtherEventType:
         {

         }
   }
}

104809


Joined: 08 May 2015
Posts: 7
Thanks guys. I didn`t know it should be while(SDL_PollEvent(&Event)).The "if's" are only there for simplification, later on i will go with switch().
But the Problem is still there, Keypressed will only get noticed after several presses, or by holding the button down.

I am out of ideas and can`t find the problem.
104809


Joined: 08 May 2015
Posts: 7
I found the Problem, another Function in the Run() Function was written bad and slowed everything down.