Keypress not registering when other keys held down

My hero character can move in 8 directions and is controlled by the cursor keys, he shoots with the space bar.

This works fine apart from when up and left are being held down - then pressing the space bar doesn’t register. All the other combinations are ok.

I’m using Windows 10.

1 Like

Could you please share your on_input() function?

I don’t know enough about the details of how input detection is implemented in Defold to tell if this is a bug, but I’m able to detect any combination of 6 keys on my Macbook Pro. I’ve never seen any limitations in terms of what combinations that can be detected at the same time, but I’m sure there are differences in hardware from different manufacturers and different keyboard types.

Sure thing. I originally checked the space bar last but moved the check to the top to see if it made a difference but it didn’t.

function on_input(self, action_id, action)
    if action_id == hash("key_space") and action.pressed then
        self.shoot = 15
        msg.post("sounds#machinegun", "play_sound", {gain = 1.0})
    end

    if action_id == hash("key_up") then
        self.velocity.y = movespeed
        if action.pressed then
            msg.post("#sprite", "play_animation", {id = hash("move")})
        end
    elseif action_id == hash("key_down") then
        self.velocity.y = -movespeed
        if action.pressed then
            msg.post("#sprite", "play_animation", {id = hash("move")})
        end
    end        
    
    if action_id == hash("key_left") then
        self.velocity.x = -movespeed
        if action.pressed then
            msg.post("#sprite", "play_animation", {id = hash("move")})
        end
    elseif action_id == hash("key_right") then
        self.velocity.x = movespeed
        if action.pressed then
            msg.post("#sprite", "play_animation", {id = hash("move")})
        end
    end        
end

Ok, thanks. The code looks good. I only wanted to ensure that it wasn’t some syntax problem with an if-elseif statement or something like that. And key_space by itself works?

I can also recommend to name the action ids in your key bindings to the actual actions to be performed. For instance instead of “key_space” I’d call the action “shoot” and instead of “key_down” I’d call it “move_down” or “down”. This way you can have multiple inputs result in the same action.

Thanks for the tip on naming action ids.

key_space works perfectly other than when the up and left cursor keys are held down. I changed the directional keys to WASD and the problem disappeared so it seems something specific to those two keys.

OS X has the same bug.

function defold.on_input (self, action_id, action)
	if action_id == hash("move_left") then
	    if action.pressed then
	      msg.post(self.target, "move_left")
	    elseif action.released then
	      msg.post(self.target, "stop")
	    else
	      msg.post(self.target, "move_left")
	    end
		
	elseif action_id == hash("move_right") then
	    if action.pressed then
	      msg.post(self.target, "move_right")
	    elseif action.released then
	      msg.post(self.target, "stop")
	    else
	      msg.post(self.target, "move_right")
	    end
	end
end

Using this code, if we press and hold left arrow key (move left) and then press right arrow key, we can move right (good).
If we press and hold right arrow key (move right) and then press left arrow key, we can’t move left (bad).

HI @benjames171!

I’ve tested your code, but simplified it like this:

function on_input(self, action_id, action)
    if action_id == hash("key_space") and action.pressed then
        print("SPACE")
    end

    if action_id == hash("key_up") then
        if action.pressed then
            print("KEY_UP")
        end
    elseif action_id == hash("key_down") then
        if action.pressed then
            print("KEY_DOWN")
        end
    end        
    
    if action_id == hash("key_left") then
        if action.pressed then
            print("KEY_LEFT")
        end
    elseif action_id == hash("key_right") then
        if action.pressed then
            print("KEY_RIGHT")
        end
    end
end

And given this, I can hold down LEFT+UP and press SPACE as many times I want.
I’ve tried this on both OSX and Win10.

Could it be something in your update code that doesn’t handle your states correctly?

@dmitriy, maybe you could try to reorganize your input handling a little bit, to make sure to handle the input events for each frame. I think the issue here is that on each frame, we check the keyboard state, and compare it with the previous frame’s keyboard state, and send events accordingly. The order in which these events are sent within one frame should not be relied upon.

2 Likes

@Mathias_Westerdahl - thanks for giving it a try.

I think my update code is handling the inputs correctly - seeing as changing the bindings to WASD sorted the issue, it seems to be those two cursor keys in particular generating the problem.

I’m going to try the code on a different machine tomorrow to see if it’s some obscure issue with my particular laptop.

You right. My code is not correct, i see now, thanks.