Trap ctrl+c with SDL2 Win7


#1

Below is the keyboard and window events code I use. I had read on other forums that SDL_QUIT is suppose to trap ctrl+c. That may have been the case in SDL 1.2 or earlier but it is not working for SDL2 on Win7. How can I trap CTRL+C? on Win7

UPDATE: The code below is working and tested as suggested below

  // main loop
	int done;
    while (!done)
    {
        // process events
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
          switch (event.type) {
            case SDL_QUIT: // signal when window "X" icon pressed
              done = 1;
              break;
								
            case SDL_KEYDOWN:
						  if( event.key.keysym.sym == SDLK_c && // ctrl c pressed
						    (event.key.keysym.mod & KMOD_CTRL) != 0 ) {
                done = 1;
                break;
              }						
            switch (event.key.keysym.scancode)  {
              case SDL_SCANCODE_SPACE:  // space bar pressed
                  printf("  space bar down\n");
                  break;					
              /*  // other key down traps go here
				       case SDL_SCANCODE_xxx:
                  printf("  xxxx key down\n");				
                  break;  */
            }
            break;
								
            case SDL_KEYUP:
                switch (event.key.keysym.scancode)
                {
                case SDL_SCANCODE_ESCAPE :  // escape key pressed
                    printf("  escape key down\n");
                    done = 1;					
                    break;
                case SDL_SCANCODE_SPACE:
                    printf("  space bar up\n");  // space bar released
                    break;		
										
                /*  // other key up traps go here
				        case SDL_SCANCODE_xxx:
                    printf("  xxxx key up\n");				
                    break;  */
                }
                break;
            }
        }
    }

#2

HI jwzumwalt, hope this helps:

int done = 0; // set to 1 to close window
while (!done) { // until done flag triggered
SDL_Event event; // process events

const Uint8 *keyboardState = (const Uint8 *) SDL_GetKeyboardState( NULL );

if ( (KeyboardState[SDL_SCANCODE_LCTRL] || KeyboardState[SDL_SCANCODE_RCTRL]) && keyboardState[SDL_SCANCODE_C] ) {
	done = 1;
}


while (SDL_PollEvent(&event)) {


#3

Yes that helps! I am new to SDL2 but that code is a familiar way of accomplishing tasks in PHP and I will be able to apply that in the future - thanks!.


#4

If you start the game from a terminal, and in the terminal (i.e. when the terminal window, not the game window, has focus) press Ctrl-C, that will generate a SDL_QUIT event.

You could integrate handling Ctrl-C while the game has focus in your event handling code like the following:

    SDL_Event event;                   // process events
    while (SDL_PollEvent(&event)) {
      switch (event.type)  {
        case SDL_QUIT:                 // window close icon
          done = 1;                    // set done flag to quit 
          break;
					
        case SDL_KEYDOWN:
            if( event.key.keysym.sym == SDLK_c
               && (event.key.keysym.mod & KMOD_CTRL) != 0 ) {
                done = 1;
                break;
            }

            switch (event.key.keysym.scancode)  {
                // ... your other code ...


#5

Thanks Mr. Gibson, your addition will make this subject complete for anyone doing a search :slight_smile:


#6

…or you could just do things the proper Windows way, where the quit command is ALT-F4, and leave it at that, and not screw up things for people who try to use CTRL-C to [C]opy highlighted text to the clipboard.

When in Rome, do as the Romans do. When on Windows 7, follow Windows platform conventions rather than Linux ones.


#7

In Linux GUI programs, Ctrl-C is also used for copy to clipboard, it only means “exit program” in the terminal