OSX not capturing keyboard input

Started by
2 comments, last by JoshuaWaring 8 years, 9 months ago

Hello, I'm trying to capture keyboard input for a Window on Yosemite.

When I launch the app my mouse events are caught by the window, although any keyboard events don't trigger my NSKeyDown in sendEvent function

How I've implemented my input handling is that I've overloaded the NSApplication sendEvent, provided a switch case for all event types and then pass the event on [super sendEvent:event] if I don't have a condition for it.

Then in my main loop I check for new events here

void CocoaUpdateWindow(){

NSEvent *event;

while((event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES]) != nil)

[NSApp sendEvent:event];

}

I got this idea from http://stackoverflow.com/questions/6732400/cocoa-integrate-nsapplication-into-an-existing-c-mainloop

Thank you for any help :)

Also I when I do type on the window, XCode in the background is receiving the input so the events aren't even getting passed to the window in the first place.

Advertisement

AFAIK you need to ensure that your window returns YES on canBecomeKeyWindow and probably also on canBecomeMainWindow. Whether this is the default depends on the style of your window, e.g. whether it is borderless or else has a title or resizing widget. However, the fact that XCode still receives keystrokes make me think that your window is not key window / main window.

Personally, I try to avoid using Obj-C when on OSX, so I use SDL to handle all that in my engine. Is this an option for you?

Shogun.

Hate to revive a old post, but I found the solution now that I've come back to the problem.

Firstly I had to get the window to focus correctly, so I found some code which made the window a foreground application.


[application activateIgnoringOtherApps:YES];
// Set as a forground application
ProcessSerialNumber psn = {0, kCurrentProcess};
TransformProcessType(&psn, kProcessTransformToForegroundApplication);

[application finishLaunching];

but after getting the input to not just pass through to any application in the background, I still couldn't get the events from the window with my nextEventMatchingMask, until it occurred to me that I'm not calling that function from the main thread. So now that it's being called from the main thread the window behaves like expected.

https://github.com/Protheus-Engine/Protheus/blob/devel/src/Core/Graphics/Window_OSX.mm

This topic is closed to new replies.

Advertisement