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

How do i find out if a key/mouse button is pressed?

Asked by
HDWC 53
5 years ago

im attempting to make a game that includes keybinds. I've wrote a small module to simple the task. Module:

local Keys = {}

function Keys.new()
    local self = Keys
    return self
end

function Keys:GetKeyPressed(key)

end

return Keys

But i want to be able to bind both mouse buttons and keys. I dont now what to send as the key or how to detect whether or not its mouse or key. Keep in mind i have to store the key that is pressed when setting the keybinds.

Thank you in advance.

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

If you want to add keybinds, use this script: Also paste this script as LocalScript and put the LocalScript on StarterPlayer > StarterPlayerScripts > The local script goes here. You can rename the local script name to anything you want.

game:GetService("UserInputService").InputBegan:Connect(function(input,gameProcessed)
    if input.KeyCode == Enum.KeyCode.E then -- Checks if player pressed the key. Change the E letter to anything you want. Example: Enum.Keycode.C
        if input.UserInputType == Enum.UserInputType.Keyboard then -- Checks if the player pressed the key with keyboard
            if game:GetService("UserInputService"):GetFocusedTextBox() == nil then -- Checks if the player pressed a key when the players are not pressing the key on text box. If not, then...
                --Your script here
            end
        end
    end
end)

If you want get key you pressed, here it is!

local uis = game:GetService("UserInputService")
local keys = {}
uis.InputBegan:Connect(function(input,gameProcessed)
    local key = tostring(input.KeyCode)
    table.insert(keys,string.sub(key,14))
end)
0
by keybinds i mean they can be set by the player HDWC 53 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago
local Mouse = game.Players.LocalPlayer:GetMouse()--gets the mouse obviously, both mouse and keyboard

local plr = game.Players.LocalPlayer--gets the local player

Mouse.KeyDown:connect(function(key)--connects the function below

if key == tostring(" ") then--checks what u clicked

Mouse.Button1Down:connect(function()--when u click on ur left mouse button then the next function can run

if not game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.T) then return end--checks if u have pressed t
--if u have pressed both keys then whatever u want to happen will happen, delete one of these 2 functions to only use the other one, both is not necessary

use the local mouse from the top in this function too

0
im sorry but this isnt exactly what im looking for. Sadly the mouse tends to be faulty and userinputservice or contextactionservice is reccommended HDWC 53 — 5y
0
oh you dont want it in a function? Gameplayer365247v2 1055 — 5y

Answer this question