Need delay while key not pressed

A previous press of ‘L’ key in an outer WHILE loop set lflag to 1. Now execution reaches the inner WHILE loop shown below. The intention is to delay the program in the WHILE loop until the ‘L’ key is pressed - but it doesn’t delay.

Code:

if (lflag==1)
while (event.type != SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_L) // long mode
{ SDL_Delay(500);
}
lflag=0;

Thanks. Bill S.

Not sure I understand what you’re trying to do. Explain a bit more what you’re trying to achieve and I can try coming up with a solution for you.

Naith wrote:

Not sure I understand what you’re trying to do. Explain a bit more what you’re trying to achieve and I can try coming up with a solution for you.

When the program reaches the ‘while’ line (below) I want it to enter the loop and stay there burning time (500 ms per loop cycle) until the ‘L’ key is pressed, at which time it exits the loop.

Code:

while (event.type != SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_L)
{
SDL_Delay(500);
}

The problem is it doesn’t work. It never enters the loop.

I think you want the condition to be (!(event.type == SDL_KEYDOWN &&
event.key.keysym.scancode == SDL_SCANCODE_L)). As is, your condition is
checking for events that happen to L key that aren’t keydown.On Sun, Jul 10, 2016 at 12:33 AM, bilsch01 wrote:

The problem is it doesn’t work. It never enters the loop.


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

Thanks. That is the solution. But now I have another problem: When the program is in the delay loop it doesn’t exit when ‘L’ is pressed. Then after about 4 seconds the screen dims down. I want the frozen display to stay to stay on the screen until the ‘L’ key is pressed, then continue execution.

Bill S.

Your code enters a hard loop where it delays forever. Nothing changes any
variable that would help the loop to end, so your OS is helpfully telling
you there’s a problem.

SDL can only change the ‘event’ variable if you give it control, say, in a
function like SDL_PollEvent(). event.key.keysym.scancode will not
magically change on its own. Use SDL_PollEvent() in your delay loop and
then you’ll be getting somewhere.

Jonny DOn Sat, Jul 9, 2016 at 7:38 PM, bilsch01 wrote:

Thanks. That is the solution. But now I have another problem: When the
program is in the delay loop it doesn’t exit when ‘L’ is pressed. Then
after about 4 seconds the screen dims down. I want the frozen display to
stay to stay on the screen until the ‘L’ key is pressed, then continue
execution.

Bill S.


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

Yes. I did that and I got it working how I want.

Thanks. Bill S.

]Your code enters a hard loop where it delays forever.?? Nothing changes any variable that would help the loop to end, so your OS is helpfully telling you there’s a problem.

How does my OS (ubuntu) know the situation inside the loop if the loop does something every 500 msec? That’s not very often.

Well, I was interpreting what you said about dimming. On Windows, if you
do not interact with the OS (e.g. no SDL_PollEvent() over a span of about 5
seconds, then the OS sees your program in an unresponsive state and prompts
you to do something about it.

Jonny DOn Sun, Jul 10, 2016 at 4:50 AM, bilsch01 wrote:

Quote:

]Your code enters a hard loop where it delays forever.? Nothing changes
any variable that would help the loop to end, so your OS is helpfully
telling you there’s a problem.

How does my OS (ubuntu) know the situation inside the loop if the loop
does something every 500 msec? That’s not very often.


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