From 389e99bf2c6fc5a04940789012db3f72a414ad55 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 5 Sep 2013 18:55:44 -0300 Subject: [PATCH] Added love.keyboard.setTextInput to enable/disable text input events (enabled by default.) This fixes the issue of receiving an unwanted text input event if a key is used to bring a game's textbox into focus, since textinput events can be triggered after keypress events if the keypress generates text. --- src/modules/keyboard/Keyboard.h | 11 +++++++++++ src/modules/keyboard/sdl/Keyboard.cpp | 13 +++++++++++++ src/modules/keyboard/sdl/Keyboard.h | 3 +++ src/modules/keyboard/wrap_Keyboard.cpp | 14 ++++++++++++++ src/modules/keyboard/wrap_Keyboard.h | 2 ++ 5 files changed, 43 insertions(+) diff --git a/src/modules/keyboard/Keyboard.h b/src/modules/keyboard/Keyboard.h index f01174909..4a92180be 100644 --- a/src/modules/keyboard/Keyboard.h +++ b/src/modules/keyboard/Keyboard.h @@ -254,6 +254,17 @@ public: **/ virtual bool isDown(Key *keylist) const = 0; + /** + * Sets whether text input events should be sent + * @param enable Whether to send text input events. + **/ + virtual void setTextInput(bool enable) = 0; + + /** + * Gets whether text input events are enabled. + **/ + virtual bool hasTextInput() const = 0; + static bool getConstant(const char *in, Key &out); static bool getConstant(Key in, const char *&out); diff --git a/src/modules/keyboard/sdl/Keyboard.cpp b/src/modules/keyboard/sdl/Keyboard.cpp index 15490430e..a91cdc4de 100644 --- a/src/modules/keyboard/sdl/Keyboard.cpp +++ b/src/modules/keyboard/sdl/Keyboard.cpp @@ -64,6 +64,19 @@ bool Keyboard::isDown(Key *keylist) const return false; } +void Keyboard::setTextInput(bool enable) +{ + if (enable) + SDL_StartTextInput(); + else + SDL_StopTextInput(); +} + +bool Keyboard::hasTextInput() const +{ + return SDL_IsTextInputActive(); +} + std::map Keyboard::createKeyMap() { std::map k; diff --git a/src/modules/keyboard/sdl/Keyboard.h b/src/modules/keyboard/sdl/Keyboard.h index 29a053e49..eb49e01b3 100644 --- a/src/modules/keyboard/sdl/Keyboard.h +++ b/src/modules/keyboard/sdl/Keyboard.h @@ -51,6 +51,9 @@ public: bool hasKeyRepeat() const; bool isDown(Key *keylist) const; + void setTextInput(bool enable); + bool hasTextInput() const; + private: bool key_repeat; diff --git a/src/modules/keyboard/wrap_Keyboard.cpp b/src/modules/keyboard/wrap_Keyboard.cpp index 47a3493a9..2b638f59e 100644 --- a/src/modules/keyboard/wrap_Keyboard.cpp +++ b/src/modules/keyboard/wrap_Keyboard.cpp @@ -62,11 +62,25 @@ int w_isDown(lua_State *L) return 1; } +int w_setTextInput(lua_State *L) +{ + instance->setTextInput(luax_toboolean(L, 1)); + return 0; +} + +int w_hasTextInput(lua_State *L) +{ + luax_pushboolean(L, instance->hasTextInput()); + return 1; +} + // List of functions to wrap. static const luaL_Reg functions[] = { { "setKeyRepeat", w_setKeyRepeat }, { "hasKeyRepeat", w_hasKeyRepeat }, + { "setTextInput", w_setTextInput }, + { "hasTextInput", w_hasTextInput }, { "isDown", w_isDown }, { 0, 0 } }; diff --git a/src/modules/keyboard/wrap_Keyboard.h b/src/modules/keyboard/wrap_Keyboard.h index 7d370e00f..f872a2dcf 100644 --- a/src/modules/keyboard/wrap_Keyboard.h +++ b/src/modules/keyboard/wrap_Keyboard.h @@ -32,6 +32,8 @@ namespace keyboard int w_setKeyRepeat(lua_State *L); int w_hasKeyRepeat(lua_State *L); int w_isDown(lua_State *L); +int w_setTextInput(lua_State *L); +int w_hasTextInput(lua_State *L); extern "C" LOVE_EXPORT int luaopen_love_keyboard(lua_State *L); } // keyboard