From eb052d99e8dfd9ea3d66d5a2e2a2bbbc7522c186 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 29 Aug 2013 03:20:27 -0300 Subject: [PATCH] Added love.keyboard.setKeyRepeat and love.keyboard.hasKeyRepeat (resolves issue #717) --- src/modules/event/sdl/Event.cpp | 9 +++++++++ src/modules/keyboard/Keyboard.h | 12 ++++++++++++ src/modules/keyboard/sdl/Keyboard.cpp | 15 +++++++++++++++ src/modules/keyboard/sdl/Keyboard.h | 7 +++++++ src/modules/keyboard/wrap_Keyboard.cpp | 16 +++++++++++++++- src/modules/keyboard/wrap_Keyboard.h | 2 ++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index a28b2c041..0c7385ee9 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -97,6 +97,8 @@ Message *Event::convert(const SDL_Event &e) const { Message *msg = NULL; + love::keyboard::Keyboard *kb = 0; + love::keyboard::Keyboard::Key key; love::mouse::Mouse::Button button; Variant *arg1, *arg2, *arg3; @@ -106,6 +108,13 @@ Message *Event::convert(const SDL_Event &e) const switch (e.type) { case SDL_KEYDOWN: + if (e.key.repeat) + { + kb = (love::keyboard::Keyboard *) Module::findInstance("love.keyboard."); + if (kb && !kb->hasKeyRepeat()) + break; + } + keyit = keys.find(e.key.keysym.sym); if (keyit != keys.end()) key = keyit->second; diff --git a/src/modules/keyboard/Keyboard.h b/src/modules/keyboard/Keyboard.h index ace45422b..f01174909 100644 --- a/src/modules/keyboard/Keyboard.h +++ b/src/modules/keyboard/Keyboard.h @@ -235,6 +235,18 @@ public: virtual ~Keyboard() {} + /** + * Sets whether repeat keypress events should be sent if a key is held down. + * Does not affect text input events. + * @param enable Whether to send repeat key press events. + **/ + virtual void setKeyRepeat(bool enable) = 0; + + /** + * Gets whether repeat keypress events will be sent if a key is held down. + **/ + virtual bool hasKeyRepeat() const = 0; + /** * Checks whether a certain key is down or not. * @param key A key identifier. diff --git a/src/modules/keyboard/sdl/Keyboard.cpp b/src/modules/keyboard/sdl/Keyboard.cpp index c1a52f3f9..15490430e 100644 --- a/src/modules/keyboard/sdl/Keyboard.cpp +++ b/src/modules/keyboard/sdl/Keyboard.cpp @@ -29,11 +29,26 @@ namespace keyboard namespace sdl { +Keyboard::Keyboard() + : key_repeat(false) +{ +} + const char *Keyboard::getName() const { return "love.keyboard.sdl"; } +void Keyboard::setKeyRepeat(bool enable) +{ + key_repeat = enable; +} + +bool Keyboard::hasKeyRepeat() const +{ + return key_repeat; +} + bool Keyboard::isDown(Key *keylist) const { const Uint8 *keystate = SDL_GetKeyboardState(0); diff --git a/src/modules/keyboard/sdl/Keyboard.h b/src/modules/keyboard/sdl/Keyboard.h index 002fbb0d2..29a053e49 100644 --- a/src/modules/keyboard/sdl/Keyboard.h +++ b/src/modules/keyboard/sdl/Keyboard.h @@ -42,12 +42,19 @@ class Keyboard : public love::keyboard::Keyboard { public: + Keyboard(); + // Implements Module. const char *getName() const; + + void setKeyRepeat(bool enable); + bool hasKeyRepeat() const; bool isDown(Key *keylist) const; private: + bool key_repeat; + static std::map createKeyMap(); static std::map keys; diff --git a/src/modules/keyboard/wrap_Keyboard.cpp b/src/modules/keyboard/wrap_Keyboard.cpp index 4faf29e77..47a3493a9 100644 --- a/src/modules/keyboard/wrap_Keyboard.cpp +++ b/src/modules/keyboard/wrap_Keyboard.cpp @@ -29,7 +29,19 @@ namespace love namespace keyboard { -static Keyboard *instance; +static Keyboard *instance = 0; + +int w_setKeyRepeat(lua_State *L) +{ + instance->setKeyRepeat(luax_toboolean(L, 1)); + return 0; +} + +int w_hasKeyRepeat(lua_State *L) +{ + luax_pushboolean(L, instance->hasKeyRepeat()); + return 1; +} int w_isDown(lua_State *L) { @@ -53,6 +65,8 @@ int w_isDown(lua_State *L) // List of functions to wrap. static const luaL_Reg functions[] = { + { "setKeyRepeat", w_setKeyRepeat }, + { "hasKeyRepeat", w_hasKeyRepeat }, { "isDown", w_isDown }, { 0, 0 } }; diff --git a/src/modules/keyboard/wrap_Keyboard.h b/src/modules/keyboard/wrap_Keyboard.h index a052e93f1..7d370e00f 100644 --- a/src/modules/keyboard/wrap_Keyboard.h +++ b/src/modules/keyboard/wrap_Keyboard.h @@ -29,6 +29,8 @@ namespace love namespace keyboard { +int w_setKeyRepeat(lua_State *L); +int w_hasKeyRepeat(lua_State *L); int w_isDown(lua_State *L); extern "C" LOVE_EXPORT int luaopen_love_keyboard(lua_State *L);