evasivedocs
Roblox scriptingLuau 0.748 · 24 guides · 385 API entries
Tutorials

Input and keybinds

This tutorial covers both directions: reading the keys and mouse buttons you press, and sending input to Roblox from a script.

Key codes

Keys are named two ways, and every function here takes either:

  • an Enum.KeyCode item, like Enum.KeyCode.F, Enum.KeyCode.LeftShift or Enum.KeyCode.MouseButton2;
  • a Windows virtual key code, like 0x46 for F or 0x02 for the right mouse button.

Microsoft's "Virtual-Key Codes" page lists every number.

Is a key down right now?

The simplest way is to ask:

Luau
local UserInputService = game:GetService("UserInputService")

while true do
    if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
        print("shift is held")
    end
    if ismouse2pressed() then
        print("right mouse is held")
    end
    task.wait(0.1)
end
Function Returns
UserInputService:IsKeyDown(key) true while the key is held
iskeypressed(key) the same, by key code
ismouse1pressed() true while the left button is held
ismouse2pressed() true while the right button is held
isrbxactive() true while Roblox is the window in front
UserInputService:GetMouseLocation() the cursor as a Vector2, in Roblox window pixels

These read the keyboard and mouse directly, so they also see keys pressed in other windows. Check isrbxactive() when that matters.

Reacting to a press

For "do something when I press a key", connect to InputBegan. It fires once per press, and InputEnded fires on release:

Luau
local UserInputService = game:GetService("UserInputService")

local enabled = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then
        return
    end
    if input.KeyCode == Enum.KeyCode.F then
        enabled = not enabled
        notify(enabled and "Feature on" or "Feature off", "My script", 2)
    end
end)

The input object has three fields:

Field What it is
KeyCode The key or button, as an Enum.KeyCode item. Mouse buttons are MouseButton1 to MouseButton5.
Position Where the cursor was, in Roblox window pixels
UserInputState "Begin" or "End"

gameProcessed is true when Roblox was not the window in front, for example while you are clicking around in the evasive menu or in another program. Skipping those presses keeps your binds from firing at the wrong time.

Keybinds the user can change

Hard-coding F is fine for a quick test. For a real script, let the user pick the key with a keybind widget in your own menu tab. Making your own menu tab covers it in full. In short:

Luau
local bind

UI.AddTab("Zoom", function(tab)
    local sec = tab:Section("Zoom", "Left")
    sec:Toggle("zoom_on", "Zoom", true)
    bind = sec:Keybind("zoom_key", Enum.KeyCode.C, "hold")
    sec:SliderInt("zoom_fov", "Zoomed FOV", 10, 70, 30)
end)

local normalFov = workspace.CurrentCamera.FieldOfView

while true do
    local camera = workspace.CurrentCamera
    if UI.GetValue("zoom_on") and bind and bind.value then
        camera.FieldOfView = UI.GetValue("zoom_fov")
    elseif camera.FieldOfView ~= normalFov then
        camera.FieldOfView = normalFov
    end
    task.wait()
end

The user can rebind C to anything, or switch the mode from hold to toggle, and the script does not change.

Sending input to Roblox

These functions press keys and move the mouse for you:

Function What it does
keypress(key) / keyrelease(key) Presses or releases a key
mouse1press() / mouse1release() / mouse1click() Left button down, up, or a full click
mouse2press() / mouse2release() / mouse2click() The same for the right button
mousemoverel(dx, dy) Moves the cursor by that many pixels
mousemoveabs(x, y) Moves the cursor to that point in the Roblox window
mousescroll(amount) Turns the wheel. The sign picks the direction.
setgameinput(on) Turns all of the above on or off for every script

mousemoveabs takes Roblox window pixels, so a WorldToScreen result can go straight in.

Input only goes out while Roblox is the window in front and the evasive menu is closed. The rest of the time these calls do nothing, so a script can never type into another program or into the menu. When a script stops, any key or button it still holds down is released for it.

This one clicks for you while you hold the side mouse button:

Luau
while true do
    if iskeypressed(0x05) and isrbxactive() then
        mouse1click()
        task.wait(0.1)
    else
        task.wait()
    end
end

0x05 is the first side button on the mouse.

The clipboard

setclipboard(text) copies text to the Windows clipboard:

Luau
setclipboard(tostring(game.PlaceId))
notify("Place id copied", "Clipboard", 2)

notify(message, title, seconds) shows a small card in the top right corner of the Roblox window.