Pushing a button from somewhere else

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
gsillevis
Posts: 40
Joined: Tue Oct 13, 2020 10:55 pm

Pushing a button from somewhere else

Post by gsillevis » Wed Dec 16, 2020 7:51 pm

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!

kdjanz
Posts: 300
Joined: Fri Dec 09, 2011 12:12 pm

Re: Pushing a button from somewhere else

Post by kdjanz » Wed Dec 16, 2020 7:56 pm

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!
Last edited by kdjanz on Wed Dec 16, 2020 8:06 pm, edited 3 times in total.

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

Re: Pushing a button from somewhere else

Post by Klaus » Wed Dec 16, 2020 8:00 pm

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

gsillevis
Posts: 40
Joined: Tue Oct 13, 2020 10:55 pm

Re: Pushing a button from somewhere else

Post by gsillevis » Wed Dec 16, 2020 8:12 pm

Thank you!

That was exactly what I was looking for!

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

Re: Pushing a button from somewhere else

Post by dunbarx » Wed Dec 16, 2020 8:51 pm

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

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

Re: Pushing a button from somewhere else

Post by richmond62 » Thu Dec 17, 2020 9:45 am

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.

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

Re: Pushing a button from somewhere else

Post by dunbarx » Thu Dec 17, 2020 5:16 pm

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

Post Reply