Page 1 of 1

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

Posted: Fri Jun 14, 2019 6:44 pm
by redfield
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.

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

Posted: Fri Jun 14, 2019 7:14 pm
by SparkOut
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

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

Posted: Fri Jun 14, 2019 10:35 pm
by dunbarx
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

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

Posted: Sun Jun 16, 2019 12:20 pm
by redfield
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??)

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

Posted: Sun Jun 16, 2019 12:34 pm
by bogs
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