mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
Catch first key pressed event of CMD, Shift, Alt or Ctrl on OS X
This commit is contained in:
parent
d77fce1b77
commit
5bf065a709
@ -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.
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user