Page 1 of 1
Closing a window with the space key ?
Posted: Mon Aug 19, 2019 10:56 pm
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
Re: Closing a window with the space key ?
Posted: Mon Aug 19, 2019 11:46 pm
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
Re: Closing a window with the space key ?
Posted: Mon Aug 19, 2019 11:50 pm
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
Re: Closing a window with the space key ?
Posted: Tue Aug 20, 2019 1:13 am
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.

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
Re: Closing a window with the space key ?
Posted: Tue Aug 20, 2019 2:08 am
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.
Re: Closing a window with the space key ?
Posted: Tue Aug 20, 2019 2:13 am
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
Re: Closing a window with the space key ?
Posted: Tue Aug 20, 2019 12:33 pm
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.
Re: Closing a window with the space key ?
Posted: Tue Aug 20, 2019 12:41 pm
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
Re: Closing a window with the space key ?
Posted: Tue Aug 20, 2019 1:35 pm
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