From 5bf065a709f6528b056fde2d28501995aed6bbc7 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Sat, 21 Sep 2013 14:34:33 +0200 Subject: [PATCH] Catch first key pressed event of CMD, Shift, Alt or Ctrl on OS X --- .../Window/OSX/SFKeyboardModifiersHelper.h | 8 +++ .../Window/OSX/SFKeyboardModifiersHelper.mm | 51 ++++++++----------- src/SFML/Window/OSX/WindowImplCocoa.mm | 7 +++ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/SFML/Window/OSX/SFKeyboardModifiersHelper.h b/src/SFML/Window/OSX/SFKeyboardModifiersHelper.h index b4dd32f42..20eb74586 100644 --- a/src/SFML/Window/OSX/SFKeyboardModifiersHelper.h +++ b/src/SFML/Window/OSX/SFKeyboardModifiersHelper.h @@ -43,6 +43,14 @@ namespace sf { //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////// +/// Init the global state (only if needed). It needs to be +/// Called before any event, e.g. in the window constructor. +/// +//////////////////////////////////////////////////////////// +void initialiseKeyboardHelper(); + + //////////////////////////////////////////////////////////// /// Set up a SFML key event based on the given modifiers /// flags and key code. diff --git a/src/SFML/Window/OSX/SFKeyboardModifiersHelper.mm b/src/SFML/Window/OSX/SFKeyboardModifiersHelper.mm index 8c06b9971..3c32f83b3 100644 --- a/src/SFML/Window/OSX/SFKeyboardModifiersHelper.mm +++ b/src/SFML/Window/OSX/SFKeyboardModifiersHelper.mm @@ -84,13 +84,6 @@ static BOOL isStateInitialized = NO; BOOL isKeyMaskActive(NSUInteger modifiers, NSUInteger mask); -//////////////////////////////////////////////////////// -/// Init the global state only if needed -/// -//////////////////////////////////////////////////////////// -void ensureModifiersStateIsInitilized(); - - //////////////////////////////////////////////////////////// /// Handle one modifier key mask, update the key state and /// send events to the requester @@ -119,6 +112,27 @@ void processLeftRightModifiers(NSUInteger modifiers, //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////// +void initialiseKeyboardHelper() +{ + if (isStateInitialized) return; + + NSUInteger modifiers = [[NSApp currentEvent] modifierFlags]; + + // Load current keyboard state + state.leftShiftWasDown = isKeyMaskActive(modifiers, NSLeftShiftKeyMask); + state.rightShiftWasDown = isKeyMaskActive(modifiers, NSRightShiftKeyMask); + state.leftCommandWasDown = isKeyMaskActive(modifiers, NSLeftCommandKeyMask); + state.rightCommandWasDown = isKeyMaskActive(modifiers, NSRightCommandKeyMask); + state.leftAlternateWasDown = isKeyMaskActive(modifiers, NSLeftAlternateKeyMask); + state.rightAlternateWasDown = isKeyMaskActive(modifiers, NSRightAlternateKeyMask); + state.leftControlWasDown = isKeyMaskActive(modifiers, NSLeftControlKeyMask); + state.rightControlWasDown = isKeyMaskActive(modifiers, NSRightControlKeyMask); + + isStateInitialized = YES; +} + + //////////////////////////////////////////////////////// sf::Event::KeyEvent keyEventWithModifiers(NSUInteger modifiers, sf::Keyboard::Key key) { @@ -184,34 +198,11 @@ BOOL isKeyMaskActive(NSUInteger modifiers, NSUInteger mask) } -//////////////////////////////////////////////////////// -void ensureModifiersStateIsInitilized() -{ - if (isStateInitialized) return; - - NSUInteger modifiers = [[NSApp currentEvent] modifierFlags]; - - // Load current keyboard state - state.leftShiftWasDown = isKeyMaskActive(modifiers, NSLeftShiftKeyMask); - state.rightShiftWasDown = isKeyMaskActive(modifiers, NSRightShiftKeyMask); - state.leftCommandWasDown = isKeyMaskActive(modifiers, NSLeftCommandKeyMask); - state.rightCommandWasDown = isKeyMaskActive(modifiers, NSRightCommandKeyMask); - state.leftAlternateWasDown = isKeyMaskActive(modifiers, NSLeftAlternateKeyMask); - state.rightAlternateWasDown = isKeyMaskActive(modifiers, NSRightAlternateKeyMask); - state.leftControlWasDown = isKeyMaskActive(modifiers, NSLeftControlKeyMask); - state.rightControlWasDown = isKeyMaskActive(modifiers, NSRightControlKeyMask); - - isStateInitialized = YES; -} - - //////////////////////////////////////////////////////// void processOneModifier(NSUInteger modifiers, NSUInteger mask, BOOL& wasDown, sf::Keyboard::Key key, sf::priv::WindowImplCocoa& requester) { - ensureModifiersStateIsInitilized(); - // Setup a potential event key. sf::Event::KeyEvent event = keyEventWithModifiers(modifiers, key); diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm index e15bf0c0e..b06ea99ab 100644 --- a/src/SFML/Window/OSX/WindowImplCocoa.mm +++ b/src/SFML/Window/OSX/WindowImplCocoa.mm @@ -36,6 +36,7 @@ #import #import #import +#import namespace sf { @@ -78,6 +79,9 @@ WindowImplCocoa::WindowImplCocoa(WindowHandle handle) } [m_delegate setRequesterTo:this]; + + // Finally, set up keyboard helper + initialiseKeyboardHelper(); } @@ -97,6 +101,9 @@ WindowImplCocoa::WindowImplCocoa(VideoMode mode, m_delegate = [[SFWindowController alloc] initWithMode:mode andStyle:style]; [m_delegate changeTitle:sfStringToNSString(title)]; [m_delegate setRequesterTo:this]; + + // Finally, set up keyboard helper + initialiseKeyboardHelper(); }