Page 1 of 1
on repeat respond to pressed keys or a button
Posted: Mon Jul 25, 2011 2:04 am
by ueliweb
Hei
I would like to interrupt a repeat loop by pressing a key on the keyboard or a button.
but if the loop is running the script does not respond to any user interaction then "command period"
after searching the forum I tried unsuccessfully code sniped like:
Code: Select all
wait 2 milliseconds with messages
Code: Select all
on escapeKey
exit to top
end escapeKey
Code: Select all
if theKey is escape then exit to top
and knows anybody constants/shorts that I can use for the controlling keys (like esc, del, command, ...) in a script. As exsample see 3 code sniped above.
Re: on repeat respond to pressed keys or a button
Posted: Mon Jul 25, 2011 6:22 am
by doobox
hi there,
I was just recently introduced to this kind of thing my self.
Something like this may work for you.
Code: Select all
local sSomeVariable
put true into sSomeVariable
repeatLookalike // this could be on a button or whatever.
on repeatLookalike
// do your stuff
if sSomeVariable is true then
send repeatLookalike to this card in 2 milliseconds // send to the object we are on/in
end if
end repeatLookalike
on escapeKey
put false into sSomeVariable
end escapeKey
Re: on repeat respond to pressed keys or a button
Posted: Mon Jul 25, 2011 1:25 pm
by dunbarx
DooBox has shown you one method, that LC can create a pseudo-loop, allowing access to events.
Within a loop, however, testing for the state of any key is possible. For example:
Code: Select all
on mouseUp
put 0 into msg
repeat until the optionKey is down
add 3 to msg
wait 5
if the commandkey is down then
repeat 1000
add 3000 to msg
wait 5
if the shiftkey is down then exit repeat
end repeat
end if
end repeat
end mouseUp
Put this in a button and click. Watch for a second or so and then press the indicated keys (shift or command). Stop this madness with the optionKey.
NOW THEN. Read up on the "keysDown" function. The world is your oyster.
Craig Newman
Re: on repeat respond to pressed keys or a button
Posted: Tue Jul 26, 2011 7:16 am
by ueliweb
Hei dunbarx and doobox
thanks for your ideas.
I tried to use your code but it looks that it does not works in this case.
your examples wont help me in this issue, but I am also not sure about I implemented it at the right places ...
I will explain a bit more to understand what is coing on in this issue.
all my code are as handlers or functions on the stack script because there where used an different cards (at the moment just the first) on the buttons just are handlers that calls it from there.
pressing test button "ABC ..." starts "playABC" wich loads and plays one finger-spelling-sing-letter after the other until all are over. but now I would like to stop/ pause/ or make some other interaction to this loop ...
but it looks that this repeat dont want hear to anything until it exit by itself and then starts to do all what I pressed in the running time.
The button "X" should stop the repeat off "playABC".
Only "command period" and sometimes pressing "esc" for about 4-5s change to the debugging window where I can stop the script from running.
The "next" and "back" buttons on the screenshot has no functionality in this running mode.
Code: Select all
on playABC
repeat with i = 1 to (the number of lines of the cABC of this stack)
## loads next video & picture to the card
## itself calls 2 other commands that
## - change the number "i" to the coresponding letter
## - load the media files to the card and starts playing.
loadLetterToCardUsingLineNummber i
## wait for the video is finished playing
## because there are different length and waiting needs
waitForVideoPlayTime
## Job to do ## (not fixed yet)
#############
## find a exit "functionality" to stop the loop
## if someone press "escape" key or press the stop button
if sSomeVariable is true then
send repeatLookalike to card 1 in 2 milliseconds // send to the object we are on/in
end if
-- if theKey is the escapeKey then exit to top ## try also the optionKey / commandKey / shiftkey
end repeat
end playABC
Re: on repeat respond to pressed keys or a button
Posted: Tue Jul 26, 2011 7:43 am
by ueliweb
as I try to find a way to react to a key I just copy following code from the dictionary to the stack script:
Code: Select all
on keyDown theKey
if theKey is not a number then beep
else pass keyDown
end keyDown
the beep comes, perhaps I do/ understand something basically wrong ???
by pressing "esc" long it interrupts always with the command "waitForVideoPlayTime" witch is called in the "playABC"
Code: Select all
on waitForVideoPlayTime
-- wait ((the duration of player "signVideo")/30/1000+0.2) seconds
wait until (the currentTime of player "signVideo" = the duration of player "signVideo")
if the duration of player "signVideo" < 10 then
wait 1.5 seconds
end if
wait the cPausePlayerForSec of this stack
end waitForVideoPlayTime
perhaps this are useful information
Re: on repeat respond to pressed keys or a button
Posted: Tue Jul 26, 2011 7:54 am
by SparkOut
Try changing your "wait" statements to include "with messages" at the end.
Eg "wait 2000 milliseconds with messages"
Also, inside any repeat loop that seems to be blocking (or just as a good habit anyway) include within the repeat a line "wait 0 milliseconds with messages". That gives the engine just enough focus to do some regular housekeeping stuff, polling for keys, etc.
Re: on repeat respond to pressed keys or a button
Posted: Sat Jul 30, 2011 7:07 am
by ueliweb
Thanks for all suggestions.
I could not figure out that LC will hear and interact yet to key or button actions while it runs a repeat loop that loads short Videos in order and change content. After that the script hear and runs the interactions. Running on iPhone it also not update the picture together with the video.
Neither in LC developer environment, nor in a Mac standalone and nor in the iOS (Simulator or the real iPhone)
I feel a bit stranded and helpless.
I guess it should be basic functionality and it look so difficult ...

What I do wrong?
If someone will have a look to the hole project file he can download it from here (because it is a bit bigger then 256kb):
http://lataa.ueliisa.net/auttaa/hear_wh ... t_test.zip
If you do so please reconnect in the "Standalone Applications setting" -> "Copy Files" the three Folders (within LC itself should work without no need to do this)
thanks