Page 1 of 2
How to detect user holding enter key down
Posted: Wed Jun 19, 2024 12:32 pm
by CAsba
Hi,
It's just occurred to me what a mess ensues if the user holds down 'enter' . Is there a SIMPLE way that key holding could be detected in a way that a script could be activated to abandon the current task?
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 12:52 pm
by richmond62
Unfortunately , unlike the terms
shiftKeyDown and
controlKeyDown, there are not equivalents for the
RETURN or
ENTER keys.
But my extremely simplistic stack:
-
-
May prove vaguely useful.
Here's the cardScript:
Code: Select all
on enterKey
put "ENTER" into fld "ff"
wait 20 ticks
put empty into fld "ff"
end enterKey
on returnKey
put "RETURN" into fld "ff"
wait 20 ticks
put empty into fld "ff"
end returnKey
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 12:57 pm
by richmond62
If you are worrying about the ENTER key being pressed when another key is being pressed you could try something like this:
Code: Select all
on keysDown
if enterKey is in the keysDown then
exit to top
else
-- do something else
end if
end keysDown
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 1:06 pm
by Klaus
From the dictionary:
...
The keysDown function returns a list of keycodes of pressed keys, ...
...
So "enterkey" will NEVER be in "the keysdown"!
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 1:21 pm
by richmond62
Quite.
So the first thing you have to do is find the keyCode for the ENTER key . . . which is a right @#$%^&* is nothing is returned on Mac.
However the keyCode for ENTER is generally 13.
So:
Code: Select all
on keyDown
if numToCodePoint(13) is in the keysDown then
set the backGroundColor of this stack to pink
else
set the backGroundColor of this stack to green
end if
end keyDown
However LC does NOT pick up a 'normal' keyDown from the enterKey.
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 1:53 pm
by dunbarx
There is the "enterKey" message which is sent whenever you press the, er, enter key.
What exactly are you trying to do, or rather, prevent?
Craig
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 1:58 pm
by dunbarx
Sending such a message may not be possible in a running script. I made a stack with a single field. In the card script:
Code: Select all
on mouseUp
repeat until the optionkey is down
put random(999) into fld 1
wait 5
end repeat
end mouseUp
on enterKey
answer "XYZ"
end enterKey
if one clicks on the card, a string of random numbers appears in the field. Hitting the enter key does not interrupt that process, though the message is queued, as can be seen when one hits the option key. Then the loop ends, and the answer dialog appears.
Anyone know how to "break into" that running handler?
Craig
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 2:15 pm
by dunbarx
Add another field. Does this help?
Code: Select all
on mouseUp
repeat until the optionkey is down
put random(999) into fld 1
wait 5
put keysDown() into fld 2
end repeat
end mouseUp
Hit the enter key while the handler is running. The KeyDown function works everywhere, every time.
Craig
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 2:17 pm
by richmond62
The problem lies in the fact that
shiftKey() exists, but
enterKey() and
returnKey() do not.
c.f.:
Code: Select all
on keyDown
if shiftKey() is down then
-- do something
else
-- do something different
end if
end keyDown
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 2:20 pm
by dunbarx
Richmond.
See my post just above yours. Are we talking about the same thing?
Craig
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 2:22 pm
by richmond62
Well . . . I am writing about a way a stack can detect if an end-user has the ENTER KEY depressed either by itself . . .
or when they depress another key, such as the 'K'.
That is my reading of what's going down in the souk.

Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 2:49 pm
by Klaus
Maybe this will work with some tricks?
Put this into the script of your card:
Code: Select all
local stop_execution
## Init local var:
on opencard
put FALSE into stop_execution
end opencard
## Set the lcocal var:
on enterkey
put TRUE into stop_execution
end enterkey
##
command your_script_you_want_to_cancel_on_enterkey
## ...
## ...
if stop_execution = TRUE then
## Do some clean-up with fields and variables if neccessary
## ...
## Reset for next execution:
put FALSE into stop_execution
## Finally leave the script
exit to top
end if
## Rest of script if any...
end your_script_you_want_to_cancel_on_enterkey
Know what I mean?
Hint: If you want to do this inside of a REPEAT loop, you need to add a little WAIT statement
to make "room" for the ENTERKEY message, otherwise it will only fire AFTER the loop:
Code: Select all
...
repeat with ...
## do this
## do that
wait 0 with messages
if stop_execution = TRUE then
## see above
end if
...
Out of my head, but should work.
(Famous last words

)
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 4:15 pm
by richmond62
Use rawKeyCodes: 65505 / 65506
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 5:19 pm
by dunbarx
Klaus.
The OP wrote:
Code: Select all
Is there a SIMPLE way that key holding could be detected in a way that a script could be activated to abandon the current task?
Didn't the OP want to be able to stop (or divert?) execution of a
running handler by hitting the enter key?
Your gadget initializes the local variable, but that only stops execution of the handler after the "clean-up" lines are processed, not the handler itself the moment the enter key is pressed.
CAsba, please be precise. What do you want to do?
Craig
Re: How to detect user holding enter key down
Posted: Wed Jun 19, 2024 5:34 pm
by FourthWorld
CAsba wrote: ↑Wed Jun 19, 2024 12:32 pm
It's just occurred to me what a mess ensues if the user holds down 'enter' . Is there a SIMPLE way that key holding could be detected in a way that a script could be activated to abandon the current task?
Can you describe the use case?
It's possible, but given the differences been action keys and modifier keys it may be more trouble than it's worth, esp considering long-standing user habits with that key specifically.
Once we understand the purpose of this goal we'll be able to point you in a good direction.