Keybinds and input
Keybinds
Section titled “Keybinds”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:
idis what the player’s choice is saved under, so keep it stable.nameis what Settings lists it as.chordis its default key, akeyand themodsheld with it. Players can pick others in Settings, and theirs win.alsohas more keys that do the same by default, like a controller button. Up to 3.passthroughlets the game see the key too, andwhile_typingfires it while typing in a text box.scopeset togamestarts the player’s pick just for this game. They can switch it in Settings.heldis 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) { /* ... */ }});mods is a number: add 1 for Ctrl, 2 for Shift, 4 for Alt and 8 for the Windows key.
local Wraith = require("wraith").Wraith
local Phase = Wraith.Keybind.Phase
local heal = Wraith.input():bind({ id = "heal", name = "Heal", chord = { key = "h", mods = 1 } }, function(phase) if phase == Phase.pressed then --[[ ... ]] endend)Keybind? heal;
// in Startvar chord = new Chord { Key = Key.H, Mods = new Mods { Ctrl = true } };heal = Input.Instance.Bind(new Input.Action { Id = "heal", Name = "Heal", Chord = chord }, phase =>{ if (phase == Keybind.Phase.Pressed) { /* ... */ }});Keep the keybind in a field.
mods is a Wraith::Mods, set with with_ctrl, with_shift, with_alt and with_super.
// in startauto heal = wraith::api().input().bind({.id = sal::String::of("heal"), .name = sal::String::of("Heal"), .chord = {Wraith::Key::h, Wraith::Mods{}.with_ctrl(true)}}, sal::keep<Wraith::Keybind::Fired>([](Wraith::Keybind::Phase phase) { if (phase == Wraith::Keybind::Phase::pressed) { /* ... */ } }));mods is a Mods, set with with_ctrl, with_shift, with_alt and with_super.
use salamander::Str;use wraith::{Chord, InputAction, Key, KeybindFired, KeybindPhase, Mods};
let action = InputAction { id: Str::new("heal"), name: Str::new("Heal"), chord: Chord { key: Key::H, mods: Mods::default().with_ctrl(true) }, ..Default::default()};let heal = wraith::api() .input() .bind(action, KeybindFired::keep(|phase: KeybindPhase| { if phase == KeybindPhase::PRESSED { /* ... */ } })) .ok()?;const W = sdk.api.wraith;
fn onHeal(self: *Plugin, phase: W.Keybind.Phase) void { if (phase == .pressed) self.heal_now = true;}
// in startself.heal = try ctx.wraith.input().bind(.{ .id = "heal", .name = "Heal", .chord = .{ .key = .h, .mods = .{ .ctrl = true } } }, .init(self, onHeal));mods is a Wraith_Mods, set with Wraith_Mods_with_ctrl, _with_shift, _with_alt and _with_super.
static void on_heal(uintptr_t self, Wraith_Keybind_Phase phase) { (void)self; if (phase == Wraith_Keybind_Phase_pressed) { /* ... */ }}
static const Wraith_Keybind_Fired_table on_heal_table = { SAL_TABLE(Wraith_Keybind_Fired), .call = on_heal };
/* in start */Wraith_Input_Action action = { .id = SAL_STR("heal"), .name = SAL_STR("Heal"), .chord = { Wraith_Key_h, Wraith_Mods_with_ctrl(0, true) } };Wraith_Keybind heal;Wraith_Input_bind(Wraith_Api_input(w), action, (Wraith_Keybind_Fired){ 0, &on_heal_table }, &heal);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.
Waiting for a press
Section titled “Waiting for a press”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();local Wraith = require("wraith").Wraith
local go = Wraith.input():bind({ id = "go", name = "Go", chord = { key = "f9" } }, function() end)
task.spawn(function() while true do go:next():await() Wraith.log():info("go!") endend)var go = Input.Instance.Bind(new Input.Action { Id = "go", Name = "Go", Chord = new Chord { Key = Key.F9 } }, null)!;
while (true){ await go.NextAsync(); Log.Instance.Info("go!");}static Wraith::Keybind go;
static sal::Future<void> wait_for_go() { for (;;) { if (!co_await go.next()) co_return {}; wraith::api().log().info("go!"); }}
// in startif (auto bound = wraith::api().input().bind({.id = sal::String::of("go"), .name = sal::String::of("Go"), .chord = {Wraith::Key::f9}}, {})) { go = *bound; wait_for_go();}use salamander::Str;use wraith::{Chord, InputAction, Key, Keybind, KeybindError, KeybindFired};
async fn wait_for_go(go: Keybind) -> Result<(), KeybindError> { loop { go.next().await?; wraith::api().log().info("go!"); }}
fn bind_go() -> Option<()> { let action = InputAction { id: Str::new("go"), name: Str::new("Go"), chord: Chord { key: Key::F9, ..Default::default() }, ..Default::default() }; let go = wraith::api().input().bind(action, KeybindFired::default()).ok()?; let _ = salamander::spawn(wait_for_go(go)); Some(())}In a task, sdk.tasks.wait waits on it.
fn waitForGo(self: *Plugin) !void { while (true) { try sdk.tasks.wait(self.go.next()); log.info("go!", .{}); }}sdk.keybinds.Presses is a Zig helper on top, which counts presses so ones that come while you’re busy aren’t lost. try presses.next(io) waits for one, and presses.take() says whether there’s been one, without waiting.
Wraith_Keybind_next gives a sal_Promise, settled at the next press, like in Waiting.
static Wraith_Keybind go;static sal_Promise press;
static void on_go(uintptr_t self);static const sal_Task_table on_go_table = { SAL_TABLE(sal_Task), .call = on_go };
static void wait_for_go(void) { press = Wraith_Keybind_next(go); sal_Promise_then(press, (sal_Task){ 0, &on_go_table }, (sal_Queue){ 0, NULL });}
static void on_go(uintptr_t self) { (void)self; bool ok = sal_Promise_take(press, NULL) == SAL_OK; sal_Promise_release(press); if (!ok) return; Wraith_Log_info(Wraith_Api_log(w), SAL_STR("go!")); wait_for_go();}
/* in start */Wraith_Input_Action action = { .id = SAL_STR("go"), .name = SAL_STR("Go"), .chord = { Wraith_Key_f9 } };if (Wraith_Input_bind(Wraith_Api_input(w), action, (Wraith_Keybind_Fired){ 0 }, &go) == SAL_OK) wait_for_go();Controller buttons
Section titled “Controller buttons”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".
Write them like "gamepad_back" or "gamepad_y".
Write them like Key.GamepadBack or Key.GamepadY.
They’re Wraith::Keys, like Wraith::Key::gamepad_back or Wraith::Key::gamepad_y.
They’re Keys, like Key::GAMEPAD_BACK or Key::GAMEPAD_Y.
Write them like .gamepad_back or .gamepad_y.
They’re Wraith_Keys, like Wraith_Key_gamepad_back or Wraith_Key_gamepad_y.
Held keys and controllers
Section titled “Held keys and controllers”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;});local Wraith = require("wraith").Wraith
local fast = falselocal faster = Wraith.input():bind({ id = "faster", name = "Faster", chord = { key = "left_shift" }, also = { { key = "gamepad_right_shoulder" } }, held = true, passthrough = true }, function(phase) fast = phase == Wraith.Keybind.Phase.pressedend)var fast = false;var faster = Input.Instance.Bind(new Input.Action{ Id = "faster", Name = "Faster", Chord = new Chord { Key = Key.LeftShift }, Also = [new Chord { Key = Key.GamepadRightShoulder }], Held = true, Passthrough = true,}, phase => fast = phase == Keybind.Phase.Pressed)!;#include <array>#include <atomic>
static std::atomic<bool> fast;static const std::array<Wraith::Chord, 1> also = {{{Wraith::Key::gamepad_right_shoulder}}};
// in startauto faster = wraith::api().input().bind({.id = sal::String::of("faster"), .name = sal::String::of("Faster"), .chord = {Wraith::Key::left_shift}, .also = sal::Slice<Wraith::Chord>::of(also), .passthrough = true, .held = true}, sal::keep<Wraith::Keybind::Fired>([](Wraith::Keybind::Phase phase) { fast = phase == Wraith::Keybind::Phase::pressed; }));use std::sync::atomic::{AtomicBool, Ordering};
use salamander::{Slice, Str};use wraith::{Chord, InputAction, Key, KeybindFired, KeybindPhase};
static FAST: AtomicBool = AtomicBool::new(false);static ALSO: [Chord; 1] = [Chord { key: Key::GAMEPAD_RIGHT_SHOULDER, mods: wraith::Mods(0) }];
fn bind_faster() -> Option<()> { let action = InputAction { id: Str::new("faster"), name: Str::new("Faster"), chord: Chord { key: Key::LEFT_SHIFT, ..Default::default() }, also: Slice::of(&ALSO), passthrough: true, held: true, ..Default::default() }; let faster = wraith::api() .input() .bind(action, KeybindFired::keep(|phase: KeybindPhase| FAST.store(phase == KeybindPhase::PRESSED, Ordering::Relaxed))) .ok()?; Some(())}fn onFaster(self: *Plugin, phase: W.Keybind.Phase) void { self.fast = phase == .pressed;}
// in startself.faster = try ctx.wraith.input().bind(.{ .id = "faster", .name = "Faster", .chord = .{ .key = .left_shift }, .also = &.{.{ .key = .gamepad_right_shoulder }}, .held = true, .passthrough = true }, .init(self, onFaster));static volatile bool fast;static const Wraith_Chord also[] = { { Wraith_Key_gamepad_right_shoulder } };
static void on_faster(uintptr_t self, Wraith_Keybind_Phase phase) { (void)self; fast = phase == Wraith_Keybind_Phase_pressed;}
static const Wraith_Keybind_Fired_table on_faster_table = { SAL_TABLE(Wraith_Keybind_Fired), .call = on_faster };
/* in start */Wraith_Input_Action action = { .id = SAL_STR("faster"), .name = SAL_STR("Faster"), .chord = { Wraith_Key_left_shift }, .also = { also, 1 }, .passthrough = true, .held = true,};Wraith_Keybind faster;Wraith_Input_bind(Wraith_Api_input(w), action, (Wraith_Keybind_Fired){ 0, &on_faster_table }, &faster);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.
Keys and the mouse
Section titled “Keys and the mouse”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);local Wraith = require("wraith").Wraith
local input = Wraith.input()
local shift = input:keyDown("left_shift") or input:keyDown("right_shift")local at = input:mousePos()if shift and input:buttonDown("right") and at then aim(at.x, at.y) endvar input = Input.Instance;
var shift = input.KeyDown(Key.LeftShift) || input.KeyDown(Key.RightShift);if (shift && input.ButtonDown(MouseButton.Right) && input.MousePos() is { } p) Aim(p.X, p.Y);void aim(int x, int y);
// in startauto input = wraith::api().input();
bool shift = input.keyDown(Wraith::Key::left_shift) || input.keyDown(Wraith::Key::right_shift);auto at = input.mousePos();if (shift && input.buttonDown(Wraith::MouseButton::right) && at) aim(at->x, at->y);use wraith::{Key, MouseButton};
fn aim(x: i32, y: i32) {}
fn check() { let input = wraith::api().input();
let shift = input.key_down(Key::LEFT_SHIFT) || input.key_down(Key::RIGHT_SHIFT); if let Some(at) = input.mouse_pos() { if shift && input.button_down(MouseButton::RIGHT) { aim(at.x, at.y); } }}const input = ctx.wraith.input();
const shift = input.keyDown(.left_shift) or input.keyDown(.right_shift);if (shift and input.buttonDown(.right)) { if (input.mousePos()) |p| aim(p.x, p.y);}void aim(int x, int y);
/* in start */Wraith_Input input = Wraith_Api_input(w);
bool shift = Wraith_Input_keyDown(input, Wraith_Key_left_shift) || Wraith_Input_keyDown(input, Wraith_Key_right_shift);Opt_Wraith_Point at = Wraith_Input_mousePos(input);if (shift && Wraith_Input_buttonDown(input, Wraith_MouseButton_right) && at.some) aim(at.value.x, at.value.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.
Taking input from the game
Section titled “Taking input from the game”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);local Wraith = require("wraith").Wraith
local ui = Wraith.ui()
ui:takeInput("mouse", true)ui:showCursor(true)-- ...ui:takeInput("mouse", false)ui:showCursor(false)var ui = Ui.Instance;
ui.TakeInput(Ui.InputDevice.Mouse, true);ui.ShowCursor(true);// ...ui.TakeInput(Ui.InputDevice.Mouse, false);ui.ShowCursor(false);auto ui = wraith::api().ui();
ui.takeInput(Wraith::Ui::InputDevice::mouse, true);ui.showCursor(true);// ...ui.takeInput(Wraith::Ui::InputDevice::mouse, false);ui.showCursor(false);use wraith::UiInputDevice;
let ui = wraith::api().ui();
ui.take_input(UiInputDevice::MOUSE, true);ui.show_cursor(true);// ...ui.take_input(UiInputDevice::MOUSE, false);ui.show_cursor(false);const ui = ctx.wraith.ui();
ui.takeInput(.mouse, true);ui.showCursor(true);// ...ui.takeInput(.mouse, false);ui.showCursor(false);Wraith_Ui ui = Wraith_Api_ui(w);
Wraith_Ui_takeInput(ui, Wraith_Ui_InputDevice_mouse, true);Wraith_Ui_showCursor(ui, true);/* ... */Wraith_Ui_takeInput(ui, Wraith_Ui_InputDevice_mouse, false);Wraith_Ui_showCursor(ui, false);Players can stop controllers being taken at all in Wraith’s settings.
Games that leave the cursor free
Section titled “Games that leave the cursor free”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;local Wraith = require("wraith").Wraith
local looking = Wraith.input():buttonDown("right")Wraith.ui():holdMouse(looking)if looking then yaw = yaw + Wraith.ui():mouseMotion().x * 0.002 endvar looking = Input.Instance.ButtonDown(MouseButton.Right);Ui.Instance.HoldMouse(looking);if (looking) yaw += Ui.Instance.MouseMotion().X * 0.002f;static float yaw;
// in startbool looking = wraith::api().input().buttonDown(Wraith::MouseButton::right);wraith::api().ui().holdMouse(looking);if (looking) yaw += wraith::api().ui().mouseMotion().x * 0.002f;use wraith::MouseButton;
fn look(yaw: &mut f32) { let looking = wraith::api().input().button_down(MouseButton::RIGHT); wraith::api().ui().hold_mouse(looking); if looking { *yaw += wraith::api().ui().mouse_motion().x as f32 * 0.002; }}const looking = ctx.wraith.input().buttonDown(.right);ctx.wraith.ui().holdMouse(looking);if (looking) yaw += @as(f32, @floatFromInt(ctx.wraith.ui().mouseMotion().x)) * 0.002;static float yaw;
/* in start */bool looking = Wraith_Input_buttonDown(Wraith_Api_input(w), Wraith_MouseButton_right);Wraith_Ui_holdMouse(Wraith_Api_ui(w), looking);if (looking) yaw += Wraith_Ui_mouseMotion(Wraith_Api_ui(w)).x * 0.002f;wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
