Key Handler for client Framework help

What’s a good way to set up a key handler for a client framework? What I’m thinking is something like this, but I’m stuck and cant find any articles on it.

image

Let me know if you have any better ideas or something. I don’t know trying to come up with something good to make making things easier for pressing keys and stuff.

u can save them like

Enum.KeyCode.LeftShift.Name

Here is a resource I found cool by looking at this vehicle input module.

One aspect I like a lot about it is that it’s possible to set multiple key binds to one action so it’s possible to have mobile or controller compatibility, by converting a table of Enums into a dictionary of Enums.

Code excerpt:
local controls = {}
controls.GearDown = {
	Enum.KeyCode.F,Enum.KeyCode.Q, Enum.KeyCode.ButtonX
}
for ci,control in pairs(controls) do -- converting enums to have readable indexes.
	local new = {}
	for i,keycode in pairs(control) do
		local index = keycode.Value
		new[index] = index
	end
	controls[ci] = new
end

Then you can compare the input object during input began EnumCode with the dictionary to see if it’s a valid input then do something with it.

Edit: However the issue I found is that the vehicle input module is quite too realistic for my usage with a button for handbrake and changing gears which I didn’t need and just wanted something similar to a vehicle seat so here is the simplified version:

Vehicle input module to mimic vehicle seat throttle and steer
local m = {}		-- functions
local inputs = {}	-- input states
local controls = {}	-- keybinds

inputs.Throttle = 0; 				-- User input determining the power sent to the wheels
inputs.Steer = 0;					-- User input determining the angle at which the wheels should be turned.

controls.Throttle = {
	Enum.KeyCode.W,Enum.KeyCode.Up,Enum.KeyCode.ButtonR2,Enum.UserInputType.Touch
}
--brake = -1 for throttle
controls.Brake = {
	Enum.KeyCode.S, Enum.KeyCode.Down, Enum.KeyCode.ButtonL2,Enum.UserInputType.Touch
}
controls.SteerLeft = {
	Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.Thumbstick1,Enum.UserInputType.Touch
}
controls.SteerRight = {
	Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.Thumbstick1,Enum.UserInputType.Touch
}
--[[
controls.Handbrake = {
	Enum.KeyCode.Space, Enum.KeyCode.ButtonA
}
]]

for ci,control in pairs(controls) do -- converting enums to have readable indexes.
	local new = {}
	for i,keycode in pairs(control) do
		local index = keycode.Value
		new[index] = index
	end
	controls[ci] = new
end


function m.GetControls() -- return the list of user input controls
	return controls
end
function m.GetInputs() -- return the state of user inputs
	return inputs
end

function m.GetInput(io,gpe)	-- inputObject, gameProcessedEvent

	local state = io.UserInputState.Name -- setting enums to strings with '.Name' is much easier
	local typ = io.UserInputType.Name
	local pos = io.Position    --pos shows the current position of an input (e,g, mouse.X/Y)
	local delta = io.Delta      -- delta shows how much has CHANGED from last input
	local key = io.KeyCode.Name    
	local code = io.KeyCode.Value   -- gets the KeyCode number value
	local gamepad = typ:match("Gamepad")  -- is it a gamepad?

	pos = Vector3.new(
		pos.X * math.abs(pos.X),   -- account for deadzone from thumbstick input.
		pos.Y * math.abs(pos.Y),
		pos.Z * math.abs(pos.Z)
	)
		--pos.Magnitude <= 0.125 and Vector3.new() or pos -- fixed deadzone variant


	if gpe and not key == "Thumbstick1" then return end -- thumbstick is a GPE, who wouldn't known?...
	--print(state,typ,delta,pos,code,touch)

--	print("State",state)
--	print("IOType",typ)
--	print("POS:",pos)
--	print("DELTA:",delta)
--	print("KEY:",key)
--	print(controls.Throttle[key])
--	print("Gamepad:",gamepad)

	if controls.Throttle[code] then -- Throttle
		if key:match("Button") then
			inputs.Throttle = pos.Z
		else
			inputs.Throttle = state == "Begin" and 1 or 0
		end
--		print("Throttle",inputs.Throttle)
	end

	if controls.Brake[code] then -- Brake
		if key:match("Button") then
			inputs.Brake = pos.Z
		else
			inputs.Throttle = state == "Begin" and -1 or 0
		end
--		print("Brake:",inputs.Brake)
	end

	if controls.SteerLeft[code] or controls.SteerRight[code] then
		if key:match("Thumbstick1") then
			inputs.Steer = pos.X * (state == "End" and 0 or 1)
		else
			inputs.Steer = 1 * (controls.SteerLeft[code] and -1 or 1) * (state == "End" and 0 or 1)
		end
--		print("Steer",inputs.Steer,pos.X)
	end

end

return m
3 Likes

Hello there,
Maybe, This is a simple method.

--- Services
local UIS = game:GetService("UserInputService")

--- Functions
function Dashing(x)
	print(x)
	if x == "Base" then --- Use the "Base" delivered below as a function.
		--- do your stuff
	end
	--- do your stuff more!
end

function PunchThingsExample(x) --- this is example
	print(x)
	if x == "Kick" then
		--- PLAY KICK ANIMATION
	end
	if x == "Super Kick" then
		--- PLAY SUPER KICK ANIMATION
	end
end

--- Main
local Keys = {
	Dashing = { --- To make it cool and convenient, Choose a simple name.
		Function = Dashing; --- Write here function's name
		Binds = {
			{"Base",Enum.KeyCode.Q}; --- If you press Q, we will deliver "Base" to the function above.
			{"Right",Enum.KeyCode.D};
			{"Left",Enum.KeyCode.A};
			{"Back",Enum.KeyCode.S};
		}
	};
	Punch = {
		Function = PunchThingsExample;
		Binds = {
			{"Kick",Enum.KeyCode.X};
			{"Super Kick",Enum.KeyCode.Z};
		}
	};
}

UIS.InputBegan:Connect(function(k)
	local KeyCode = k.KeyCode
	for a,Key in pairs(Keys) do
		local Func = Key.Function
		for c,Bind in pairs(Key.Binds) do
			if KeyCode == Bind[2] then
				if Func then
					Func(Bind[1])
				end
			end
		end
	end
end)

Or you can create a module script to create a better framework!
https://education.roblox.com/en-us/resources/intro-to-module-scripts
I hope it was helpful. Thanks.
(I tested it)

Edit: I’ve added a little more comments.

How are the other binds useable? This is only specific to 1 and 2

You can edit the local Keys table (copy and paste, check this example) to add more and edit more.

function Gun(x)
    if x == "Shot" then
       print("Pew!")
    end
    if x == "Reload" then
       print("Reloading the gun!")
    end
end
local Keys = {
    Gun = {
	   Function = Gun;
	   Binds = {
		    {"Shot",Enum.KeyCode.N};
		    {"Reload",Enum.KeyCode.R};
	    }
    };
}

Edit: Fixed

elseif exists. I’m making a module to go with my key framework, I’m gonna see if this works. (got a little inspiration from your key setup)

1 Like