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