Keyboard events

I am a first time SDL user. I am trying to move a sprite around and notice
that when I press a key and DO NOT RELEASE it I seem to get a SDL_KEYDOWN
event followed immediately by a SDL_KEYUP event. Then when I release the key
I get another SDL_KEYUP event.

I am running Fedora Core 3 on a Toshiba laptop? Is this a OS or keyboard
issie?

Here is the relevant code:

while (1)
{

// Poll for events, and handle the ones we care about.
SDL_Event event;
if (SDL_PollEvent(&event))
{
std::cout << "event.key.keysym.sym is: " << event.key.keysym.sym <<
std::endl;

switch (event.type)
{
case SDL_KEYDOWN:
std::cout << “KEY PRESSED” << std::endl;

case SDL_KEYUP:
std::cout << “KEY RELEASED” << std::endl;
}

}

}

The output:

event.key.keysym.sym is: 100
KEY PRESSED
KEY RELEASED
event.key.keysym.sym is: 100
KEY RELEASED
event.key.keysym.sym is: 102
KEY PRESSED
KEY RELEASED
event.key.keysym.sym is: 102
KEY RELEASED

Is this normal? I was hope to move the sprite while the key was pressed down
and stop it when the key was released.

I am a first time SDL user. I am trying to move a sprite around and
notice that when I press a key and DO NOT RELEASE it I seem to get a
SDL_KEYDOWN event followed immediately by a SDL_KEYUP event. Then
when I release the key I get another SDL_KEYUP event.

Actually, what’s wrong is that you forgot to put a break between your
two case statements.

switch (event.type)
{
case SDL_KEYDOWN:
std::cout << “KEY PRESSED” << std::endl;

case SDL_KEYUP:
std::cout << “KEY RELEASED” << std::endl;
}

The SDL_KEYDOWN case is simply falling through to the SDL_KEYUP case.

b

It’s a bug in your code.

case SDL_KEYDOWN:
??? ??? std::cout << “KEY PRESSED” << std::endl;
break;
??? case SDL_KEYUP:
??? ??? std::cout << “KEY RELEASED” << std::endl;
break;

ALWAYS put breaks after your switch cases otherwise things happen that
are usually difficult to trace!

  • Iain> I am a first time SDL user.? I am trying to move a sprite around and

notice that when I press a key and DO NOT RELEASE it I seem to get a
SDL_KEYDOWN event followed immediately by a SDL_KEYUP event.? Then
when I release the key I get another SDL_KEYUP event.

I am running Fedora Core 3 on a Toshiba laptop?? Is this a OS or
keyboard issie?

Here is the relevant code:

while (1)
? {

??? // Poll for events, and handle the ones we care about.
??? SDL_Event event;
??? if (SDL_PollEvent(&event))
??? {
??? std::cout << "event.key.keysym.sym is: " <<
event.key.keysym.sym << std::endl;
???
??? switch (event.type)
??? {
??? case SDL_KEYDOWN:
??? ??? std::cout << “KEY PRESSED” << std::endl;
??? ???
??? case SDL_KEYUP:
??? ??? std::cout << “KEY RELEASED” << std::endl;
??? }
???
??? }
???
? }

The output:

event.key.keysym.sym is: 100
KEY PRESSED
KEY RELEASED
event.key.keysym.sym is: 100
KEY RELEASED
event.key.keysym.sym is: 102
KEY PRESSED
KEY RELEASED
event.key.keysym.sym is: 102
KEY RELEASED

Is this normal?? I was hope to move the sprite while the key was
pressed down and stop it when the key was released.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

[…]

   switch (event.type)
   {
   case SDL_KEYDOWN:
       std::cout << "KEY PRESSED" << std::endl;

   case SDL_KEYUP:
       std::cout << "KEY RELEASED" << std::endl;
   }

Your break; statements are missing… ;)On 9/21/05, Michael Whitman wrote:

  • SR

Michael Whitman wrote:

I am a first time SDL user. I am trying to move a sprite around and
notice that when I press a key and DO NOT RELEASE it I seem to get a
SDL_KEYDOWN event followed immediately by a SDL_KEYUP event. Then when
I release the key I get another SDL_KEYUP event.

I am running Fedora Core 3 on a Toshiba laptop? Is this a OS or
keyboard issie?

Here is the relevant code:

while (1)
{

// Poll for events, and handle the ones we care about.
SDL_Event event;
if (SDL_PollEvent(&event))
{
  std::cout << "event.key.keysym.sym is: " << event.key.keysym.sym 

<< std::endl;

  switch (event.type)
  {
  case SDL_KEYDOWN:
      std::cout << "KEY PRESSED" << std::endl;

You need a brake here.> case SDL_KEYUP:

      std::cout << "KEY RELEASED" << std::endl;
  }
 
}

}

The output:

event.key.keysym.sym is: 100
KEY PRESSED
KEY RELEASED
event.key.keysym.sym is: 100
KEY RELEASED
event.key.keysym.sym is: 102
KEY PRESSED
KEY RELEASED
event.key.keysym.sym is: 102
KEY RELEASED

Is this normal? I was hope to move the sprite while the key was pressed
down and stop it when the key was released.

Michael Whitman wrote:

Is this normal? I was hope to move the sprite while the key was pressed down
and stop it when the key was released.

It’s perfectly normal, because you forgot to terminate your switch cases
with break =). The key down event falls through to the key up event.

  • Gerry