Page 1 of 1

Pushing a button from somewhere else

Posted: Wed Dec 16, 2020 7:51 pm
by gsillevis
I can't seem to figure out the right vocabulary for this, so my googling isn't finding anything.

So I have a button (Button1) that on Mousedown, does something.

If I make another button (Button2) that, when clicked, simply activates Mousedown on Button1?

I know I could just copy the code, but I'm trying something else.

Thank you in advance!

Re: Pushing a button from somewhere else

Posted: Wed Dec 16, 2020 7:56 pm
by kdjanz
Should be as simple as:

Code: Select all

send "mouseDown" to button "button2"
Try that...

or you could create a command "soSomething" on the card or stack level and then have this script in each of the buttons:

Code: Select all

on mouseDown
    doSomething
end mouseDown
Copying the code is for beginners. You bloat your code, leave two places for errors and you will drive yourself nuts when you change one button but forget the other - and now your program isn't working the way it used to. NEVER copy code if you can help it.
You are on the right path to think of a better way. Using the hierarchy is the LiveCode way.

LATER EDIT - what Klaus said!

Re: Pushing a button from somewhere else

Posted: Wed Dec 16, 2020 8:00 pm
by Klaus
HI gsillevis,

we can simply SEND messages, so do like this:

Code: Select all

## Button2
on mouseup
   send "mousedown" to btn "Button1"
   ## More mouseup stuff if neccessary...
end mouseup
However, REALLY Pro would be to not have the complete code in your button but in the card- or stack-script.
This way you could simply "call" the handler that get executed "on mousedown"!

Example:

Code: Select all

## Instead of this in Button1
on mousedown
  dothis
  dothat
  dothisonetoo
  andthatone
  plus_this
end mousedown

on mouseup
  ## more stuff...
end mouseup
This goes into the stack- or card-script:

Code: Select all

command do_everything
  dothis
  dothat
  dothisonetoo
  andthatone
  plus_this
end do_everything
Then THIS goes into btn -> Button1:

Code: Select all

on mousedown
  do_everything
end mousedown

on mouseup
  ## more stuff...
end mouseup
NOW button Button2 can do:

Code: Select all

on mouseup
  do_everything
end mouseup
Get it?
No need to CALL mousedown of Button1 etc. anymore!

Best

Klaus

Re: Pushing a button from somewhere else

Posted: Wed Dec 16, 2020 8:12 pm
by gsillevis
Thank you!

That was exactly what I was looking for!

Re: Pushing a button from somewhere else

Posted: Wed Dec 16, 2020 8:51 pm
by dunbarx
Another way, which may or may not be appropriate depending on how many controls are in play and what they do. Imagine you have three buttons, named "X", "Y" and "Z". In the card script:

Code: Select all

on mouseDown
if the short name of the target is "X" then doXStuff
if the short name of the target is "Y" then doYStuff
if the short name of the target is "Z" then doZStuff
end mouseDown
Now nothing at all needs to go into those buttons, you only need to name them.

Craig

Re: Pushing a button from somewhere else

Posted: Thu Dec 17, 2020 9:45 am
by richmond62
At the risk of pushing the OP's button from somewhere else. :?

This is ever so slightly OT, but about 19-20 years ago when I was getting started with
what is now called LiveCode Klaus (a mage round these parts) asked me why I kept using
mouseDown rather than mouseUp.

Having come to LiveCode from HyperCard via Toolbook I hadn't really thought about this, mainly
because, as far as I could remember, I'd never heard of mouseUp.

However, as Klaus explained, mouseUp offered the end user the chance to change their mind.

Re: Pushing a button from somewhere else

Posted: Thu Dec 17, 2020 5:16 pm
by dunbarx
Richmond.

There are good reasons for both, though certainly most of my existing handlers could exploit either. But users expect actions to initiate on mouseUp, not mouseDown.

The "grab" command, for example is explicitly bound with mouseDown.

As for Klaus' point, the user could indeed change his mind with mouseUp, since the "up" has to have the mouseLoc within the rect of the target control. Enter the "mouseRelease".

Craig