Multiple keyboard inputs

I have a following problem:

When I hold right arrow key to move a hero right and, while still holding this key, I press left arrow key to go left, but the game doesn’t recognize the released action and do not stop moving right :confused: The same problem appears when a space key is pressed down.

How do I properly deal with multiple keyboard input? What am I missing or missunderstanding?

Below is a module function I’m using in go script’s on_input. The module uses Platypus and self.dir is used to specify if it should go left/right or not move at all.

 function MOVE:on_input(action_id, action)    	
 if action.pressed then
		if action_id == hJUMP then			-- click jump
			self:jump(true)
			d.log(d.MOVE, "clicked jump")
		elseif action_id == hLEFT then
			self.dir = u.left
			d.log(d.MOVE, "clicked left")
		elseif action_id == hRIGHT then
			self.dir = u.right
			d.log(d.MOVE, "clicked right")
		end
	end
	if action.released then
		if action_id == hJUMP then
			d.log(d.MOVE, "jump released")
			self:jump(false)
		elseif (action_id == hLEFT) or (action_id == hRIGHT) then
			d.log(d.MOVE, "direction keys released")
			self.dir = u.stop
		end
	end
end

I think the problem is with a logic and not the input handling, while releasing a key when other is pressed:

  1. right key is pressed => self.dir was changed once to u.right
  2. left key is pressed => self.dir was changed once to u.left, BUT then:
  3. right key is released => self.dir was changed once to u.stop and thus the go isn’t moving

Hi, I havent started using defold yet so could be wrong, but could you not solve this by simply ignoring press releases?

I needed it for stopping a go. Normally I would check each frame for an input status, but since it is possible to check only key state’s change it is convenient to stop a go at release. But as I described above, it’s a problem with handling those actions and not input itself, so the solution I made for now is to count left and right keys press and at every release decreasing this counter, and when it reaches 0 then to stop a go. For now it works, but I’ll see if it’s stable and then share it :wink:

1 Like

Still only watching some tutorial videos so glad you found a way around it :grinning:

2 Likes

You can get input from a key while it’s held down.

2 Likes