The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
need delay while key not pressed
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
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.
Naith


Joined: 03 Jul 2014
Posts: 158
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.
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
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);
    }
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
The problem is it doesn't work. It never enters the loop.
need delay while key not pressed
Michael Fairley
Guest

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:
Quote:
The problem is it doesn't work. It never enters the loop.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
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.
need delay while key not pressed
Jonny D


Joined: 12 Sep 2009
Posts: 932
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 D





On Sat, Jul 9, 2016 at 7:38 PM, bilsch01 wrote:
Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
Yes. I did that and I got it working how I want.

Thanks. Bill S.
Re: need delay while key not pressed
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
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.
need delay while key not pressed
Jonny D


Joined: 12 Sep 2009
Posts: 932
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 D




On Sun, Jul 10, 2016 at 4:50 AM, bilsch01 wrote:
Quote:



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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org