Browse Source

Converting key flags to up and down key events.

David Piuva 8 months ago
parent
commit
e658955e67
1 changed files with 28 additions and 10 deletions
  1. 28 10
      Source/windowManagers/CocoaWindow.mm

+ 28 - 10
Source/windowManagers/CocoaWindow.mm

@@ -23,8 +23,10 @@ class CocoaWindow : public dsr::BackendWindow {
 private:
 private:
 	// Handle to the Cocoa window
 	// Handle to the Cocoa window
 	NSWindow *window = nullptr;
 	NSWindow *window = nullptr;
-	// Handle to the Cocoa view
-	//NSView *view = nullptr;
+	// Last modifiers to allow converting NSEventTypeFlagsChanged into up and down key press events.
+	bool pressedControlCommand = false;
+	bool pressedShift = false;
+	bool pressedAltOption = false;
 
 
 	// Double buffering to allow drawing to a canvas while displaying the previous one
 	// Double buffering to allow drawing to a canvas while displaying the previous one
 	// The image which can be drawn to, sharing memory with the Cocoa image
 	// The image which can be drawn to, sharing memory with the Cocoa image
@@ -132,10 +134,6 @@ CocoaWindow::CocoaWindow(const dsr::String& title, int width, int height) {
 	[window center];
 	[window center];
 	[window makeKeyAndOrderFront:nil];
 	[window makeKeyAndOrderFront:nil];
 	[window makeFirstResponder:nil];
 	[window makeFirstResponder:nil];
-	if (![window isKeyWindow]) {
-		// TODO: Why does the window never become key despite being visible and active?
-		dsr::sendWarning(U"Failed to make the Cocoa window key!\n");
-	}
 }
 }
 
 
 // Also locked, but cannot change the name when overriding
 // Also locked, but cannot change the name when overriding
@@ -198,16 +196,36 @@ void CocoaWindow::prefetchEvents() {
 				// TODO: Make sure that the window catches keyboard events instead of corrupting terminal input.
 				// TODO: Make sure that the window catches keyboard events instead of corrupting terminal input.
 				if ([event type] == NSEventTypeKeyDown) {
 				if ([event type] == NSEventTypeKeyDown) {
 					if (!(event.isARepeat)) {
 					if (!(event.isARepeat)) {
-						dsr::printText(U"KeyDown\n");
+						dsr::printText(U"KeyDown: keyCode = ", event.keyCode, U"\n");
 					}
 					}
 					dsr::printText(U"KeyType\n");
 					dsr::printText(U"KeyType\n");
 				} else if ([event type] == NSEventTypeKeyUp) {
 				} else if ([event type] == NSEventTypeKeyUp) {
-					dsr::printText(U"KeyUp\n");
+					dsr::printText(U"KeyUp: keyCode = ", event.keyCode, U"\n");
 				} else if ([event type] == NSEventTypeFlagsChanged) {
 				} else if ([event type] == NSEventTypeFlagsChanged) {
 					dsr::printText(U"FlagsChanged\n");
 					dsr::printText(U"FlagsChanged\n");
+					NSEventModifierFlags newModifierFlags = [event modifierFlags];
+					bool newControlCommand = (newModifierFlags & (NSEventModifierFlagControl | NSEventModifierFlagCommand)) != 0u;
+					bool newShift = (newModifierFlags & NSEventModifierFlagShift) != 0u;
+					bool newAltOption = (newModifierFlags & NSEventModifierFlagOption) != 0u;
+					if (newControlCommand && !pressedControlCommand) {
+						dsr::printText(U"KeyDown: Control\n");
+					} else if (!newControlCommand && pressedControlCommand) {
+						dsr::printText(U"KeyUp: Control\n");
+					}
+					if (newShift && !pressedShift) {
+						dsr::printText(U"KeyDown: Shift\n");
+					} else if (!newShift && pressedShift) {
+						dsr::printText(U"KeyUp: Shift\n");
+					}
+					if (newAltOption && !pressedAltOption) {
+						dsr::printText(U"KeyDown: Alt\n");
+					} else if (!newAltOption && pressedAltOption) {
+						dsr::printText(U"KeyUp: Alt\n");
+					}
+					pressedControlCommand = newControlCommand;
+					pressedShift = newShift;
+					pressedAltOption = newAltOption;
 				}
 				}
-				dsr::printText(U"keyCode = ", event.keyCode, U"\n");
-				
 			}
 			}
 			[application sendEvent:event];
 			[application sendEvent:event];
 			[application updateWindows];
 			[application updateWindows];