Page 1 of 1

Wait until commandKey is down only works once

Posted: Fri Aug 22, 2025 8:21 pm
by Fermin
Hi, I’m facing a problem I thought I had already solved, but now I’m stuck again and can’t make it work.
I’m trying to run three actions, each triggered by a separate press of the Command key:

Code: Select all

on mouseUp
   wait until the commandKey is down 
   put "AAAAA"

   wait until the commandKey is down 
   put "BBBBB"

   wait until the commandKey is down
   put "CCCCC"
end mouseUp

But it runs through all steps quickly after the first press, as if the key remains down and the condition is always true.

I’ve tried approaches like repeat while, repeat until, is down, is up... but nothing seems to work.
Could someone please help?

Thanks in advance!

Re: Wait until commandKey is down only works once

Posted: Fri Aug 22, 2025 9:47 pm
by SWEdeAndy
Either you can use a counter variable, to just perform different actions depending on the number of times the command key has been down and up.

Or introduce a wait period in between key presses:

Code: Select all

   wait until the commandKey is down 
   put "AAAAA"
   wait 1 secs with messages
   
   wait until the commandKey is down 
   put "BBBBB"
   wait 1 secs with messages
   
   wait until the commandKey is down
   put "CCCCC"
The reason your code example does not work as you intend is that as soon as the "commandKey is down" condition is satisfied, it takes like a millisec to process the rest of the code, so no human can be fast enough to release the command key before the script is already at the end. So all you see is CCCCC... :)

Re: Wait until commandKey is down only works once

Posted: Fri Aug 22, 2025 10:08 pm
by Fermin
Great, thank you so much, SWEdeAndy. And since this is about synchronizing musical moments where a second is sometimes too long, I assume ticks might work. I've tried wait 10 with messages and it seems to be working. :) :) :)

Re: Wait until commandKey is down only works once

Posted: Sun Aug 24, 2025 3:39 am
by dunbarx
The timing comment is spot on. Know that if that is in place, it is not required to include "with "messages". It is not necessary.

But all in all, the method itself, though it works, seems fragile. I do not know the working intent of this handler, but this also works:

Code: Select all

on idle
   if the commandKey is down then 
   switch fld 1
      case ""
         put "AAAAA" into fld 1
         break
      case "AAAAA"
         put "BBBBB" into fld 1
         break
      case "BBBBB"
         put "CCCCC" into fld 1
         break
   end switch
   end if
end idle
But apart from being so old fashioned and unreliable as to make Jacque cringe, it indicates that the method itself needs to be rethought. It just does not seem sound to set up a "queue" of actions in this way. I would love to hear more from the OP.

Craig

Re: Wait until commandKey is down only works once

Posted: Sun Aug 24, 2025 5:41 pm
by Fermin
Thank you, Craig, I want to use this method in an application that basically plays a song, and while listening I press a key to mark specific moments, which are stored in a list. Later, those moments will be used to trigger processes synchronized with the same song.

Re: Wait until commandKey is down only works once

Posted: Sun Aug 24, 2025 7:53 pm
by dunbarx
Ah. OK, I see.

I recommend that you use a key that sends a message as opposed to a control key (like the "command" key), which does not, rather it only describes a condition.

In this way each keypress triggers a handler all on its own, and the "queue" that you first set up goes away. Something like:

Code: Select all

on functionKey tKey
   if tKey = "F2" then put the seconds & return after fld 1
end functionKey
The point here is that each keypress has its own "life". I have no idea if the "seconds" which would log the time of the keypress is useful to you, as this is just an example of another way of thinking.

Craig