handling return/enter key actions

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

Post Reply
glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

handling return/enter key actions

Post by glenn9 » Thu Apr 09, 2020 7:45 am

Hi everyone,

I've got two fields on a stack and I want a user to enter field 2 either by directly clicking into, or by 'tabbing', to field 2 from field 1.

Because the return/enter key causes a carriage return within field 1, I didn't want the user to think they could use the enter key to move to the next field, ie field 2

I've set up some datavalidation for field 1, with number and character limitations, as below:

Code: Select all

on keydown pKey
   if the length of me = 3 or pKey is not a number then
      answer warning "Check entry!" titled"Error"
  
   else 
      	      pass keyDown
   end if
end keydown
To handle the return/entry key issue I've tried the functions 'returnKey' and 'returnInField' but I can't get these to handle a desired effect of preventing the return/entry key passing into field 1, or indeed to move the focus to field 2!

Grateful for any help or advice.

Kind regards,

Glenn

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

Re: handling return/enter key actions

Post by Klaus » Thu Apr 09, 2020 9:31 am

Hi Glenn,

adding two dummy handlers should do the trick:

Code: Select all

on keydown pKey
   if the length of me = 3 or pKey is not a number then
      answer warning "Check entry!" titled "Error"
   else 
      pass keyDown
   end if
end keydown

on returninfield
   ##
end returninfield

on enterinfield
   ##
end enterinfield
Best

Klaus

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: handling return/enter key actions

Post by glenn9 » Thu Apr 09, 2020 10:04 am

Hi Klaus,

that's brilliant, it works - many thanks - however, I'm not sure why it works! - if you've got opportunity/time etc would you be able to do a short explanation??

(by coincidence, as originally I couldn't get anything inside the returnInField handler to work, I left it empty in my original code - if only I'd added another empty one!!!)

Kind regards,

Glenn

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

Re: handling return/enter key actions

Post by Klaus » Thu Apr 09, 2020 10:23 am

Hi Glenn,

well, this is just a rude case of "breaking the message hierarchy"! :-D

Without the dummy handlers RETURN goes up in the message hierarchy and checks if there is some instruction for RETURN(infield):
Its own script -> possible group script -> card -> stack -> library -> Home stack -> Engine
No script for RETURN? Then RETURN "gets through" = new line in field

But we added these dummies and so the engine already stops at "its (field) own script", and sees the instruction -> Don't do anything!
Well, maybe technically a tad incorrect, but you get the picture.

Here a good read about this topic:
http://www.fourthworld.com/embassy/arti ... _path.html


Best from germany

Klaus

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: handling return/enter key actions

Post by glenn9 » Thu Apr 09, 2020 10:33 am

Thanks for the explanation Klaus, I think I understand it now!

I'll also have a read through Richard's article over the bank holiday weekend here in the UK.

Kind regards,

Glenn

Post Reply