[Solved] Action on return, enter and also on button click

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
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

[Solved] Action on return, enter and also on button click

Post by redfield » Fri Jun 14, 2019 6:44 pm

Hi guys,
probably another silly question of mine:

I would like to give the user (which is me :lol: ) the possibility of firing the program action either by mouseclick or by hitting the enter or the return key, after me has entered a value in a field.

I am wondering if I have to copy the complete code not only in the button script but also twice in the field script (with returnKeyUp and enterKeyUp (?)) to achieve this? I know of rawKeyUp but that won't work for me, because then some user input checks are triggered too soon.
Last edited by redfield on Sun Jun 16, 2019 12:47 pm, edited 1 time in total.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Action on return, enter and also on button click

Post by SparkOut » Fri Jun 14, 2019 7:14 pm

Notwithstanding that there may be other considerations or approaches (eg on closeField), depending on your user interface and program flow, where you have multiple possible triggers of the same code, you can easily avoid code duplication by leveraging the message path.
Create a handler in a suitable place in the message path (group script, card script or stack script, for example)

Code: Select all

on validateInput pData
  if pData is not an integer then
    answer "Invalid input. You entered" && quote & pData & quote && "which is not an integer."
    end if
    --and or your code that you want to avoid duplicating 
 end validateInput
Then in the different trigger places, you can

Code: Select all

on enterInField
   put the text of me into tData
   validateInput tData
end enterInField
etc etc for returnInField, closeField

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Action on return, enter and also on button click

Post by dunbarx » Fri Jun 14, 2019 10:35 pm

I think Sparkout and I will be on the same page.

Make a few buttons and fields. Name one button "B1". Name one field "F1".

In the card script:

Code: Select all

on mouseup
   if the short name of the target = "b1" then doAction
end mouseup

on closeField
     if the short name of the target = "f1" then doAction
  end closeField
  
  on doAction
     answer "Get going..."
  end doAction
And as Sparkout said, add as many other message traps as you need. Be aware that you may need to pass some of those messages, depending on what you are doing.

A behavior script would do much the same thing.

Craig

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Action on return, enter and also on button click

Post by redfield » Sun Jun 16, 2019 12:20 pm

Thanks a lot, this is working very well. I put the whole script in a custom handler and then call that handler from different fields and buttons.

(Is there a way here to mark topics as solved??)

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Action on return, enter and also on button click

Post by bogs » Sun Jun 16, 2019 12:34 pm

Yes, you go to your first post in the thread, click on the 'edit' pencil, and in the highlighted box above the post type "Solved" in whatever form you like!
Selection_001.png
Solve-ed, no charge!
There are many variations, such as :
* [Solved]
* {Solved}
* : Solved :
* --Solved...

Invent a new one if you like :D
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”