Direction that NSView received focus (tab or backtab)?












0















I have a custom view that has multiple components that can receive the focus, similar to NSDatePicker in the textFieldAndStepper style. If it gets the focus the first component is focused. You can then tab through the components. If you hit tab while the last component has the focus, the next view in the window gets the focus. When you use backtab (shift + tab) it goes in the other direction. How would I do that?



I currently move the focus along the components in the keyDown event, and I override becomeFirstResponder and resignFirstResponder to switch on or switch off the focus when the user tabs beyond the last component or backtabs beyond the first component.



However in becomeFirstResponder I cannot tell how the view got the focus. It could have been a tab or a backtab or a mouse click.



How do I get this information in becomeFirstResponder, or is there another way to solve this problem.



Note that I don't want to introduce subviews, for various reasons.



Here is the code I have so far:



private var selectedComponent: Int?

override public var acceptsFirstResponder: Bool {
get { return true }
}

override public func becomeFirstResponder() -> Bool {
selectedComponent = 0
needsDisplay = true
return true
}

override public func resignFirstResponder() -> Bool {
selectedComponent = nil
needsDisplay = true
return true
}

override public func keyDown(with event: NSEvent) {
guard let selectedComponent = selectedComponent else {
self.nextResponder?.keyDown(with: event)
return
}

guard let specialKey = event.specialKey else {
self.nextResponder?.keyDown(with: event)
return
}

if specialKey == NSEvent.SpecialKey.tab && selectedComponent < componentFormats.count - 1 {
self.selectedComponent = selectedComponent + 1
needsDisplay = true
} else if specialKey == NSEvent.SpecialKey.backTab && selectedComponent > 0 {
self.selectedComponent = selectedComponent - 1
needsDisplay = true
} else {
self.nextResponder?.keyDown(with: event)
}
}









share|improve this question


















  • 1





    Is currentEvent of NSWindow what you're looking for?

    – Willeke
    Nov 20 '18 at 22:01











  • @Willeke, that seems to work. Thank you.

    – mistercake
    Nov 21 '18 at 20:32
















0















I have a custom view that has multiple components that can receive the focus, similar to NSDatePicker in the textFieldAndStepper style. If it gets the focus the first component is focused. You can then tab through the components. If you hit tab while the last component has the focus, the next view in the window gets the focus. When you use backtab (shift + tab) it goes in the other direction. How would I do that?



I currently move the focus along the components in the keyDown event, and I override becomeFirstResponder and resignFirstResponder to switch on or switch off the focus when the user tabs beyond the last component or backtabs beyond the first component.



However in becomeFirstResponder I cannot tell how the view got the focus. It could have been a tab or a backtab or a mouse click.



How do I get this information in becomeFirstResponder, or is there another way to solve this problem.



Note that I don't want to introduce subviews, for various reasons.



Here is the code I have so far:



private var selectedComponent: Int?

override public var acceptsFirstResponder: Bool {
get { return true }
}

override public func becomeFirstResponder() -> Bool {
selectedComponent = 0
needsDisplay = true
return true
}

override public func resignFirstResponder() -> Bool {
selectedComponent = nil
needsDisplay = true
return true
}

override public func keyDown(with event: NSEvent) {
guard let selectedComponent = selectedComponent else {
self.nextResponder?.keyDown(with: event)
return
}

guard let specialKey = event.specialKey else {
self.nextResponder?.keyDown(with: event)
return
}

if specialKey == NSEvent.SpecialKey.tab && selectedComponent < componentFormats.count - 1 {
self.selectedComponent = selectedComponent + 1
needsDisplay = true
} else if specialKey == NSEvent.SpecialKey.backTab && selectedComponent > 0 {
self.selectedComponent = selectedComponent - 1
needsDisplay = true
} else {
self.nextResponder?.keyDown(with: event)
}
}









share|improve this question


















  • 1





    Is currentEvent of NSWindow what you're looking for?

    – Willeke
    Nov 20 '18 at 22:01











  • @Willeke, that seems to work. Thank you.

    – mistercake
    Nov 21 '18 at 20:32














0












0








0








I have a custom view that has multiple components that can receive the focus, similar to NSDatePicker in the textFieldAndStepper style. If it gets the focus the first component is focused. You can then tab through the components. If you hit tab while the last component has the focus, the next view in the window gets the focus. When you use backtab (shift + tab) it goes in the other direction. How would I do that?



I currently move the focus along the components in the keyDown event, and I override becomeFirstResponder and resignFirstResponder to switch on or switch off the focus when the user tabs beyond the last component or backtabs beyond the first component.



However in becomeFirstResponder I cannot tell how the view got the focus. It could have been a tab or a backtab or a mouse click.



How do I get this information in becomeFirstResponder, or is there another way to solve this problem.



Note that I don't want to introduce subviews, for various reasons.



Here is the code I have so far:



private var selectedComponent: Int?

override public var acceptsFirstResponder: Bool {
get { return true }
}

override public func becomeFirstResponder() -> Bool {
selectedComponent = 0
needsDisplay = true
return true
}

override public func resignFirstResponder() -> Bool {
selectedComponent = nil
needsDisplay = true
return true
}

override public func keyDown(with event: NSEvent) {
guard let selectedComponent = selectedComponent else {
self.nextResponder?.keyDown(with: event)
return
}

guard let specialKey = event.specialKey else {
self.nextResponder?.keyDown(with: event)
return
}

if specialKey == NSEvent.SpecialKey.tab && selectedComponent < componentFormats.count - 1 {
self.selectedComponent = selectedComponent + 1
needsDisplay = true
} else if specialKey == NSEvent.SpecialKey.backTab && selectedComponent > 0 {
self.selectedComponent = selectedComponent - 1
needsDisplay = true
} else {
self.nextResponder?.keyDown(with: event)
}
}









share|improve this question














I have a custom view that has multiple components that can receive the focus, similar to NSDatePicker in the textFieldAndStepper style. If it gets the focus the first component is focused. You can then tab through the components. If you hit tab while the last component has the focus, the next view in the window gets the focus. When you use backtab (shift + tab) it goes in the other direction. How would I do that?



I currently move the focus along the components in the keyDown event, and I override becomeFirstResponder and resignFirstResponder to switch on or switch off the focus when the user tabs beyond the last component or backtabs beyond the first component.



However in becomeFirstResponder I cannot tell how the view got the focus. It could have been a tab or a backtab or a mouse click.



How do I get this information in becomeFirstResponder, or is there another way to solve this problem.



Note that I don't want to introduce subviews, for various reasons.



Here is the code I have so far:



private var selectedComponent: Int?

override public var acceptsFirstResponder: Bool {
get { return true }
}

override public func becomeFirstResponder() -> Bool {
selectedComponent = 0
needsDisplay = true
return true
}

override public func resignFirstResponder() -> Bool {
selectedComponent = nil
needsDisplay = true
return true
}

override public func keyDown(with event: NSEvent) {
guard let selectedComponent = selectedComponent else {
self.nextResponder?.keyDown(with: event)
return
}

guard let specialKey = event.specialKey else {
self.nextResponder?.keyDown(with: event)
return
}

if specialKey == NSEvent.SpecialKey.tab && selectedComponent < componentFormats.count - 1 {
self.selectedComponent = selectedComponent + 1
needsDisplay = true
} else if specialKey == NSEvent.SpecialKey.backTab && selectedComponent > 0 {
self.selectedComponent = selectedComponent - 1
needsDisplay = true
} else {
self.nextResponder?.keyDown(with: event)
}
}






swift focus nsview appkit nsresponder






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 18:09









mistercakemistercake

404311




404311








  • 1





    Is currentEvent of NSWindow what you're looking for?

    – Willeke
    Nov 20 '18 at 22:01











  • @Willeke, that seems to work. Thank you.

    – mistercake
    Nov 21 '18 at 20:32














  • 1





    Is currentEvent of NSWindow what you're looking for?

    – Willeke
    Nov 20 '18 at 22:01











  • @Willeke, that seems to work. Thank you.

    – mistercake
    Nov 21 '18 at 20:32








1




1





Is currentEvent of NSWindow what you're looking for?

– Willeke
Nov 20 '18 at 22:01





Is currentEvent of NSWindow what you're looking for?

– Willeke
Nov 20 '18 at 22:01













@Willeke, that seems to work. Thank you.

– mistercake
Nov 21 '18 at 20:32





@Willeke, that seems to work. Thank you.

– mistercake
Nov 21 '18 at 20:32












1 Answer
1






active

oldest

votes


















0














It sounds like you do have your problem sorted out now. But, I was thinking, would it be possible to get the same effect by overriding NSView's nextKeyView and previousKeyView? This should provide the directionality you need, but also might interact better with the system overall (ie Accessibility) than just via keydowns.






share|improve this answer



















  • 1





    I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

    – mistercake
    Nov 24 '18 at 18:54











  • It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

    – Mattie
    Nov 27 '18 at 20:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399013%2fdirection-that-nsview-received-focus-tab-or-backtab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It sounds like you do have your problem sorted out now. But, I was thinking, would it be possible to get the same effect by overriding NSView's nextKeyView and previousKeyView? This should provide the directionality you need, but also might interact better with the system overall (ie Accessibility) than just via keydowns.






share|improve this answer



















  • 1





    I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

    – mistercake
    Nov 24 '18 at 18:54











  • It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

    – Mattie
    Nov 27 '18 at 20:35
















0














It sounds like you do have your problem sorted out now. But, I was thinking, would it be possible to get the same effect by overriding NSView's nextKeyView and previousKeyView? This should provide the directionality you need, but also might interact better with the system overall (ie Accessibility) than just via keydowns.






share|improve this answer



















  • 1





    I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

    – mistercake
    Nov 24 '18 at 18:54











  • It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

    – Mattie
    Nov 27 '18 at 20:35














0












0








0







It sounds like you do have your problem sorted out now. But, I was thinking, would it be possible to get the same effect by overriding NSView's nextKeyView and previousKeyView? This should provide the directionality you need, but also might interact better with the system overall (ie Accessibility) than just via keydowns.






share|improve this answer













It sounds like you do have your problem sorted out now. But, I was thinking, would it be possible to get the same effect by overriding NSView's nextKeyView and previousKeyView? This should provide the directionality you need, but also might interact better with the system overall (ie Accessibility) than just via keydowns.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 15:15









MattieMattie

1,39711323




1,39711323








  • 1





    I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

    – mistercake
    Nov 24 '18 at 18:54











  • It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

    – Mattie
    Nov 27 '18 at 20:35














  • 1





    I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

    – mistercake
    Nov 24 '18 at 18:54











  • It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

    – Mattie
    Nov 27 '18 at 20:35








1




1





I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

– mistercake
Nov 24 '18 at 18:54





I don't understand your proposed solution. What would I return in the getters of these properties? The view itself? Also, what would the setters do?

– mistercake
Nov 24 '18 at 18:54













It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

– Mattie
Nov 27 '18 at 20:35





It's my mistake. I didn't look closely enough at these APIs before making the suggestion. While there might still be something to the idea of hooking into the next key view mechanism of NSView/NSWindow itself, these particular properties won't get you want you need.

– Mattie
Nov 27 '18 at 20:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399013%2fdirection-that-nsview-received-focus-tab-or-backtab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?