Focus on a button?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

Focus on a button?

Post by dunbarx » Wed Aug 26, 2020 12:04 am

How does one focus a button? To trap a message like "commandKeyDown", requires a focused control (or the card or stack, which are always, er, "in focus") to trap it.

So this works in a locked or unlocked field script, if you click on the field and press CMD-3:

Code: Select all

on commandkeyDown tkey
   if  tkey = "3" then beep 3
end commandkeyDown
Because the field has focus.

But not a button, You cannot, from msg, "focus on btn 1" and then move the cursor over that button and press "CMD-3". It may have once had focus, but does not maintain it very well.

However, this works:

Code: Select all

on mouseEnter
   focus on me
end mouseEnter

on commandkeyDown tkey
   if  tkey = "3" then beep 3
end commandkeyDown
Move the cursor into the button rect, and press CMD-3. Perhaps if the cursor stays within the rect, so does focus?

I was intrigued by the notion of a focused button, essentially an entirely invisible property, in more ways than simply visually. Whether it is useful to be able to use messages like commandKeyDown there is another story.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Focus on a button?

Post by jacque » Wed Aug 26, 2020 6:56 pm

Buttons have focus on Windows and show a visual clue. Mac doesn't have that, though you seem to have found a workaround. I've always assumed it's only useful on Windows.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Focus on a button?

Post by dunbarx » Wed Aug 26, 2020 7:21 pm

Jacque.

The kludgey workaround notwithstanding, (you know how much I like to play) how do you use focus in a button?

Visual clues in Windows notwithstanding, button focus on a Mac is fragile indeed. How do you set the focus on a button that sticks?

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Focus on a button?

Post by jacque » Wed Aug 26, 2020 9:25 pm

You don't. It's just a Windows thing. If you run the stack on a Windows machine, you'll see the faint outline on the button that shows it has focus. That said, there is (or used to be) a system setting on Mac that allows you to tab into buttons like Windows users can do. Maybe if you set that up, focus will be retained. I never tried it, and of course you couldn't rely on other Mac users having the same setup.

Edit: Alternately, did you try setting the traversalOn of the button? That might be enough.
Last edited by jacque on Wed Aug 26, 2020 9:27 pm, edited 1 time in total.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Focus on a button?

Post by SparkOut » Wed Aug 26, 2020 9:27 pm

I dont know how it works on a Mac. On Windows, it's an ease of use thing. You can expect a form with buttons to be traversed without a mouse. Using tab to move from one object to another, this will include setting the focus on a button, which will typically be set to display an internal margin to indicate that it has focus. Hitting the space bar when a button is focused with effectively perform a mouseclick on that button. (Similarly, hitting space bar will toggle a checkbox hilite.)

PBH
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 133
Joined: Sun Feb 20, 2011 4:26 pm
Contact:

Re: Focus on a button?

Post by PBH » Thu Aug 27, 2020 4:41 am

AFAIK the only button that can receive focus on a Mac is the "Default" button (i.e. the Blue button in the tool palette). Often used in dialogue boxes where you typically don't need to click the button, just hit "Return" on the keyboard.

Paul

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

Re: Focus on a button?

Post by dunbarx » Thu Aug 27, 2020 2:39 pm

Here is an error I never saw before:
"focus: not a valid control"
So for those with nothing better to do, the following handlers are in the script of various controls:

Code: Select all

on mouseEnter
   focus on the target
end mouseEnter

on commandkeyDown tkey
   if  tkey = "3" then beep 3
end commandkeyDown
Default buttons, radio btns, checkboxes, popups and comboBoxes can be focused. All others cannot be. You get the error above.
Scrollbars, little arrows, Progress bars, etc. can be. Any field can be.

I cannot think of another way to focus on certain controls on a Mac apart from the code above.

Craig

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

Re: Focus on a button?

Post by richmond62 » Thu Aug 27, 2020 2:57 pm

Personally I'd focus on a button using a subtle colour overlay:
-
Screenshot 2020-08-27 at 16.55.36.png
-
That will stick.

Code: Select all

on openCard
   set the colorOverlay["color"] of btn "B3" to red
   set the colorOverlay["opacity"] of btn "B3" to 20
end openCard
Then it would be reasonable to have some sort of code that cycled through
the buttons checking their colorOverlay.
Attachments
Btn Focus.livecode.zip
Here's the stack.
(1.02 KiB) Downloaded 254 times

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

Re: Focus on a button?

Post by dunbarx » Thu Aug 27, 2020 4:52 pm

Richmond.

The issue is not identifying a focused button so much as having it focused at all. I could only do so with the two-handler script posted. Focusing from any other source, the msg box, for example, does not "stick".

Craig

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

Re: Focus on a button?

Post by richmond62 » Thu Aug 27, 2020 5:10 pm

Code: Select all

on mouseEnter
   set the colorOverlay["color"] of me to red
   set the colorOverlay["opacity"] of me to 20
end mouseEnter

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

Re: Focus on a button?

Post by dunbarx » Thu Aug 27, 2020 7:21 pm

Nope. Not understanding.

The commandKeyDown handler requires a focused control. Your property settings do nothing for that.

Craig

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

Re: Focus on a button?

Post by richmond62 » Thu Aug 27, 2020 7:25 pm

9 hours editing PDF files for children's placement exams . . .

. . . what can I say except, "A bit slow off the mark." :?
-
wp-1485949833396.jpg

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

Re: Focus on a button?

Post by richmond62 » Thu Aug 27, 2020 7:51 pm

To trap a message like "commandKeyDown", requires a focused control
Is that a fact?

Code: Select all

on mouseUp
   put empty into fld "ff"
   if the commandKey is down then
      put "Yowza" into fld "ff"
   end if
end mouseUp
-
Screenshot 2020-08-27 at 21.49.41.png
Attachments
Commander Bond.livecode.zip
Here's the stack.
(983 Bytes) Downloaded 240 times

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Re: Focus on a button?

Post by SparkOut » Thu Aug 27, 2020 8:29 pm

What I read in Craig's post
dunbarx wrote:
Thu Aug 27, 2020 7:21 pm
The commandKeyDown handler requires a focused control.
does not quite match Richmond's quote
richmond62 wrote:
Thu Aug 27, 2020 7:51 pm
To trap a message like "commandKeyDown", requires a focused control

Code: Select all

if the commandKey is down then
is not handling a message, it is checking a property.
So I am not sure if this is a apples vs oranges scenario, or whether a "focused control" is moot because the commandKeyDown handler could be in the stack or a library script. I am a bit intrigued but prepared to just chalk it down to Mac/Win/'nix variance.

Post Reply