Page 1 of 1

Focus on a button?

Posted: Wed Aug 26, 2020 12:04 am
by dunbarx
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

Re: Focus on a button?

Posted: Wed Aug 26, 2020 6:56 pm
by jacque
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.

Re: Focus on a button?

Posted: Wed Aug 26, 2020 7:21 pm
by dunbarx
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

Re: Focus on a button?

Posted: Wed Aug 26, 2020 9:25 pm
by jacque
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.

Re: Focus on a button?

Posted: Wed Aug 26, 2020 9:27 pm
by SparkOut
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.)

Re: Focus on a button?

Posted: Thu Aug 27, 2020 4:41 am
by PBH
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

Re: Focus on a button?

Posted: Thu Aug 27, 2020 2:39 pm
by dunbarx
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

Re: Focus on a button?

Posted: Thu Aug 27, 2020 2:57 pm
by richmond62
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.

Re: Focus on a button?

Posted: Thu Aug 27, 2020 4:52 pm
by dunbarx
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

Re: Focus on a button?

Posted: Thu Aug 27, 2020 5:10 pm
by richmond62

Code: Select all

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

Re: Focus on a button?

Posted: Thu Aug 27, 2020 7:21 pm
by dunbarx
Nope. Not understanding.

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

Craig

Re: Focus on a button?

Posted: Thu Aug 27, 2020 7:25 pm
by richmond62
9 hours editing PDF files for children's placement exams . . .

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

Re: Focus on a button?

Posted: Thu Aug 27, 2020 7:51 pm
by richmond62
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

Re: Focus on a button?

Posted: Thu Aug 27, 2020 8:29 pm
by SparkOut
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.