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

How do I implement IsKeyDown to get a true false value?

Asked by 8 years ago

I haven't dabbled far from using events on functions I already know, so all I really need is an example of IsKeyDown. Any example.

Thanks.

1 answer

Log in to vote
1
Answered by 8 years ago

You can use the UserInputService with the method GetKeysPressed which returns a list of current InputObject.

This an example to show how it can be used, is also must be a local script:-

local inpServ = game:GetService('UserInputService') -- gets the service

function isKeyDown(key)
    for i,v in pairs(inpServ:GetKeysPressed()) do
        if v.KeyCode  == key then return true end
    end

    return false -- no keys found
end

function isMultiKeyDown(keys)
    local num = 0 -- keydown counter

    for i,v in pairs(inpServ:GetKeysPressed()) do
        for i2, v2 in pairs(keys) do -- probably should check for multiples of the same key
            if v.KeyCode == v2 then num = num + 1 end
        end
    end

    return num == #keys -- just return true of false
end

while true do
    print("multiple", isMultiKeyDown({Enum.KeyCode.A, Enum.KeyCode.S}))
    print("single", isKeyDown(Enum.KeyCode.A))
    wait(3)
end

Hope this helps

Ad

Answer this question