Results 1 to 6 of 6

Thread: Detect key or button press in SDL

  1. #1
    Join Date
    Jun 2009
    Posts
    3,960

    Detect key or button press in SDL

    I have a code:
    Code:
    SDL_Event event;
    while(continue){
    /*full instructions*/
    quitOrEsc(&continue, event);
    }
    With the function quitOrEsc:

    Code:
    /*Procedure: verified if the ESC key is pressed or if it was an event SDL_QUIT (v)
                 without blocking the execution of the program
      Data: Address varible loop and variable of the event.
      EB: zero of the variable if either event is detected.
      Preconditions: have initialized SDL
    */
    void quitOrEsc(int *continue, SDL_Event event)
    {
      SDL_PollEvent(&event);
      switch(event.type){
        case SDL_QUIT:
          *continue = 0;
           break;
        case SDL_KEYDOWN:
          switch (event.key.keysym.sym){
            case SDLK_ESCAPE:
              *continue = 0;
              break;
          default:
            break;
          }
           break;
      default:
        break;
      }
    }
    Unfortunately I'm pressing the escape key or clicking on the cross but still it won't leave it, do you know why? How will the program come to know whether I have clicked any key or button?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Detect key or button press in SDL

    I do not see what you used for the event to pass by value to your function quitOrEsc, otherwise it looks to me like ok. You can put breakpoints to see if you get in the functions that interest and draw.

    Do you code this for your office? Because the code that you do not show us might contain a continuous code which may be important for us to solve your problem.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Detect key or button press in SDL

    If that's your problem lies in the "supportSurV ()" where do you already SQL_PollEvent, so that the following function "quitOrEsc()" will have nothing to put under tooth .

    And I see that a loop with a wait time of 5 ms (200 fps anyway). On Windows, if you want a short time, must go nag at helping "while (getticks () - old <5). The timer under Windows on an accuracy of 10 to 15ms, or just good for the 50 to 60 fps.

    I just see more detail on your event management and its not too much. In general we do a single call SQL_PollEvent or SQL_WaitEvent and one switch on "event.type". After you can call other functions in the branches of the switch.

  4. #4
    Join Date
    Jun 2009
    Posts
    3,960

    Re: Detect key or button press in SDL

    Yes it is the same program.

    For the expectation that I change the value depending on the number of calculations that the program must perform. It serves me just to run slowly so as not to go too fast, so I do not care for accuracy.

    To appeal to SDL_WaitEvent I do when? Before the while? After I'm just a switch on the event.type while (so in my position)?

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Detect key or button press in SDL

    It's not complicated, however, provided that there is only one call to SDL_PollEvent () throughout your program.

    So call this function once and transmert structure "event" at your office supportSurV () or quitOrEsc (), style (main.c):

    Code:
    if (SDL_PollEvent(&event))
    {
        supportSurV(&vector, event);
        quitOrEsc(&continue, event);
    }
    And of course, remove the appeal to SDL_PollEvent () in the two sub-functions.

  6. #6
    Join Date
    Jun 2009
    Posts
    3,960

    Re: Detect key or button press in SDL

    Thank you very much

    Another question, how to use the debugger Code:: Blocks?

    In Project-> Build option I have ticked "Produce debugging symbols" and I put a stop point in the program, but when I compile in debug launch in software that launches normally, it does not stop the breakpoint ....

    Because I have a bug and I really do not understand where it comes. If I really find it I'll post the sources updated maybe you can still save me.

Similar Threads

  1. Desktop Computer only beeps when i press power button
    By Panchu in forum Hardware Peripherals
    Replies: 6
    Last Post: 02-01-2011, 06:10 AM
  2. Automating Button Press with Hardware
    By Blesseds in forum Hardware Peripherals
    Replies: 3
    Last Post: 31-12-2010, 01:13 PM
  3. Zune player freezes when i press any button
    By Author in forum Portable Devices
    Replies: 3
    Last Post: 07-10-2009, 09:50 PM
  4. Actions depending on the length of the press of a button
    By Elijah in forum Software Development
    Replies: 3
    Last Post: 16-03-2009, 10:40 AM
  5. Impossible to press a button to install windows xp
    By Jaden in forum Operating Systems
    Replies: 2
    Last Post: 28-11-2008, 03:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,450,066.07386 seconds with 17 queries