How to detect user holding enter key down
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to detect user holding enter key down
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?
			
			
									
									
						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?
- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to detect user holding enter key down
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:
			
			
									
									
						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
- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to detect user holding enter key down
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 keysDownRe: How to detect user holding enter key down
From the dictionary:
			
			
									
									
						So "enterkey" will NEVER be in "the keysdown"!...
The keysDown function returns a list of keycodes of pressed keys, ...
...
- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to detect user holding enter key down
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:
However LC does NOT pick up a 'normal' keyDown from the enterKey.
			
			
									
									
						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 keyDownRe: How to detect user holding enter key down
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
			
			
									
									
						What exactly are you trying to do, or rather, prevent?
Craig
Re: How to detect user holding enter key down
Sending such a message may not be possible in a running script. I made a stack with a single field. In the card script:
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
			
			
									
									
						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 enterKeyAnyone know how to "break into" that running handler?
Craig
Re: How to detect user holding enter key down
Add another field. Does this help?
Hit the enter key while the handler is running. The KeyDown function works everywhere, every time.
Craig
			
			
													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 mouseUpCraig
					Last edited by dunbarx on Wed Jun 19, 2024 2:18 pm, edited 1 time in total.
									
			
									
						- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to detect user holding enter key down
The problem lies in the fact that shiftKey() exists, but enterKey() and returnKey() do not.
c.f.:
			
			
									
									
						c.f.:
Code: Select all
on keyDown
if shiftKey() is down then
   -- do something
   else
   -- do something different
   end if
end keyDownRe: How to detect user holding enter key down
Richmond.
See my post just above yours. Are we talking about the same thing?
Craig
			
			
									
									
						See my post just above yours. Are we talking about the same thing?
Craig
- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to detect user holding enter key down
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.
			
			
									
									
						Code: Select all
on enterKeyThat is my reading of what's going down in the souk.

Re: How to detect user holding enter key down
Maybe this will work with some tricks?
Put this into the script of your card:
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:
Out of my head, but should work.
(Famous last words )
 )
			
			
									
									
						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_enterkeyHint: 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
...(Famous last words
 )
 )- 
				richmond62
- Livecode Opensource Backer 
- Posts: 10202
- Joined: Fri Feb 19, 2010 10:17 am
Re: How to detect user holding enter key down
Use rawKeyCodes: 65505 / 65506
			
			
									
									
						Re: How to detect user holding enter key down
Klaus.
The OP wrote:
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
			
			
									
									
						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?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
- 
				FourthWorld
- VIP Livecode Opensource Backer 
- Posts: 10065
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: How to detect user holding enter key down
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
						LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn