05-03-26

Window-Specific Key Remapper

This Lua script for Rebind remaps keys only when a specific window is active. It includes a UI for configuring key mappings and logging, offering a flexible hardware input solution.

rebindlua scriptkey remapperwindow specifichardware inputluascript

Question

Key remapper only active in specific window

Rebind AI

Key Remapper (Window-Specific)

luau
-- rebind: name=Window-Specific Key Remapper
-- rebind: window=Game Title

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable" }),
    remap_1_from = UI.Keybind("CapsLock", { label = "Remap From", group = "Mapping 1" }),
    remap_1_to = UI.Keybind("Escape", { label = "Remap To", group = "Mapping 1" }),
    
    remap_2_from = UI.Keybind("Mouse4", { label = "Remap From", group = "Mapping 2" }),
    remap_2_to = UI.Keybind("4", { label = "Remap To", group = "Mapping 2" }),
    
    remap_3_from = UI.Keybind("Mouse5", { label = "Remap From", group = "Mapping 3" }),
    remap_3_to = UI.Keybind("5", { label = "Remap To", group = "Mapping 3" }),
})

local remaps = {}

function OnStart()
    remaps = {
        [cfg.remap_1_from] = cfg.remap_1_to,
        [cfg.remap_2_from] = cfg.remap_2_to,
        [cfg.remap_3_from] = cfg.remap_3_to,
    }
    Log.Info("Window-specific remapper loaded")
end

function OnDown(key)
    if not cfg.enabled then
        return true
    end
    
    if remaps[key] then
        HID.Press(remaps[key])
        return false
    end
    
    return true
end

function OnUp(key)
    if not cfg.enabled then
        return true
    end
    
    if remaps[key] then
        return false
    end
    
    return true
end

function OnFocus(window)
    Log.Info("Window focused: " .. window.title)
end

function OnBlur(window)
    Log.Info("Window lost focus")
end

How it works

This script only runs when the specified window is in the foreground. The window= modeline directive automatically activates/deactivates it based on window focus.

Features:

  • Three configurable remaps — edit remap_X_from and remap_X_to in the UI to change what keys remap to what
  • OnFocus/OnBlur logging — shows when the window gains/loses focus in the logs
  • Toggle enable — quickly turn the remapper on/off without leaving the window
  • Zero overhead when inactive — when the target window isn't focused, this script consumes no resources at all

The script blocks the original key and sends the remapped one. If you want to pass the original key through to the game in addition to sending the remapped key, return true from OnDown instead of false.

To use:

  1. Change window=Game Title to match your target window's title (case-insensitive substring match)
  2. Configure the three remaps in the UI, or add more by copying the pattern
  3. The script is automatically active only in that window

Learn more: