i like to stop a loop by pressing the "s" key
-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
i like to stop a loop by pressing the "s" key
How can i make a repeat ---- until loop which stops when i press the "s" key
Re: i like to stop a loop by pressing the "s" key
in a console application it could be done like this..
There's I'm sure a dozen ways to do a windowed example.. I'm not familiar with the normal way most people would do it with native pb commands so lol here's what Ive come up with.
PS: lol I have my own system using the win api to do this normally so dont pick on me if this is really terrible.
Code: Select all
openconsole()
repeat
key$=inkey()
debug "hello world"
until key$="s"
PS: lol I have my own system using the win api to do this normally so dont pick on me if this is really terrible.
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
Last edited by epidemicz on Fri Jan 07, 2011 10:11 pm, edited 2 times in total.

Re: i like to stop a loop by pressing the "s" key
AddKeyboardShortcut() ?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
Re: i like to stop a loop by pressing the "s" key
thanks epidemicz..
that's it
Wim
that's it
Wim
Re: i like to stop a loop by pressing the "s" key
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
corrected code:
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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
Re: i like to stop a loop by pressing the "s" key
But... i have a problem:
the repeat----until loop calculates for a minute.....and then calculates again
the stopping command "s" or whatever must be seen everywhere in the program...
ts-soft.. will your demo do that job?
wim
the repeat----until loop calculates for a minute.....and then calculates again
the stopping command "s" or whatever must be seen everywhere in the program...
ts-soft.. will your demo do that job?
wim
-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
Re: i like to stop a loop by pressing the "s" key
ts_soft
I did merge your program with mine...
but no result....
I did merge your program with mine...
but no result....
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: i like to stop a loop by pressing the "s" key
If it must halt the 'whole program', then the loop must encompass the whole program.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
Re: i like to stop a loop by pressing the "s" key
it does...but it does not stop when i press a key
Re: i like to stop a loop by pressing the "s" key
The loop is "blocked" because of the calculations? I'd say you would need a thread then.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
Re: i like to stop a loop by pressing the "s" key
Yes, i think that it must be something like that.
If i put a inkey statement in the middel of the most uses loop in the calculation
it does not react either.
So... ... what to do now?
we are talking about the program that will measure the whole day, the signals
coming from the stars... via the soundcard.
It waits until there is a new minute, then measures 1 second, and than wait until
the next minute.
in that waiting loop i put the inkey statement....
the program does not need to stop ... but as usual.. once in the week in case of
problems, you must stop it by hand....
Wim
If i put a inkey statement in the middel of the most uses loop in the calculation
it does not react either.
So... ... what to do now?
we are talking about the program that will measure the whole day, the signals
coming from the stars... via the soundcard.
It waits until there is a new minute, then measures 1 second, and than wait until
the next minute.
in that waiting loop i put the inkey statement....
the program does not need to stop ... but as usual.. once in the week in case of
problems, you must stop it by hand....
Wim
Re: i like to stop a loop by pressing the "s" key
Mayby it's SysTray's you are looking for?
Then you could just have a small icon in the corner for your Logger/Recorder and when you get a "Quit" from the user you just shut down your program in a controlled way.
Then you could just have a small icon in the corner for your Logger/Recorder and when you get a "Quit" from the user you just shut down your program in a controlled way.
The best preparation for tomorrow is doing your best today.
-
- Enthusiast
- Posts: 290
- Joined: Thu Dec 16, 2010 2:05 pm
- Location: Delfzijl ( The Netherlands )
- Contact:
Re: i like to stop a loop by pressing the "s" key
Yes, but i can not program that...
Re: i like to stop a loop by pressing the "s" key
Do not use waiting loops, use timer events instead.
One Main loop which starts the measurement when a timer event occurs:
OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
AddWindowTimer(0, 1, 60000)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Menu
men = EventMenu()
ElseIf Event = #PB_Event_Timer And EventTimer() = 1
StartMeasurement1sec()
Endif
Until Event = #PB_Event_CloseWindow Or men = 777
One Main loop which starts the measurement when a timer event occurs:
OpenWindow(0, 0, 0, 240, 70, "ShortcutGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddKeyboardShortcut(0, #PB_Shortcut_S, 777)
AddWindowTimer(0, 1, 60000)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Menu
men = EventMenu()
ElseIf Event = #PB_Event_Timer And EventTimer() = 1
StartMeasurement1sec()
Endif
Until Event = #PB_Event_CloseWindow Or men = 777
Re: i like to stop a loop by pressing the "s" key
Got a little carried away....
Maybe this skeleton can be something for you to implement?
Maybe this skeleton can be something for you to implement?
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
The best preparation for tomorrow is doing your best today.