Catch first key pressed event of CMD, Shift, Alt or Ctrl on OS X

This commit is contained in:
Marco Antognini 2013-09-21 14:34:33 +02:00
parent d77fce1b77
commit 5bf065a709
3 changed files with 36 additions and 30 deletions

View File

@ -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.

View File

@ -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);

View File

@ -36,6 +36,7 @@
#import <SFML/Window/OSX/AutoreleasePoolWrapper.h>
#import <SFML/Window/OSX/SFApplication.h>
#import <SFML/Window/OSX/SFApplicationDelegate.h>
#import <SFML/Window/OSX/SFKeyboardModifiersHelper.h>
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();
}