Sign in to follow this  
JoshuaWaring

OSX not capturing keyboard input

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this