evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Tutorials

Input and keys

In this tutorial you build a keystrokes display: W, A, S, D, the jump key and both mouse buttons light up while you hold them. It is the kind of thing you see in movement videos.

Reading the keyboard and the mouse needs no permission. Pressing keys for the user is a different thing and needs the action permission, see Aim, attack and keys.

How input is read

  • input.is_down(key) is true while a key or mouse button is held.
  • input.pressed(key) is true on the frame the key went down, input.released(key) on the frame it came up.
  • Keys are named in the vk table: vk.w, vk.space, vk.f5, vk.shift, vk.lbutton for the left mouse button and so on.
  • Input is sampled once per drawn frame, so read it in paint. A tick sees the last sample.
  • While a text field in the menu has focus, every key reads as up. Typing a name in the menu never sets off your script.

1. Four keys

Lua
local layout = {
    {key = vk.w, text = "W", col = 1, row = 0},
    {key = vk.a, text = "A", col = 0, row = 1},
    {key = vk.s, text = "S", col = 1, row = 1},
    {key = vk.d, text = "D", col = 2, row = 1},
}

local SIZE, GAP = 34, 4

local function key_box(x, y, w, h, text, down)
    draw.rect_filled(x, y, w, h, down and ui.accent() or color(17, 17, 17, 200), 4)
    draw.text(x + w / 2, y + h / 2 - 7, text, color(255, 255, 255), {align = "center", font = "bold"})
end

events.on("paint", function()
    local x0, y0 = 40, 300
    for _, k in ipairs(layout) do
        local x = x0 + k.col * (SIZE + GAP)
        local y = y0 + k.row * (SIZE + GAP)
        key_box(x, y, SIZE, SIZE, k.text, input.is_down(k.key))
    end
end)

2. The user's own jump key

Not everyone jumps with space. With Allow game scripting on, game.binding(command) tells you which keys CS2 has bound to a command, and game.action_down(command) tells you whether one of them is held:

Lua
events.on("paint", function()
    if not game then return end
    local b = game.binding("+jump")
    local name = b and b.vk and input.key_name(b.vk) or "jump"
    local down = game.action_down("+jump") or false
    draw.text(40, 260, name .. (down and " (held)" or ""), color(255, 255, 255))
end)

game.binding gives a table with keys (CS2's own names), vks (codes for input.is_down) and vk (the main key, or nil). It reads CS2's binds every 2 seconds and gives nil when you are not in a match. input.key_name(code) turns a key code into a readable name.

3. A key to hide it

You could check input.pressed(vk.f6) yourself, but a keybind control is better: the user picks the key, and it is saved with their config.

Lua
local visible_key = ui.keybind("Show keystrokes", vk.f6, "toggle")

events.on("paint", function()
    if not visible_key:active() then return end
    draw.text(40, 240, "keystrokes on", color(255, 255, 255))
end)

The finished script

Lua
-- @name Keystrokes
-- @description WASD, jump and mouse buttons light up while held.

local visible_key = ui.keybind("Show keystrokes", vk.f6, "always")
local pos_x = ui.slider("X", 0, 100, 3, {suffix = "%"})
local pos_y = ui.slider("Y", 0, 100, 40, {suffix = "%"})

local SIZE, GAP = 34, 4
local layout = {
    {key = vk.w, text = "W", col = 1, row = 0},
    {key = vk.a, text = "A", col = 0, row = 1},
    {key = vk.s, text = "S", col = 1, row = 1},
    {key = vk.d, text = "D", col = 2, row = 1},
}

local function key_box(x, y, w, h, text, down)
    draw.rect_filled(x, y, w, h, down and ui.accent() or color(17, 17, 17, 200), 4)
    draw.text(x + w / 2, y + h / 2 - 7, text, color(255, 255, 255), {align = "center", font = "bold"})
end

local function jump_down()
    if game then
        local down = game.action_down("+jump")
        if down ~= nil then return down end
    end
    return input.is_down(vk.space)
end

events.on("paint", function()
    if not visible_key:active() then return end
    local sw, sh = draw.screen_size()
    local width = 3 * SIZE + 2 * GAP
    local x0 = math.floor((sw - width) * pos_x:get() / 100)
    local y0 = math.floor((sh - 4 * SIZE) * pos_y:get() / 100)

    for _, k in ipairs(layout) do
        key_box(x0 + k.col * (SIZE + GAP), y0 + k.row * (SIZE + GAP), SIZE, SIZE, k.text, input.is_down(k.key))
    end

    local row3 = y0 + 2 * (SIZE + GAP)
    key_box(x0, row3, width, SIZE - 10, "JUMP", jump_down())

    local row4 = row3 + SIZE - 10 + GAP
    local half = (width - GAP) / 2
    key_box(x0, row4, half, SIZE - 10, "LMB", input.is_down(vk.lbutton))
    key_box(x0 + half + GAP, row4, half, SIZE - 10, "RMB", input.is_down(vk.rbutton))
end)

The keybind starts in "always" mode, so the display is on until the user changes the mode in Scripts > Settings. Like every keybind it reads false while the menu is open, so the display hides while you are in the menu. game.action_down gives nil outside a match, and then the script falls back to the space bar.

More input

Function Gives
input.mouse() The cursor position inside the game window.
input.mouse_delta() How far the cursor moved since the last frame. In game CS2 holds the cursor still, so use the change of entities.local_player().view_angles to follow aim.
input.wheel() Wheel notches this frame. vk.wheel_up and vk.wheel_down also work with input.pressed.
input.keys_down() Every key held right now, as a list of codes.
input.menu_open() True while the evasive menu is open.
input.key_name(code) A readable name for a key code.

A quick way to find a key's code is to print what you hold:

Lua
events.on("paint", function()
    for _, k in ipairs(input.keys_down()) do
        if input.pressed(k) then print(k, input.key_name(k)) end
    end
end)

Key names

vk has a name for every key: a to z, n0 to n9 for the number row, num0 to num9 for the keypad, f1 to f24, shift, ctrl, alt and their left and right versions like lshift, the arrows left, up, right, down, and insert, delete, home, pageup, pagedown, space, enter, escape, tab and back. The End key is vk.end_, because end is a Lua keyword. Mouse buttons are lbutton, rbutton, mbutton, xbutton1 and xbutton2, also called mouse1 to mouse5.

Next

Files, storage and HTTP keeps data between sessions and talks to the web.