How to detect user holding enter key down

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

CAsba
Posts: 423
Joined: Fri Sep 30, 2022 12:11 pm

How to detect user holding enter key down

Post by CAsba » Wed Jun 19, 2024 12:32 pm

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?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: How to detect user holding enter key down

Post by richmond62 » Wed Jun 19, 2024 12:52 pm

Unfortunately , unlike the terms shiftKeyDown and controlKeyDown, there are not equivalents for the RETURN or ENTER keys.

But my extremely simplistic stack:
-
Screenshot 2024-06-19 at 14.51.01.png
-
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
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: How to detect user holding enter key down

Post by richmond62 » Wed Jun 19, 2024 12:57 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to detect user holding enter key down

Post by Klaus » Wed Jun 19, 2024 1:06 pm

From the dictionary:
...
The keysDown function returns a list of keycodes of pressed keys, ...
...
So "enterkey" will NEVER be in "the keysdown"!

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: How to detect user holding enter key down

Post by richmond62 » Wed Jun 19, 2024 1:21 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to detect user holding enter key down

Post by dunbarx » Wed Jun 19, 2024 1:53 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to detect user holding enter key down

Post by dunbarx » Wed Jun 19, 2024 1:58 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to detect user holding enter key down

Post by dunbarx » Wed Jun 19, 2024 2:15 pm

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
Last edited by dunbarx on Wed Jun 19, 2024 2:18 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: How to detect user holding enter key down

Post by richmond62 » Wed Jun 19, 2024 2:17 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to detect user holding enter key down

Post by dunbarx » Wed Jun 19, 2024 2:20 pm

Richmond.

See my post just above yours. Are we talking about the same thing?

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: How to detect user holding enter key down

Post by richmond62 » Wed Jun 19, 2024 2:22 pm

Well . . . I am writing about a way a stack can detect if an end-user has the ENTER KEY depressed either by itself . . .

Code: Select all

on enterKey
or when they depress another key, such as the 'K'.

That is my reading of what's going down in the souk. 8)

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to detect user holding enter key down

Post by Klaus » Wed Jun 19, 2024 2:49 pm

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 :-D )

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10076
Joined: Fri Feb 19, 2010 10:17 am

Re: How to detect user holding enter key down

Post by richmond62 » Wed Jun 19, 2024 4:15 pm

Use rawKeyCodes: 65505 / 65506

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to detect user holding enter key down

Post by dunbarx » Wed Jun 19, 2024 5:19 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to detect user holding enter key down

Post by FourthWorld » Wed Jun 19, 2024 5:34 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply