Page 1 of 1

End a program with cmd+q problems

Posted: Fri Aug 09, 2013 9:26 am
by gekkonier
I'm a little lost ;)


Test1:
In this small example I can quit with cmd+q

Code: Select all

OpenWindow(0,0,0,600,800,"test")
Debug "test"
Repeat
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        quit = 1
      Case #PB_Event_Menu
        Select EventMenu()
          Case -1
            quit = 1
        EndSelect
    EndSelect
  Until event = 0
Until quit
Test2:
Here I'm able to do it also with cmd+q

Code: Select all

InitSprite()

OpenWindow(0,0,0,600,800,"test")
OpenWindowedScreen(WindowID(0),0,0,600,800)

Repeat
  
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        quit = 1
      Case #PB_Event_Menu
        Select EventMenu()
          Case -1
            quit = 1
        EndSelect
    EndSelect
  Until event = 0

  ClearScreen(RGB(0,255,0))
  FlipBuffers()
  
Until quit

As soon as i try with the keyboard lib (i would like to program a windowed game, that also can be quitted with cmd+q) it would not quit.

Test3:
This program cannot be terminated with cmd+q.

Code: Select all

InitKeyboard()
InitSprite()

OpenWindow(0,0,0,600,800,"test")
OpenWindowedScreen(WindowID(0),0,0,600,800)

Repeat
  
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        quit = 1
      Case #PB_Event_Menu
        Select EventMenu()
          Case -1
            quit = 1
        EndSelect
    EndSelect
  Until event = 0
  
  
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Escape)
    quit = 1
  EndIf
  

  ClearScreen(RGB(0,255,0))
  FlipBuffers()
  
Until quit
Can anyone tell my why? I think it's something with the keyboard library, but I don't get it. Thank you very much.

Re: End a program with cmd+q problems

Posted: Fri Aug 09, 2013 1:17 pm
by gekkonier
If i debug this, the special keys like cmd, alt, ctrl & co would not be recognised by KeyboardInkey(), so I can't do a check on cmd+q with the keyboard lib.
There has to be a solution ;)

Code: Select all

InitKeyboard()
InitSprite()

OpenWindow(0,0,0,600,800,"test")
OpenWindowedScreen(WindowID(0),0,0,600,800)

Repeat
  
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        quit = 1
      Case #PB_Event_Menu
        Select EventMenu()
          Case -1
            quit = 1
        EndSelect
    EndSelect
  Until event = 0

  ExamineKeyboard()
  
  test.s = KeyboardInkey()
  If Len(test) > 0
    Debug test
  EndIf

  ClearScreen(RGB(0,255,0))
  FlipBuffers()
  
Until quit

Re: End a program with cmd+q problems

Posted: Fri Aug 09, 2013 5:43 pm
by Shardik
gekkonier wrote:If i debug this, the special keys like cmd, alt, ctrl & co would not be recognised by KeyboardInkey(), so I can't do a check on cmd+q with the keyboard lib.
There has to be a solution ;)
I can confirm that <Cmd> + <q> don't work in PB 5.11 and PB 5.20 Beta 8 when using a WindowedScreen with WindowEvent() and ExamineKeyboard(). Until this bug is fixed you may use a workaround which I have integrated into your Test3 example. This workaround is taken from a mouse and keyboard handler posted by wilbert.

Code: Select all

EnableExplicit

#NSCommandKeyMask = 1 << 20
#NSKeyDown        = 10

Define CurrentEvent.I
Define event.I
Define quit.I
Define SharedApplication.I

SharedApplication = CocoaMessage(0, 0, "NSApplication sharedApplication")

InitKeyboard()
InitSprite()

OpenWindow(0,0,0,600,800,"test")
OpenWindowedScreen(WindowID(0),0,0,600,800)

Repeat
 
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        quit = 1
      Case #PB_Event_Menu
        Select EventMenu()
          Case -1
            quit = 1
        EndSelect
    EndSelect
  Until event = 0

  CurrentEvent = CocoaMessage(0, SharedApplication, "currentEvent")

  If CurrentEvent
    If CocoaMessage(0, CurrentEvent, "type") = #NSKeyDown
      If CocoaMessage(0, CurrentEvent, "keyCode") = 12
        If CocoaMessage(0, CurrentEvent, "modifierFlags") & #NSCommandKeyMask
          quit = 1
        EndIf
      EndIf
    EndIf
  EndIf
 
  ExamineKeyboard()

  If KeyboardPushed(#PB_Key_Escape)
    quit = 1
  EndIf
 
  ClearScreen(RGB(0,255,0))
  FlipBuffers()
 
Until quit

Re: End a program with cmd+q problems

Posted: Fri Aug 09, 2013 7:57 pm
by gekkonier
Thank you very much for your example, this seems to work very well! :P

Re: End a program with cmd+q problems

Posted: Fri Aug 16, 2013 10:23 am
by gekkonier
I filed this as a bug in the bug forum:

http://www.purebasic.fr/english/viewtop ... 24&t=56016