Skip to content

Keybinds and input

input.bind(action, fired) adds a key setting, listed under your plugin in Wraith’s Settings, and calls fired on the render thread when it’s pressed or released. It gives back a Keybind, and it goes when your plugin does.

The Input.Action says what it is:

  • id is what the player’s choice is saved under, so keep it stable.
  • name is what Settings lists it as.
  • chord is its default key, a key and the mods held with it. Players can pick others in Settings, and theirs win.
  • also has more keys that do the same by default, like a controller button. Up to 3.
  • passthrough lets the game see the key too, and while_typing fires it while typing in a text box.
  • scope set to game starts the player’s pick just for this game. They can switch it in Settings.
  • held is for keys that are held, see below.

fired gets a Keybind.Phase, pressed or released.

mods is a number: add 1 for Ctrl, 2 for Shift, 4 for Alt and 8 for the Windows key.

import { Wraith } from "wraith";
const { Phase } = Wraith.Keybind;
const heal = Wraith.input().bind({ id: "heal", name: "Heal", chord: { key: "h", mods: 1 } }, (phase) => {
if (phase === Phase.pressed) { /* ... */ }
});

A keybind’s chords() says what it’s on now, the player’s picks if they made some. rebind(chords) moves it to other keys, saved like picking them in Settings, and unbind() takes it away, keeping its keys saved for next time.

next() waits for the keybind’s next press, and carries on on the thread that asked. Once it’s unbound it fails with Unavailable. If you only ever wait, fired can be null.

import { Wraith } from "wraith";
const go = Wraith.input().bind({ id: "go", name: "Go", chord: { key: "f9" } }, () => {});
async function waitForGo() {
while (true) {
await go.next();
Wraith.log().info("go!");
}
}
waitForGo();

A controller button works as a key too, without modifiers. The buttons are named after an Xbox pad: gamepad_a, gamepad_b, gamepad_x, gamepad_y, gamepad_left_shoulder, gamepad_right_shoulder, gamepad_left_trigger, gamepad_right_trigger, gamepad_dpad_up and so on, gamepad_start, gamepad_back, gamepad_left_stick and gamepad_right_stick.

Write them like "gamepad_back" or "gamepad_y".

For keys that are held, like ones to move with, set held. They fire pressed and released whatever modifiers are down too, so a Shift to go faster doesn’t stop W working, and the key can be a modifier itself. Players pick them the same way in Settings.

A keybind can have up to four keys, any of which does it, like a key and a controller button. also gives the defaults after the first, and players add and take off keys in Settings with + and -.

import { Wraith } from "wraith";
let fast = false;
const faster = Wraith.input().bind({ id: "faster", name: "Faster", chord: { key: "left_shift" }, also: [{ key: "gamepad_right_shoulder" }], held: true, passthrough: true }, (phase) => {
fast = phase === Wraith.Keybind.Phase.pressed;
});

input.gamepad() gives the sticks and triggers of every controller that’s connected, merged, or null without one. Sticks go -1 to 1 with up and right positive, triggers 0 to 1, with no dead zone.

input.keyDown(key) and input.buttonDown(button) say what’s held now, from any thread. Keys are named like in a chord, and sided keys like left_shift always have their side, so check both. Mouse buttons are left, right, middle, x1 and x2. input.mousePos() is where the mouse is in the game window, or null if it doesn’t know yet.

import { Wraith } from "wraith";
const input = Wraith.input();
const shift = input.keyDown("left_shift") || input.keyDown("right_shift");
const at = input.mousePos();
if (shift && input.buttonDown("right") && at) aim(at.x, at.y);

Keys and the mouse go by the game window’s messages, so nothing’s down while another window has focus. Controllers are read on their own and don’t need focus.

ui.takeInput(device, take) hides the keyboard, the mouse or controllers from the game, until you give it back or the plugin stops. A taken keyboard or controller also moves around the overlay’s windows. ui.showCursor only draws the cursor, so take the mouse too if clicks shouldn’t reach the game. ui.inputTaken(device) says whether anyone has it, like the user being in the overlay.

import { Wraith } from "wraith";
const ui = Wraith.ui();
ui.takeInput("mouse", true);
ui.showCursor(true);
// ...
ui.takeInput("mouse", false);
ui.showCursor(false);

Players can stop controllers being taken at all in Wraith’s settings.

Some games keep their cursor on screen and don’t turn the camera with the mouse unless a button’s held, like FFXIV. In those, Wraith’s windows don’t need the mouse and keyboard to themselves. A game’s plugin says so with ui.setMouseFree(true). Opening Wraith’s bar then leaves the game playable, and Wraith only takes clicks over its own windows and keys typed into them. It lasts till the plugin stops, and players can say otherwise per game, in Settings under Controls.

For turning something with the mouse, like a camera, ui.holdMouse(true) keeps the cursor hidden in the middle of the game window, so it never runs into the edge of the screen. Each frame, ui.mouseMotion() says how far it was pushed since the last one, in pixels. Letting go puts the cursor back where it was. Take the mouse too, or the game sees it move.

import { Wraith } from "wraith";
const looking = Wraith.input().buttonDown("right");
Wraith.ui().holdMouse(looking);
if (looking) yaw += Wraith.ui().mouseMotion().x * 0.002;

Made with ♥ by Asgard