Commits

Geraldo Ramos  committed 71d4ed1 Merge

Merged in leonidas/zoetrope09 (pull request #1)

Fixed the movement keys repeat handling

  • Participants
  • Parent commits 9decd31, fbc0665

Comments (0)

Files changed (2)

File zoetrope/input/keys.lua

 
 	_thisFrame = {},
 
+	-- private property: _alreadyHandled
+	-- was the key already processed by the user code since it was pressed?
+	-- Useful for handling repeated keys, check <pressed> and then you can see
+	-- if the key was repeated.
+
+	_alreadyHandled = {},
+
 	-- private property: _lastFrame
 	-- what keys were pressed last frame
 	
 		return unpack(result)
 	end,
 
+	-- Method: alreadyHandled
+	-- Returns whether this key was already handled since it was pressed.
+	-- Also sets the flag that something was handled, when calling.
+
+	alreadyHandled = function (self, key)
+		if not self._alreadyHandled[key] then
+			-- mark preemptively as handled
+			self._alreadyHandled[key] = true
+			-- return false, because we need to handle it
+			return false
+		end
+		return true
+	end,
+
 	-- Converts a character code to a Unicode string
 	-- see http://stackoverflow.com/questions/7780179/what-is-the-way-to-represent-a-unichar-in-lua/7799843
 
 
 	keyPressed = function (self, key, isrepeat)
 		self._thisFrame[key] = true
+
+		self._alreadyHandled[key] = false
 		--if unicode and unicode >= 0x20 and unicode ~= 127 and unicode < 0x3000 then
 		--	self.typed = self.typed .. self:unicodeChar(unicode)
 		--end

File zoetrope/ui/textinput.lua

 			end
 
 			-- handle movement keys that repeat
-			-- we have to simulate repeat rates manually :(
 
-			-- TODO: fix repeating keys
 			local frameAction
 
 			for _, key in pairs{'backspace', 'delete', 'left', 'right'} do
 				if the.keys:pressed(key) then
 					frameAction = key
+					if the.keys:alreadyHandled(key) then
+						frameAction = nil
+					end
 				end
 			end