End a program with cmd+q problems

Mac OSX specific forum
gekkonier
User
User
Posts: 78
Joined: Mon Apr 23, 2007 9:42 am

End a program with cmd+q problems

Post 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.
gekkonier
User
User
Posts: 78
Joined: Mon Apr 23, 2007 9:42 am

Re: End a program with cmd+q problems

Post 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
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: End a program with cmd+q problems

Post 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
gekkonier
User
User
Posts: 78
Joined: Mon Apr 23, 2007 9:42 am

Re: End a program with cmd+q problems

Post by gekkonier »

Thank you very much for your example, this seems to work very well! :P
gekkonier
User
User
Posts: 78
Joined: Mon Apr 23, 2007 9:42 am

Re: End a program with cmd+q problems

Post by gekkonier »

I filed this as a bug in the bug forum:

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