Browse Source

Added love.keyboard.setKeyRepeat and love.keyboard.hasKeyRepeat (resolves issue #717)

Alex Szpakowski 12 years ago
parent
commit
eb052d99e8

+ 9 - 0
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;

+ 12 - 0
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.

+ 15 - 0
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);

+ 7 - 0
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<Key, SDL_Keycode> createKeyMap();
 	static std::map<Key, SDL_Keycode> keys;
 

+ 15 - 1
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 }
 };

+ 2 - 0
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);