Quick Q regarding reading keyboard input

Hello all,

I was wondering what the difference is between:

  • using SDL_GetKeyState to read the current keyb state and
  • using the SDL_KEYDOWN/UP events to handle key presses

I know that the key up/down events only happen when the key is
pressed/released. So, I have some custom code that deals with things like,
“is the key held down”.

I was wondering if I could simplify that code by using the SDL_GetKeyState
stuff. (I’m sure it didn’t exist when I first started using SDL btw :)).

Will SDL_GetKeyState keep reporting a held down key as pressed?
What are the pros/cons of using the key up/down events versus the
GetKeyState method? Are there any performance issues (since I will have to
keep calling it once every tick.)

Cheers,
K.

Heya,

Calling SDL_GetKeyState is essentially “free”. I’m pretty sure it just
returns a pointer to the internal array that sdl uses to keep track of what
keys are up or down.

Using GetKeyState is superior to the keyup/keydown events in my books
because with keyup/keydown you get repeated keydown events for “key repeat”.

You can easily ignore those of course with your own keystate array, but I
like GetKeyState because I can use the array that is already being used
instead of making my own array and maintaining that.

Ever so slightly more efficient I think…hope im not way off base though (:> ----- Original Message -----

From: sdl-bounces+atrix2=cox.net@libsdl.org
[mailto:sdl-bounces+atrix2=cox.net at libsdl.org] On Behalf Of Kostas Kostiadis
Sent: Friday, April 28, 2006 2:00 AM
To: 'sdl at libsdl.org
Subject: [SDL] quick Q regarding reading keyboard input

Hello all,

I was wondering what the difference is between:

  • using SDL_GetKeyState to read the current keyb state and
  • using the SDL_KEYDOWN/UP events to handle key presses

I know that the key up/down events only happen when the key is
pressed/released. So, I have some custom code that deals with things like,
“is the key held down”.

I was wondering if I could simplify that code by using the SDL_GetKeyState
stuff. (I’m sure it didn’t exist when I first started using SDL btw :)).

Will SDL_GetKeyState keep reporting a held down key as pressed?
What are the pros/cons of using the key up/down events versus the
GetKeyState method? Are there any performance issues (since I will have to
keep calling it once every tick.)

Cheers,
K.


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

Kostas Kostiadis wrote:

I was wondering what the difference is between:

  • using SDL_GetKeyState to read the current keyb state and
  • using the SDL_KEYDOWN/UP events to handle key presses

They have different purposes - use whichever fits your need.

If the question is, “Has the user pressed key x since I last checked?”,
use events. (Don’t use SDL_GetKeyState because you might miss short key
presses.)

If the question is, “Is the x key pressed right now?”, use
SDL_GetKeyState. (You can keep your own array of key states and update
it using events, as you seem to be doing now, but why reinvent the wheel?)

If I’m reading the source right, you only need to call SDL_GetKeyState
once, and the pointer you get from it will stay valid forever (it’s a
static variable in SDL_keyboard.c). (The documentation was not clear
about this - I just corrected it.) SDL is updating this array all the
time anyway, you don’t lose any performance by reading it.

-Christian

Alan / Christian,

Thx for your replies…
This is for a game/demo I’m writing, so maintaining my own array for
something SDL is doing already does seem indeed like re-inventing the wheel.

GetKeyState didn’t exist when I originally wrote the part of code that
handles input, but it’s definitely the way forward for what I’m trying to
do.

Cheers,
K.

Message: 12Date: Fri, 28 Apr 2006 02:08:57 -0700
From: atrix2@cox.net (atrix2)
Subject: Re: [SDL] quick Q regarding reading keyboard input
To: “‘A list for developers using the SDL library.
(includesSDL-announce)’”
Message-ID: <000001c66aa3$61c12820$0a00000a at ALAN>
Content-Type: text/plain; charset=“us-ascii”

Heya,

Calling SDL_GetKeyState is essentially “free”. I’m pretty sure it just
returns a pointer to the internal array that sdl uses to keep track of what
keys are up or down.

Using GetKeyState is superior to the keyup/keydown events in my books
because with keyup/keydown you get repeated keydown events for “key repeat”.

You can easily ignore those of course with your own keystate array, but I
like GetKeyState because I can use the array that is already being used
instead of making my own array and maintaining that.

Ever so slightly more efficient I think…hope im not way off base though (:


Message: 13
Date: Fri, 28 Apr 2006 11:48:20 +0200
From: cwalther@gmx.ch (Christian Walther)
Subject: Re: [SDL] quick Q regarding reading keyboard input
To: sdl at libsdl.org
Message-ID: <e2soch$3js$1 at sea.gmane.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Kostas Kostiadis wrote:

I was wondering what the difference is between:

  • using SDL_GetKeyState to read the current keyb state and
  • using the SDL_KEYDOWN/UP events to handle key presses

They have different purposes - use whichever fits your need.

If the question is, “Has the user pressed key x since I last checked?”,
use events. (Don’t use SDL_GetKeyState because you might miss short key
presses.)

If the question is, “Is the x key pressed right now?”, use
SDL_GetKeyState. (You can keep your own array of key states and update
it using events, as you seem to be doing now, but why reinvent the wheel?)

If I’m reading the source right, you only need to call SDL_GetKeyState
once, and the pointer you get from it will stay valid forever (it’s a
static variable in SDL_keyboard.c). (The documentation was not clear
about this - I just corrected it.) SDL is updating this array all the
time anyway, you don’t lose any performance by reading it.

-Christian