We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5237 - Keyboard events are not received in cocoa app when not starting from SDL_main
Summary: Keyboard events are not received in cocoa app when not starting from SDL_main
Status: NEW
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: 2.0.12
Hardware: x86_64 macOS 10.15
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-17 21:28 UTC by Amir Amitai
Modified: 2020-07-18 09:58 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Amir Amitai 2020-07-17 21:28:50 UTC
My setup is like this:
From a WDL/SWELL main application (only a wrapper) I'm creating an SDL window on macOS - therefore an instance of SDLApplication is not being set.

After polling event with SDL_PollEvent,
SDL_KEYDOWN, and SDL_KEYUP are not being called.
You can force it to call if you smash the keyboard and it gets some keystrokes here and there.

SDL_GetModState() returns always zero and SDL_GetKeyboardState(NULL) never changes.

Debugging this, leads me to the problematic line:
SDL_cocoaevents.m:
NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];

if we change it to 
NSEvent *nextDown = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate dateWithTimeIntervalSinceNow:0.01] inMode:NSDefaultRunLoopMode dequeue:YES];

After this, it started work, but it's too much time waiting there polling the event. I was looking everywhere what could be dequeuing this event. 

Eventually I have found the SDLWindow class, in SDL_cocoawindow.m.
It has the same callback as SDLApplication - sendEvent. It receives all the keystrokes in time like a charm. from there I'm pushing it back to SDL's pipe by calling Cocoa_DispatchEvent(event).

It looks that the key handling events is on the NSApplication (SDLApplication) and not on the NSWindon (SDLWindow)


here's my working patch (needs a bit of tidying):
https://github.com/amiramitai/SDL-mirror/commit/5b5b818795e39b751c65b8e6c9bd908a4a0df853