Jump to content
  • Advertisement
Sign in to follow this  

OSX not capturing keyboard input

This topic is 1184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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?



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.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!