NSWindow does not receive keyboard events

I am creating NSWindow from a package loaded at runtime, so I do not have the source code for NSWindow that is created when the application starts. When I show my window, it receives a mouse event, but it does not receive any keyboard events. I tried to add a window with the following methods:

[_myWindow makeKeyAndOrderFront:nil]; [[NSApplication sharedApplication].mainWindow addChildWindow:_myWindow ordered:NSWindowAbove]; [NSApp beginModalSessionForWindow:_myWindow]; 

In all three cases, the main window appears, where there are all keyboard events. Mouse events also leak into the original window (I can see how the mouse above the backlight and the buttons respond to clicks through my window). I also tried:

 [_myWindow makeMainWindow]; [_myWindow orderWindow:NSWindowAbove relativeTo:[originalWindow windowNumber]]; 

Any tips on getting keyboard events for my window are welcome.

+4
source share
1 answer

You can subclass NSApplication and override sendEvent: to print [[NSApplication sharedApplication] keyWindow] before sending the event to keyWindow (remember to call super to actually send the event).

If the key window does not accept events, then you can track events somewhere that captures events before sending them.

0
source

Source: https://habr.com/ru/post/1391567/


All Articles