Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get "focus" to a specific NSView programmatically?

I switch from displaying one NSView to another. But to be able to receive keystrokes typed in from the user, the user must first click the mouse on the NSView. I want the user to be able to type-in without having to click on the NSView...

like image 697
jdl Avatar asked Sep 03 '25 14:09

jdl


1 Answers

There is a good guide from Apple on Event Handling Basics. See the "Preparing a Custom View for Receiving Events" and "Setting the First Responder" sections.

TL;DR: You have to override acceptsFirstResponder on your custom NSView to return true and then call makeFirstResponder on the NSWindow with your view as argument.

like image 180
Nef10 Avatar answered Sep 05 '25 15:09

Nef10