Browse Source

Improved mouse, keyboard, and resize handling for macOS (#1436)

* Suppressed beep on unhandled key-down events on macOS
* Added null check to window resize callback
* Improved mouse-move event handling on macOS
Warren Moore 7 months ago
parent
commit
cf1f2d5fb2

+ 10 - 0
TestFramework/Input/MacOS/KeyboardMacOS.mm

@@ -75,6 +75,16 @@ static EKey sToKey(GCKeyCode inValue)
 - (KeyboardDelegate *)init:(KeyboardMacOS *)Keyboard
 - (KeyboardDelegate *)init:(KeyboardMacOS *)Keyboard
 {
 {
 	mKeyboard = 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;
 	return self;
 }
 }
 
 

+ 1 - 1
TestFramework/Window/ApplicationWindow.h

@@ -29,7 +29,7 @@ public:
 	virtual void					MainLoop(RenderCallback inRenderCallback) = 0;
 	virtual void					MainLoop(RenderCallback inRenderCallback) = 0;
 	
 	
 	/// Function that will trigger the callback
 	/// 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:
 protected:
 	int								mWindowWidth = 1920;
 	int								mWindowWidth = 1920;

+ 7 - 2
TestFramework/Window/ApplicationWindowMacOS.mm

@@ -38,10 +38,15 @@
 	return YES;
 	return YES;
 }
 }
 
 
+- (BOOL)isFlipped {
+    return YES;
+}
+
 - (void)mouseMoved:(NSEvent *)event
 - (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
 - (void)drawInMTKView:(MTKView *)view