[SDL] Keyboard Input Question [Almost Solved, Maybe Not Optimally]

Started by
3 comments, last by signal_ 16 years, 1 month ago
So I tried searching for the answer on here and on google to no avail. If someone could set me in the right direction, I'd be most appreciative. EDIT: I am using C++ language and the SDL library. For the time being I am using DEVC++ IDE, but am switching to the Microsoft C++ very soon. Objective: in my keyboard handling routine I would like to have the key press 'c' go into "Cursor Mode". Upon pressing 'c' a second time the program user exits "Cursor Mode". "Cursor Mode" is simply a game state where an 'X' is placed on the screen that can be incrementally moved across the game space for purposes of interaction. Code Snippet:

if (SDL_PollEvent(&event)) 
     if( event.type == SDL_KEYDOWN )
         if( event.key.keysym.sym == SDLK_c )
              {
              int cursorMode = 1;
              Cursor cursor;
              cursor.initCursor();

              while (cursorMode == 1)
                   {
                   if (SDL_PollEvent(&event)) 
                       if( event.type == SDL_KEYDOWN )
                           if( event.key.keysym.sym == SDLK_c )
                                cursorMode = 0; //setting to 0 exits cursor mode
                   // do cursor stuff here
                   }
              cursor.~Cursor();          
              }



Discussion of Problem: When the program is running I can press 'c' to get into cursor mode; however, when pressed a second time, I cannot exit out of cursor mode. If I change the second statement from the code [if( event.key.keysym.sym == SDLK_c )] to [if( event.key.keysym.sym == SDLK_d )], the 'd' key press will exit from cursor mode! wtf? I know this is probably a basic problem, but I am a beginner and cannot solve it yet. Please help! I will continue to work on the problem in the mean time. Thanks in advance! [Edited by - signal_ on March 16, 2008 1:22:26 PM]
Advertisement
I can't answer your question exactly, but I can guarantee you can avoid a lot of headaches later by separating your logic code from your input code by writing a small wrapper. Doing that will not only solve this problem, but many other potential issues down the road, and make your code a hell of a lot cleaner.
Ok. So I solved it. However I am unsure as to whether this solution is optimal. Also, thanks fd9_ for the advice; it is noted and I always appreciate the insight.

I added a check for the cursorMode flag before I enter "Cursor Mode". This seems to solve it.

My theory on what is/was happening: one single keystroke of 'c' satisfied both if statements (the outer and the nested one). This led to the poor outcome of being inexorably stuck in the nested loop. I am still shaky on the understanding, but I should probably "get it" eventually. Also, a proper explanation from any of you smart coders will be appreciated as well.

Best wishes.


UPDATED Code Snippet:

//initialize cursorMode flagint cursorMode = 1;if (SDL_PollEvent(&event))      if( event.type == SDL_KEYDOWN )         if( event.key.keysym.sym == SDLK_c && cursorMode == 1) // NOTE CHANGE              {// the above if statement checks the cursorMode flag!!!              Cursor cursor;              cursor.initCursor();              while (cursorMode == 1)                   {                   if (SDL_PollEvent(&event))                        if( event.type == SDL_KEYDOWN )                           if( event.key.keysym.sym == SDLK_c )                                cursorMode = 0; //setting to 0 exits cursor mode                   // do cursor stuff here                   }              cursor.~Cursor();                        }



EDIT: So I tried some more things and I think I have fully solved the problem. I found an example online. This example said that a common mistake is to handle only the KEYDOWN event and not handling the KEYUP event. Using this knowledge I was able to formulate a suitable solution; maybe not the most optimal, but it works for me.

[Edited by - signal_ on March 16, 2008 5:21:13 PM]
You seem to be calling SDL_PollEvent() twice in one loop. Each time you call SDL_PollEvent, it retrieves a different event, so you usually don't want it called twice anywhere in your code.

What you'd want might be simular to this:
Cursor cursor;while( SDL_PollEvent(&event) )     if( event.type == SDL_KEYDOWN )     {          if( event.key.keysym.sym == SDLK_c)          {               if(cursor.cursorModeIsOn() == true)                    cursor.turnCursorModeOff()               else //if(cursor.cursorModeIsOn() == false)                    cursor.turnCursorModeOn()          }     }//Do cursor stuff outside of your event poll.
Thank you SotL. I have a better handle on things now.

This topic is closed to new replies.

Advertisement