How do I code "If this is a button, then ..."?

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
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

How do I code "If this is a button, then ..."?

Post by sritcp » Sat Jun 23, 2012 1:43 am

I want my program to respond only if a button is clicked (not if clicked anywhere else). Since the action of all buttons on all cards are similar, I am trying to write a mouseUp message handler at the stack level. So I need to first make sure that if the click was received by a button. I tried

If the target is a button then .....

but it doesn't seem to be legal. So, what is the property of an object that specifies it as a button, field, menu, etc. ? Or, what's another way to achieve my objective?

Thanks,
Sri.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do I code "If this is a button, then ..."?

Post by sturgis » Sat Jun 23, 2012 2:25 am

if word 1 of the target is "button" then...

Should work like a charm.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How do I code "If this is a button, then ..."?

Post by sritcp » Sat Jun 23, 2012 3:34 am

Hi sturgis:

Thanks. Simple and clever solution!
By the way, if I use target instead of the target, it doesn't work (even though the dictionary says that both are equivalent for objects other than a field).

Regards,
Sri.

gpb01
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Sat Jun 04, 2011 5:41 pm
Location: Switzerland

Re: How do I code "If this is a button, then ..."?

Post by gpb01 » Sat Jun 23, 2012 11:43 am

sritcp wrote:Hi sturgis:

Thanks. Simple and clever solution!
By the way, if I use target instead of the target, it doesn't work (even though the dictionary says that both are equivalent for objects other than a field).

Regards,
Sri.
Is a function so, or you use the target or you use target() ... with the parenthesis !

Guglielmo

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do I code "If this is a button, then ..."?

Post by sturgis » Sat Jun 23, 2012 2:12 pm

Yep, as Guglielmo says, check the dictionary, you can either use THE TARGET or TARGET().

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How do I code "If this is a button, then ..."?

Post by sritcp » Sat Jun 23, 2012 3:22 pm

I was referring to the following, found in the Comments section under target keyword in the dictionary

If the target object is not a field, the target keyword reports the name of the object, just like the target function.

I found that it does not; target keyword reports nothing; the message box is blank. (The target function works, of course).
This is what I was trying to convey.

Regards,
Sri.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do I code "If this is a button, then ..."?

Post by sturgis » Sat Jun 23, 2012 3:35 pm

Hmm interesting.

If I put a mouseup handler into the card, then drop a button and a field onto the card

And the mouseup handler is as follows

on mouseUp
put target
end mouseUp

It works exactly as described. If I click the text field (with text in it, and with the locktext set to true) it displays the contents of the field in the msg box. If I click the button (and there is no mouseup handler in it) it displays the button name.

So the most likely answer is your field is not locked, so when yuo click it there is no mousup sent, instead the insertion point is placed at the location of the mouse click.

From the mouseup dictionary entry
The mouseUp message is sent only when the Browse tool is being used. If an unlocked field is clicked with mouse button 1 or 2, no mouseUp message is sent.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How do I code "If this is a button, then ..."?

Post by sritcp » Sat Jun 23, 2012 5:53 pm

sturgis wrote: .............
on mouseUp
put target
end mouseUp

It works exactly as described...........
Yes, but not if you try

put word 1 of target

I guess "word 1 of" doesn't work with target but does with the target.
(By the way, I tried only with a button; so the "lockedText" explanation, while valid, didn't apply in my case)

Regards,
Sri.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do I code "If this is a button, then ..."?

Post by sturgis » Sat Jun 23, 2012 6:03 pm

That is a bit weird, and I finally see what you're talking about.

of course for what you're trying to do using the function is a better choice either way, however to make it work as expected with the keyword rather than function you can do this

put word 1 of (target) -- force target to be completely evaluated first

Good catch, it SHOULD work as you say. I just always use target() or the target so this is a new one on me!

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How do I code "If this is a button, then ..."?

Post by sritcp » Sat Jun 23, 2012 6:08 pm

sturgis wrote:......
put word 1 of (target) -- force target to be completely evaluated first
And it works! And, I learned something today!

Thanks,
Sri.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: How do I code "If this is a button, then ..."?

Post by sturgis » Sat Jun 23, 2012 6:10 pm

sritcp wrote:
sturgis wrote:......
put word 1 of (target) -- force target to be completely evaluated first
And it works! And, I learned something today!

Thanks,
Sri.
So did I!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7241
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How do I code "If this is a button, then ..."?

Post by jacque » Sat Jun 23, 2012 9:58 pm

It's historical syntax dating back to HyperCard. The keyword "target" (without "the" or parentheses) refers to the text content of a field.

If you click on a field:

put target -- field text content
put the target -- "field x of stack y"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”