Discussion:
keyboard focus handling in custom controls
iain
2018-09-10 12:21:37 UTC
Permalink
Hi,

I have an application, which has 3 controls

[button] [custom view] [button]

I’ve set the NextKeyView of each of them to the control to the right
[button] -> [custom view] -> [button]

And the tab and shift tab works between them as expected.

The situation gets more complicated because the custom view has 3 possible focus areas, which I also want to be part of the tab chain.
As it’s one big custom drawn widget, I’ve implemented a rudimentary tab handler in the KeyDown: method with a counter to keep track of which area is focused.
And it sort of works

But, I’m wondering how to know if the parent NSView gained keyboard focus through a tab or a shift-tab

If focus is on the first button, and you press Tab, the focus should go to the 1st area of the custom view
If focus is on the last button and you Shift Tab from it, the focus should go to the 3rd area of the custom view
Currently, it always has to go to the area that was focused when the view lost focus.

Is there a way to do what I’m wanting?

thanks
iain


_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.nar
Alastair Houghton
2018-09-10 13:19:10 UTC
Permalink
Post by iain
I have an application, which has 3 controls
[button] [custom view] [button]
I’ve set the NextKeyView of each of them to the control to the right
[button] -> [custom view] -> [button]
And the tab and shift tab works between them as expected.
The situation gets more complicated because the custom view has 3 possible focus areas, which I also want to be part of the tab chain.
As it’s one big custom drawn widget, I’ve implemented a rudimentary tab handler in the KeyDown: method with a counter to keep track of which area is focused.
And it sort of works
But, I’m wondering how to know if the parent NSView gained keyboard focus through a tab or a shift-tab
If focus is on the first button, and you press Tab, the focus should go to the 1st area of the custom view
If focus is on the last button and you Shift Tab from it, the focus should go to the 3rd area of the custom view
Currently, it always has to go to the area that was focused when the view lost focus.
Is there a way to do what I’m wanting?
In your -becomeFirstResponder, you could examine the current event (noting that keypresses aren’t the only way you could gain keyboard focus) to determine whether it was a Tab or Shift-Tab keypress. You can get the event using [NSApp currentEvent]. Make sure it works with Full Keyboard Access turned on in System Preferences as well.

Also note that you will want to implement the relevant accessibility support; otherwise, when users turn on VoiceOver, they won’t be able to focus individual elements in your view, and the VoiceOver cursor (which is not the same thing as the normal keyboard focus system) won’t interact properly with your view.

Kind regards,

Alastair.

--
http://alastairs-place.net

_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.n
iain
2018-09-10 15:07:32 UTC
Permalink
Post by Alastair Houghton
Post by iain
I have an application, which has 3 controls
[button] [custom view] [button]
I’ve set the NextKeyView of each of them to the control to the right
[button] -> [custom view] -> [button]
And the tab and shift tab works between them as expected.
The situation gets more complicated because the custom view has 3 possible focus areas, which I also want to be part of the tab chain.
As it’s one big custom drawn widget, I’ve implemented a rudimentary tab handler in the KeyDown: method with a counter to keep track of which area is focused.
And it sort of works
But, I’m wondering how to know if the parent NSView gained keyboard focus through a tab or a shift-tab
If focus is on the first button, and you press Tab, the focus should go to the 1st area of the custom view
If focus is on the last button and you Shift Tab from it, the focus should go to the 3rd area of the custom view
Currently, it always has to go to the area that was focused when the view lost focus.
Is there a way to do what I’m wanting?
In your -becomeFirstResponder, you could examine the current event (noting that keypresses aren’t the only way you could gain keyboard focus) to determine whether it was a Tab or Shift-Tab keypress. You can get the event using [NSApp currentEvent]. Make sure it works with Full Keyboard Access turned on in System Preferences as well.
Ah, good idea, thank you.
Post by Alastair Houghton
Also note that you will want to implement the relevant accessibility support; otherwise, when users turn on VoiceOver, they won’t be able to focus individual elements in your view, and the VoiceOver cursor (which is not the same thing as the normal keyboard focus system) won’t interact properly with your view.
Yup, that’s all working already, the proper keyboard focus handling was the last bit I needed that I couldn’t work out.

thanks
Post by Alastair Houghton
Kind regards,
Alastair.
--
http://alastairs-place.net <http://alastairs-place.net/>
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email s
Ken Thomases
2018-09-10 16:20:24 UTC
Permalink
Post by iain
I have an application, which has 3 controls
[button] [custom view] [button]
I’ve set the NextKeyView of each of them to the control to the right
[button] -> [custom view] -> [button]
And the tab and shift tab works between them as expected.
The situation gets more complicated because the custom view has 3 possible focus areas, which I also want to be part of the tab chain.
As it’s one big custom drawn widget, I’ve implemented a rudimentary tab handler in the KeyDown: method with a counter to keep track of which area is focused.
And it sort of works
But, I’m wondering how to know if the parent NSView gained keyboard focus through a tab or a shift-tab
If focus is on the first button, and you press Tab, the focus should go to the 1st area of the custom view
If focus is on the last button and you Shift Tab from it, the focus should go to the 3rd area of the custom view
Currently, it always has to go to the area that was focused when the view lost focus.
Is there a way to do what I’m wanting?
I haven't tried it, but have you considered implementing -select{Next,Previous}KeyView: on your view. If you're staying within your view, don't call through to super. Otherwise, do, which will pass it up the responder chain to the window, which will do the normal thing. (Or, you could reimplement what the window would do using -selectKeyView{Following,Preceding}View:.)

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.nar
Continue reading on narkive:
Loading...