Handle modifier keys differently to avoid Apple flagging us as wishing to monitor input from other applications

Fixes #291
This commit is contained in:
2023-01-12 21:43:45 -05:00
parent 35a890a577
commit 3bdcf02be0
13 changed files with 221 additions and 88 deletions

View File

@@ -5,6 +5,7 @@ tools = Split("""
undo.cpp
framerate_limiter.cpp
drawable_manager.cpp
keymods.cpp
../location.cpp
../mathutil.cpp
../porting.cpp

93
src/tools/keymods.cpp Normal file
View File

@@ -0,0 +1,93 @@
//
// keymods.cpp
// Common
//
// Created by Celtic Minstrel on 2023-01-12.
//
#include "keymods.hpp"
#include <SFML/Window/Keyboard.hpp>
keymods_t kb;
bool keymods_t::isAltPressed() const {
return alt;
}
bool keymods_t::isCtrlPressed() const {
return ctrl;
}
bool keymods_t::isShiftPressed() const {
return shift;
}
bool keymods_t::isMetaPressed() const {
return meta;
}
bool keymods_t::isSystemPressed() const {
#ifdef __APPLE__
return meta;
#else
return ctrl;
#endif
}
bool keymods_t::isUpPressed() const {
return up;
}
bool keymods_t::isDownPressed() const {
return down;
}
bool keymods_t::isLeftPressed() const {
return left;
}
bool keymods_t::isRightPressed() const {
return right;
}
bool keymods_t::handleModifier(const sf::Event& evt) {
if(evt.type != sf::Event::KeyPressed && evt.type != sf::Event::KeyReleased) {
return false;
}
using Key = sf::Keyboard::Key;
bool newState = evt.type == sf::Event::KeyPressed;
switch(evt.key.code) {
case Key::LShift:
case Key::RShift:
shift = newState;
break;
case Key::LAlt:
case Key::RAlt:
alt = newState;
break;
case Key::LControl:
case Key::RControl:
ctrl = newState;
break;
case Key::LSystem:
case Key::RSystem:
meta = newState;
break;
case Key::Left:
left = newState;
break;
case Key::Right:
right = newState;
break;
case Key::Up:
up = newState;
break;
case Key::Down:
down = newState;
break;
default: return false;
}
return true;
}

33
src/tools/keymods.hpp Normal file
View File

@@ -0,0 +1,33 @@
//
// keymods.hpp
// Common
//
// Created by Celtic Minstrel on 2023-01-12.
//
#ifndef BOE_keymods_hpp
#define BOE_keymods_hpp
#include <SFML/Window/Event.hpp>
class keymods_t {
bool alt = false, ctrl = false, shift = false, meta = false;
bool up = false, down = false, left = false, right = false;
public:
bool isAltPressed() const;
bool isCtrlPressed() const;
bool isShiftPressed() const;
// Windows key, Command key, Meta key, etc
bool isMetaPressed() const;
// System is a special pseudo-key - it's Command on Mac and Control on other systems
bool isSystemPressed() const;
bool isUpPressed() const;
bool isDownPressed() const;
bool isLeftPressed() const;
bool isRightPressed() const;
bool handleModifier(const sf::Event& evt);
};
extern keymods_t kb;
#endif /* keymods_hpp */