Handling keys?

Hello everyone! :smiley:

Okay. What was the code to get a key while it is pressed, instead of
just shooting the event when it’s first pressed?

I tried this, but it doesn’t seem to work: (I key)----------
iPressed = false

while true do

while (SDL.SDL_PollEvent(event) == 0) do
	-- Render the screen
	render()
	-- Move the ball
	moveBall()
   		SDL.SDL_Delay(100)
 	end

if event.type == SDL.SDL_KEYDOWN then

key = event.key.keysym.sym
if key == SDL.SDLK_I then
	iPressed = true
else
	iPressed = false
end
			
			
if iPressed then
	-- Do my stuff here
end

end

OK, I know this isn’t C++, but it should be pretty straight-forward if
you know that… And I know C++, so you can use that in your answers. :stuck_out_tongue:

So what’s the correct code? Thanks! :wink:

L-28C wrote:

Hello everyone! :smiley:

Okay. What was the code to get a key while it is pressed, instead of
just shooting the event when it’s first pressed?

I tried this, but it doesn’t seem to work: (I key)


iPressed = false

while true do

while (SDL.SDL_PollEvent(event) == 0) do
– Render the screen
render()
– Move the ball
moveBall()
SDL.SDL_Delay(100)
end

if event.type == SDL.SDL_KEYDOWN then

key = event.key.keysym.sym
if key == SDL.SDLK_I then
iPressed = true
else
iPressed = false
end

if iPressed then
– Do my stuff here
end
end

OK, I know this isn’t C++, but it should be pretty straight-forward if
you know that… And I know C++, so you can use that in your answers. :stuck_out_tongue:

So what’s the correct code? Thanks! :wink:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

do you have the actual code that doesnt work ? perhaps post a tarball
on the web so we can look at it ?

also, i use a thread for the rendering code instead of a delay.

here is my main loop (still working on the core so this it temporary code):

while(!quit)
{
    SDL_WaitEvent(&event);

    switch (event.type)
    {
        case SDL_KEYUP:
            do_key_up(key_event(event.key.keysym.sym));
            break;
        case SDL_KEYDOWN:
            do_key_down(key_event(event.key.keysym.sym));
            break;
        case SDL_QUIT:
            quit = -1;
    }
}

matt

I’m sorry for having wasted your time, it does work… Well, not really,
but it seems to be a problem with the Lua port… :-/

None of the letter keys work, so I’m stuck with the arrows… Gotta look
into that, but thanks though! :wink:

matt wrote:> L-28C wrote:

Hello everyone! :smiley:

Okay. What was the code to get a key while it is pressed, instead of
just shooting the event when it’s first pressed?

I tried this, but it doesn’t seem to work: (I key)


iPressed = false

while true do

while (SDL.SDL_PollEvent(event) == 0) do
– Render the screen
render()
– Move the ball
moveBall()
SDL.SDL_Delay(100)
end

if event.type == SDL.SDL_KEYDOWN then

key = event.key.keysym.sym
if key == SDL.SDLK_I then
iPressed = true
else
iPressed = false
end

if iPressed then
– Do my stuff here
end
end

OK, I know this isn’t C++, but it should be pretty straight-forward if
you know that… And I know C++, so you can use that in your answers. :stuck_out_tongue:

So what’s the correct code? Thanks! :wink:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

do you have the actual code that doesnt work ? perhaps post a tarball
on the web so we can look at it ?

also, i use a thread for the rendering code instead of a delay.

here is my main loop (still working on the core so this it temporary code):

while(!quit)
{
    SDL_WaitEvent(&event);

    switch (event.type)
    {
        case SDL_KEYUP:
            do_key_up(key_event(event.key.keysym.sym));
            break;
        case SDL_KEYDOWN:
            do_key_down(key_event(event.key.keysym.sym));
            break;
        case SDL_QUIT:
            quit = -1;
    }
}

matt


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Leo M. Cabrera wrote:

I’m sorry for having wasted your time, it does work… Well, not really,
but it seems to be a problem with the Lua port… :-/

Which Lua port are you using? There are a number of OSS projects
that has Lua SDL bindings. It might be a good idea contacting a
list dealing with that particular project, or contact the
author(s) directly.> None of the letter keys work, so I’m stuck with the arrows… Gotta look

into that, but thanks though! :wink:

matt wrote:

L-28C wrote:

Hello everyone! :smiley:

Okay. What was the code to get a key while it is pressed, instead of
just shooting the event when it’s first pressed?

I tried this, but it doesn’t seem to work: (I key)


[snip snip]


Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

I figured it out; it seems it doesn’t support the PollEvent method for
getting keys, so I must use GetKeyState(). It works pretty well IMO. :stuck_out_tongue:

Kein-Hong Man wrote:> Leo M. Cabrera wrote:

I’m sorry for having wasted your time, it does work… Well, not really,
but it seems to be a problem with the Lua port… :-/

Which Lua port are you using? There are a number of OSS projects
that has Lua SDL bindings. It might be a good idea contacting a
list dealing with that particular project, or contact the
author(s) directly.

None of the letter keys work, so I’m stuck with the arrows… Gotta look
into that, but thanks though! :wink:

matt wrote:

L-28C wrote:

Hello everyone! :smiley:

Okay. What was the code to get a key while it is pressed, instead of
just shooting the event when it’s first pressed?

I tried this, but it doesn’t seem to work: (I key)


[snip snip]