Pushing a button from somewhere else
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Pushing a button from somewhere else
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!
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
Should be as simple as:
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:
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!
Code: Select all
send "mouseDown" to button "button2"
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
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.
Re: Pushing a button from somewhere else
HI gsillevis,
we can simply SEND messages, so do like this:
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:
This goes into the stack- or card-script:
Then THIS goes into btn -> Button1:
NOW button Button2 can do:
Get it?
No need to CALL mousedown of Button1 etc. anymore!
Best
Klaus
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
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
Code: Select all
command do_everything
dothis
dothat
dothisonetoo
andthatone
plus_this
end do_everything
Code: Select all
on mousedown
do_everything
end mousedown
on mouseup
## more stuff...
end mouseup
Code: Select all
on mouseup
do_everything
end mouseup
No need to CALL mousedown of Button1 etc. anymore!
Best
Klaus
Re: Pushing a button from somewhere else
Thank you!
That was exactly what I was looking for!
That was exactly what I was looking for!
Re: Pushing a button from somewhere else
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:
Now nothing at all needs to go into those buttons, you only need to name them.
Craig
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
Craig
-
- Livecode Opensource Backer
- Posts: 10080
- Joined: Fri Feb 19, 2010 10:17 am
Re: Pushing a button from somewhere else
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.

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
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
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