Ad
Log in to vote
0

Checking for ALL keys instead of one?

Asked by
chrono2 189
7 years ago

In a localscript, I'm trying to code a weapon so that it'll activate a specific function when ANY letter key, even WASD, are pressed, and terminate the same function when the key is lifted. Is there any way I can do that W/O going through and assigning every individual key?

3 answers

Log in to vote
1
Answered by 7 years ago

Not tested, but should work:

local down

function AnyKeyPressed()
    while down do
        print("Any letter key was pressed!")
        wait(1)
    end
end

script.Parent.Selected:connect(function(mouse)
    mouse.KeyDown:connect(function(key)
        if key:match("%l") then
            -- we have a letter
            down = true
            AnyKeyPressed()
        end
    end)
    mouse.KeyUp:connect(function(key)
        if key:match("%l") then
            down = false
        end
    end)
end)

EDIT: Updated answer according to clarification from asker

0
So where would I put my function in that? :P chrono2 189 — 7y
0
Are you trying to execute a specific action for certain keys, or one action for any key? User#11893 186 — 7y
0
One action for any key. chrono2 189 — 7y
0
I've updated my answer accordingly User#11893 186 — 7y
0
Thanks. chrono2 189 — 7y
Ad
Log in to vote
0
Answered by
jobro13 980 Moderation Voter
7 years ago

Yes, you can. I'll use "Activate" as function which is called when such key is pressed.

The idea behind this function is that we do a pattern match on the key to check if it is in the "character" group: lower case only, as upper cases don't matter.

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
    if key and key:match("%l") then -- %l -> matches on lowercase letters. 
        Activate()
    end
end)

Please note that not all keys fire KeyDown: o and i for example are not passed, unless CameraType of CurrentCamera is set to scriptable.

0
So would I replace "Activate()" with my function, or would I place it below it? chrono2 189 — 7y
0
No, you can just define the Activate() function to the function which you wanted to call when a "letter key" has been pressed. (Who down voted my solution? Yes, it is the same solution as kenetec, but he ninja'd me...) jobro13 980 — 7y
Log in to vote
0
Answered by 5 years ago

OK THIS IS GREAT

0
1234567dgsdgsgs boom131 0 — 5y
0
ok2321421321 boom131 0 — 5y

Answer this question