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 disable an onKeyPress function when chat is open?

Hello ScriptingBuilders! Indeed, I require your assistance. :D So, I have a script similar to this (taken from here:

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		print("R was pressed")
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

And the script works all fine whenever I press a key (or in this case "R"), but it also works when typing in the chat and when pressing a key does something more visual (such as cloning a ScreenGUI), it can get pretty distracting and at a point, frustrating. Now, I haven't found anything about this on the forums, so I was wondering if anyone could help me out here. The help is always appreciated. Thanks. :D -IronSpider671

1 answer

✓ Accepted answer

The second value returned from the InputBegan event tells you whether or not the event fired as a gameProcessedEvent - e.g. while typing in the chat. It seems you supplied a parameter for this in line 1 but you never actually checked it. CHECK IT.

function onKeyPress(inputObject, gameProcessedEvent)
	--Check the second parameter before continuing
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.R then
			print("R was pressed")
		end
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 comment

0

Ah, I see. I guess I overlooked that. Thanks!

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