Skip to content

Commit

Permalink
Improved mouse, keyboard, and resize handling for macOS (#1436)
Browse files Browse the repository at this point in the history
* Suppressed beep on unhandled key-down events on macOS
* Added null check to window resize callback
* Improved mouse-move event handling on macOS
  • Loading branch information
warrenm authored Jan 4, 2025
1 parent a9d7cd1 commit cf1f2d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions TestFramework/Input/MacOS/KeyboardMacOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ @implementation KeyboardDelegate
- (KeyboardDelegate *)init:(KeyboardMacOS *)Keyboard
{
mKeyboard = Keyboard;

[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent *(NSEvent *event) {
// Ignore all keystrokes except Command-Q (Quit).
if ((event.modifierFlags & NSEventModifierFlagCommand) && [event.charactersIgnoringModifiers isEqual:@"q"]) {
return event;
} else {
return nil;
}
}];

return self;
}

Expand Down
2 changes: 1 addition & 1 deletion TestFramework/Window/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ApplicationWindow
virtual void MainLoop(RenderCallback inRenderCallback) = 0;

/// Function that will trigger the callback
void OnWindowResized(int inWidth, int inHeight) { mWindowWidth = inWidth; mWindowHeight = inHeight; mWindowResizeListener(); }
void OnWindowResized(int inWidth, int inHeight) { mWindowWidth = inWidth; mWindowHeight = inHeight; if (mWindowResizeListener) { mWindowResizeListener(); } }

protected:
int mWindowWidth = 1920;
Expand Down
9 changes: 7 additions & 2 deletions TestFramework/Window/ApplicationWindowMacOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ - (bool)canBecomeKeyView
return YES;
}

- (BOOL)isFlipped {
return YES;
}

- (void)mouseMoved:(NSEvent *)event
{
NSPoint location = [event locationInWindow];
mWindow->OnMouseMoved(location.x, mWindow->GetWindowHeight() - location.y);
NSPoint locationInView = [self convertPoint:event.locationInWindow fromView:nil];
NSPoint locationInBacking = [self convertPointToBacking:locationInView];
mWindow->OnMouseMoved(locationInBacking.x, -locationInBacking.y);
}

- (void)drawInMTKView:(MTKView *)view
Expand Down

0 comments on commit cf1f2d5

Please sign in to comment.