Key Combinations (SOLVED)

I’ve been searching around for this but didn’t quite find a fitting solution.
I want the player to be able to hold shift and click at the sane time causing something else to happen than when the player simply clicks. I know that the action_id variable cannot hold multiple hashes at the same time meaning that the on_input function in itself cannot check for key combinations.
Is there a simple and flexible workaround for this?
Thanks in advance

Hi , you can check if key1 was pressed and set a varible to true, then check if key2 and the previous variable is true, to do stuff for this combination.

remember to set variable to false when the key 1 or 2 is released

if action_id == hash(“up”) and action.released then

Thanks for the reply, but I have already been trying this multiple times. The problem is that every time the on_input function is called, only one action is registered, meaning that it is simply impossible to check for two actions at the same time in one script.
For example the boolean “up” will be set to false when I let go of the key, but also when I start pressing a different one, even if I did not let go of the key. There will always only be one boolean which is set true.

if action_id == hash(“up”) and action.released then
print(“releasing”)
end

Im presing up and other key, and the up input is not releasing, then it has to work

you dont need to check 2 actions. you chek 1 action in 1 frame (key 1) and 1 action in second frame (key2)

1 Like

The on_input function will be called multiple times each frame if you have multiple inputs.

You need to store the pressed state of the modifier keys somewhere so that you can check them when you get the click input. If all you need is shift, then it’s easiest to just set a “self.isShiftPressed” variable to true or false when you get input for the shift key. Like so:

function on_input(self, action_id, action)
	if action_id == hash("shift") then
		if action.pressed then
			self.isShiftPressed = true
		elseif action.released then
			self.isShiftPressed = false
		end
	elseif action_id == hash("click") then
		if self.isShiftPressed then
			-- Do one thing.
		else
			-- Do an different thing.
		end
	end
end

If you want a more systematic way to use multiple modifier keys in multiple scripts, I use a very simple Lua module to store key states:

-- Stupid-simple module to remember whether actions are pressed or not.
local M = {}

local state = {}

function M.on_input(action_id, action)
	if action.pressed then
		state[action_id] = true
	elseif action.released then
		state[action_id] = false
	end
end

function M.is_pressed(action_id)
	if state[action_id] then  return true
	else  return false  end
end

return M

Have a script in your top-level collection that requires this module and calls it’s on_input function for all inputs. Then any other script that needs to check the state of a key can require the module and use M.is_pressed.

This is also useful to fix the issue of player inputs getting “stuck” if you happen to pause/unpause your game while the player is holding down keys.

6 Likes

Isn’t this equivalent to:

function M.is_pressed(action_id)
	return state[action_id]
end
2 Likes

Yeah pretty much. I can’t remember why, but for some reason, at the time I wrote that, I wanted it to always be a boolean.

1 Like

Thanks to all of you!
It was really just a human error, I ended up using ross’ code and it all worked out. Seems like I only messed up the action.released and action.pressed variables.

function M.is_pressed(action_id)
	return state[action_id]
end

It will return nil if the action_id have never been processed by on_input before.
Could be also:

function M.is_pressed(action_id)
	return state[action_id] or false
end

I’ll be using that stupid-simple module everywhere hereinafter.

3 Likes