[ion] Handle key events in all simulators as in EPSILON_SDL_SCREEN_ONLY

This way, physical keyboard events can be caught by python on all
simulators
This commit is contained in:
Léa Saviot
2020-02-25 11:21:59 +01:00
committed by RubenNumworks
parent 4a4ba52e38
commit 1242258992
3 changed files with 39 additions and 54 deletions

View File

@@ -188,14 +188,12 @@ static Event eventFromSDLTextInputEvent(SDL_TextInputEvent event) {
}
char character = event.text[0];
if (character >= 32 && character < 127) {
#if EPSILON_SDL_SCREEN_ONLY
/* We remove the shift, otherwise it might stay activated when it shouldn't.
* For instance on a French keyboard, to input "1", we first press "Shift"
* (which activates the Shift modifier on the calculator), then we press
* "&", transformed by eventFromSDLTextInputEvent into the text "1". If we
* do not remove the Shift here, it would still be pressed afterwards. */
Ion::Events::removeShift();
#endif
return sEventForASCIICharAbove32[character-32];
}
return None;
@@ -220,11 +218,9 @@ Event getPlatformEvent() {
return Termination;
}
if (event.type == SDL_KEYDOWN) {
#if EPSILON_SDL_SCREEN_ONLY
if (IonSimulatorSDLKeyDetectedByScan(event.key.keysym.scancode)) {
continue;
}
#endif
return eventFromSDLKeyboardEvent(event.key);
}
if (event.type == SDL_TEXTINPUT) {

View File

@@ -17,6 +17,7 @@ void IonSimulatorKeyboardKeyUp(int keyNumber) {
Ion::Keyboard::Key key = static_cast<Ion::Keyboard::Key>(keyNumber);
sKeyboardState.clearKey(key);
}
#endif
namespace Ion {
namespace Keyboard {
@@ -49,6 +50,42 @@ constexpr static KeySDLKeyPair sKeyPairs[] = {
constexpr int sNumberOfKeyPairs = sizeof(sKeyPairs)/sizeof(KeySDLKeyPair);
State scan() {
// We need to tell SDL to get new state from the host OS
SDL_PumpEvents();
// Notify callbacks in case we need to do something
IonSimulatorCallbackDidScanKeyboard();
// Grab this opportunity to refresh the display if needed
Simulator::Main::refresh();
// Start with a "clean" state
State state;
#if EPSILON_SDL_SCREEN_ONLY
// In this case, keyboard states are sent over another channel.
state = sKeyboardState;
#else
// Register a key for the mouse, if any
SDL_Point p;
Uint32 mouseState = SDL_GetMouseState(&p.x, &p.y);
if (mouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) {
Key k = Simulator::Layout::keyAt(&p);
state.setKey(k);
}
#endif
// Catch the physical keyboard events
const uint8_t * SDLstate = SDL_GetKeyboardState(NULL);
for (int i = 0; i < sNumberOfKeyPairs; i++) {
KeySDLKeyPair pair = sKeyPairs[i];
if (SDLstate[pair.SDLKey()]) {
state.setKey(pair.key());
}
}
return state;
}
}
}
@@ -60,48 +97,3 @@ bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key) {
}
return false;
}
#endif
namespace Ion {
namespace Keyboard {
State scan() {
// We need to tell SDL to get new state from the host OS
SDL_PumpEvents();
// Notify callbacks in case we need to do something
IonSimulatorCallbackDidScanKeyboard();
// Grab this opportunity to refresh the display if needed
Simulator::Main::refresh();
#if EPSILON_SDL_SCREEN_ONLY
/* In this case, keyboard states will be sent over another channel, but we
* still need to catch the physical keyboard events */
const uint8_t * SDLstate = SDL_GetKeyboardState(NULL);
State state = sKeyboardState;
for (int i = 0; i < sNumberOfKeyPairs; i++) {
KeySDLKeyPair pair = sKeyPairs[i];
if (SDLstate[pair.SDLKey()]) {
state.setKey(pair.key());
}
}
return state;
#else
// Start with a "clean" state
State state;
// Register a key for the mouse, if any
SDL_Point p;
Uint32 mouseState = SDL_GetMouseState(&p.x, &p.y);
if (mouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) {
Key k = Simulator::Layout::keyAt(&p);
state.setKey(k);
}
return state;
#endif
}
}
}

View File

@@ -2,6 +2,7 @@
#define ION_SIMULATOR_PLATFORM_H
#include <SDL.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
@@ -14,16 +15,12 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
char * IonSimulatorGetLanguageCode();
#if EPSILON_SDL_SCREEN_ONLY
void IonSimulatorKeyboardKeyDown(int keyNumber);
void IonSimulatorKeyboardKeyUp(int keyNumber);
void IonSimulatorEventsPushEvent(int eventNumber);
bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key);
#endif
bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key);
void IonSimulatorCallbackDidRefresh();
void IonSimulatorCallbackDidScanKeyboard();