i like to stop a loop by pressing the "s" key
Posted: Fri Jan 07, 2011 9:43 pm
How can i make a repeat ---- until loop which stops when i press the "s" key
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
openconsole()
repeat
key$=inkey()
debug "hello world"
until key$="s"
Code: Select all
;lol this is hacky
OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
Repeat
Event = WaitWindowEvent()
men=EventMenu()
Until Event = #PB_Event_CloseWindow Or men=777
this is incorrect codeepidemicz wrote:Code: Select all
;lol this is hacky OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) AddKeyboardShortcut(0, #PB_Shortcut_S, 777) Repeat Event = WaitWindowEvent() men=EventMenu() Until Event = #PB_Event_CloseWindow Or men=777
Code: Select all
;lol this is hacky
OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Menu : men = EventMenu() : EndIf
Until Event = #PB_Event_CloseWindow Or men = 77
Code: Select all
EnableExplicit
Declare Main()
main() ; Start Main() Prcedure
End ; And close all when shutting down....
;//////////////////////////
Procedure Sampler(MySemaphore)
If TrySemaphore(MySemaphore)
;
;- *** Sampling code
;
;- *** File access code
;
SignalSemaphore(MySemaphore)
; Signal the semapore, this so that next thread can pick it up!
EndIf
EndProcedure
Macro WaitAndBreak()
ForEach MyThread()
WaitThread(MyThread())
Next
Break
EndMacro
Procedure main()
Protected hWind, Icon, i, Semaphore
NewMap MyThread()
Semaphore = CreateSemaphore(1)
; This could just as easely be a Mutex.
; It is just to secure that only one Thread rus ath the time,
; and then only one thread writes to the log-file.
hWind= OpenWindow(0,0,0,200,100,"window",#PB_Window_SystemMenu|#PB_Window_Minimize)
CreatePopupMenu(0)
MenuItem(1,"Quit")
MenuItem(2,"Info")
UsePNGImageDecoder()
Icon = LoadImage(#PB_Any, #PB_Compiler_Home + "Examples\Sources\Data\world.png")
AddSysTrayIcon(0,hWind,ImageID(Icon))
AddWindowTimer(0, 0, 60*1000) ; a 60 sec timer (for the Sampler()).
AddWindowTimer(0, 0, 5*10*1000) ; a 5 minute timer ( for internal Garbage Collection).
Repeat
Select WaitWindowEvent()
Case #PB_Event_Timer
Select EventTimer()
Case 0 ; need to verify which - if more then one...
i=CreateThread(@Sampler(), Semaphore)
If i
MyThread(Str(i))=i ; save all started threadID's.
EndIf
Case 1 ; Just an examle of a simpe garbage collector
ForEach MyThread()
If Not IsThread(MyThread())
DeleteMapElement(MyThread())
EndIf
Next
Default ; Should not be reached!
EndSelect
Case #PB_Event_SysTray
DisplayPopupMenu(0,hWind)
Case #PB_Event_CloseWindow
WaitAndBreak()
Case #PB_Event_Menu
Select EventMenu()
Case 1
WaitAndBreak()
Case 2
MessageRequester("Hi!", "I'm a program.")
EndSelect
EndSelect
ForEver
EndProcedure