Closing a window with the space key ?

Just starting out? Need help? Post your questions and find answers here.
forgottencoder
User
User
Posts: 12
Joined: Sun Jul 21, 2019 1:46 am

Closing a window with the space key ?

Post by forgottencoder »

Hi everyone

Is it possible to close a window with the space key ?

Code: Select all


Global event

If OpenWindow(0,0,0,800,600,"Close window with <space>",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  Repeat
    event=WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        End
        
   ;   this next code is my invention and it doesn't work     
   ;     
   ;   Case #PB_Event_Keyboard 
   ;     If EventType()=#PB_Key_Space
   ;       End
   ;     EndIf
        
    EndSelect    
    
  ForEver
  
EndIf  
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Closing a window with the space key ?

Post by Kiffi »

take a look at AddKeyboardShortcut():

Code: Select all

Global event

#Window = 0
#ShortcutSpaceEvent = 0

If OpenWindow(#Window, 0, 0, 800, 600, "Close window with <space>", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  AddKeyboardShortcut(#Window, #PB_Shortcut_Space, #ShortcutSpaceEvent)
  
  Repeat
    
    event = WaitWindowEvent()
    
    Select event
        
      Case #PB_Event_CloseWindow
        End
        
      Case #PB_Event_Menu 
        
        Select EventMenu()
            
          Case #ShortcutSpaceEvent
            End
            
        EndSelect    
        
    EndSelect    
    
  ForEver
  
EndIf
Greetings ... Peter

// Edit: WindowEvent -> WaitWindowEvent
Last edited by Kiffi on Tue Aug 20, 2019 1:35 pm, edited 1 time in total.
Hygge
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Closing a window with the space key ?

Post by mk-soft »

Makes no sense because it is not a standard operation.
But this is how it works

Code: Select all


Enumeration MenuItems
  #Menu_Key_Space
EndEnumeration


Global event

If OpenWindow(0,0,0,800,600,"Close window with <space>",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  
  AddKeyboardShortcut(0, #PB_Shortcut_Space, #Menu_Key_Space)
  
  Repeat
    event=WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Menu
        Select EventMenu()
          Case #Menu_Key_Space
            Break
        EndSelect
    EndSelect    
    
  ForEver
  
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
forgottencoder
User
User
Posts: 12
Joined: Sun Jul 21, 2019 1:46 am

Re: Closing a window with the space key ?

Post by forgottencoder »

I continue to search in forum and i start seing a function called GetAsyncKeyState_
and codes like #VK_Control, #VK_F5 ... hum ... what if i can do something like this GetAsyncKeyState_(#VK_SPACE) ... let me try ... it worked ... wtf?
This must be Purebasic deep underground because i can't find this function in purebasic help. :D
Well sharing with you all.

Code: Select all


If OpenWindow(0, 0, 0, 800, 600, "Close window with <space>", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        End
    EndSelect   
    
    If GetAsyncKeyState_(#VK_SPACE)
      End
    EndIf  
    
  ForEver
 
EndIf

User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Closing a window with the space key ?

Post by Paul »

Commands that end with " _ " are API commands so of course you will not find them in the PB Help file, they would be documented by the OS developer ;)

GetAsyncKeyState_ is fine if you are coding something you only want to run on Windows OS.
If you want cross platform then a PB command like AddKeyboardShortcut should be used.
Image Image
forgottencoder
User
User
Posts: 12
Joined: Sun Jul 21, 2019 1:46 am

Re: Closing a window with the space key ?

Post by forgottencoder »

function GetAsyncKeyState_ is reacting to all space presses because if i pressed space inside a string gadget it exits window.

So sharing a possible solution.

Code: Select all


Enumeration
  #sg1
  #sg2
EndEnumeration  

Global eg, event

If OpenWindow(0, 0, 0, 800, 600, "Close window with <space> after pressing <tab>", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  StringGadget(#sg1,5,5,100,20,"Test me1")
  StringGadget(#sg2,5,30,100,20,"Test me2")
  
  SetActiveWindow(0)
  
  Repeat
    event = WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        End
    EndSelect   
    
    If GetAsyncKeyState_(#VK_SPACE) And GetActiveGadget()<>#sg1 And GetActiveGadget()<>#sg2
      End  
    EndIf  
    
    If GetAsyncKeyState_(#VK_TAB)
      SetActiveGadget(-1) ;remove keyboard focus from window
    EndIf  
    
  ForEver
 
EndIf


User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Closing a window with the space key ?

Post by NicTheQuick »

Don't use WindowEvent() if you can avoid it. It creates a high CPU usage and is a bad programming style. Use the Shortcut versions.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Closing a window with the space key ?

Post by BarryG »

For Windows PCs:

Code: Select all

Global event

If OpenWindow(0,0,0,800,600,"Close window with <space>",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  Repeat
    event=WindowEvent()
    Select event
      Case #PB_Event_CloseWindow
        End
      Case #WM_KEYUP
        If EventwParam()=#VK_SPACE
          End
        EndIf
    EndSelect
  ForEver
EndIf  
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Closing a window with the space key ?

Post by Kiffi »

NicTheQuick wrote:Don't use WindowEvent() if you can avoid it. It creates a high CPU usage and is a bad programming style. Use the Shortcut versions.
Good point! I overlooked that. I have corrected my code above.

Greetings ... Peter
Hygge
Post Reply