Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I disable an onKeyPress function when chat is open?

Asked by 8 years ago

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

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

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)
0
Ah, I see. I guess I overlooked that. Thanks! IronSpider671 50 — 8y
Ad

Answer this question