Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I detect if a key is let go on a keyboard (ContextActionService)?

I need to detect key presses, but I also need to detect when the key is let go. I want to use ContextActionService so I can easily detect input across platforms without changing my script, but I don't know how to detect when the key is let go.

Here's a gif of my airplane flaps moving: https://i.gyazo.com/e6802616144783ce4531c52c92aad295.gif

I need to know when the W or S key is let go so I can make the flaps FLAT when nothing is pressed.

Here's my current script for how I detect key presses:

local CAS = game:GetService("ContextActionService")

function lift()
	hingeConstraint.TargetAngle = 30
end
function dive()
	hingeConstraint.TargetAngle = -30
end
function flat()
	hingeConstraint.TargetAngle = 0
end

CAS:BindAction("Dive", dive, false, "w")
CAS:BindAction("Lift", lift, false, "s")

3 answers

✓ Accepted answer

Check the function's parameters

On the function these events are calling, it will give objects like the actionName, userInputState, and inputObject. Just check the userInputState.

-- The function, 
function lift(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.End then
		-- Do stuff
	end
end
That would be the same for the other function.

I got all this information from these two links.

UserInputState

Keyboard Input

Good Luck

if I helped, please don't forget to accept my answer.

3 comments

0

I'm still confused on how I would use this. I'll edit my question so it includes more of my script. I'll be gone for a few hours in case you don't hear back from me immediately.

0

You can figure it out.

0

OK I figured it out. I didn't know that by letting go of a key, BindAction triggers its function.

My final script was:

function FTRHinge(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
		if actionName == "Dive" then
			hingeConstraint.TargetAngle = -30
		elseif actionName == "Lift" then
			hingeConstraint.TargetAngle = 30
		end
	else
		hingeConstraint.TargetAngle = 0 --If you hold W then S then let go of S, W being pressed is ignored. Small bug but fix this
	end
end

CAS:BindAction("Dive", FTRHinge, false, "w")
CAS:BindAction("Lift", FTRHinge, false, "s")

Here's a GIF: https://i.gyazo.com/7a846d52e3a260678c075de3a790367e.gif

function FTRHinge(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        if actionName == "Dive" then
            hingeConstraint.TargetAngle = -30
        elseif actionName == "Lift" then
            hingeConstraint.TargetAngle = 30
        end
elseif state == Enum.UserInputState.End then
		print(" stoped")
	end
    end
end
 
CAS:BindAction("Dive", FTRHinge, true, "w")--turn it to true to make ur game mobile supported 
CAS:BindAction("Lift", FTRHinge, true, "s")

This discussion is preserved as it was. New answers, comments, and votes are closed.