Desmos Keybind Assist

Enables you to bind keyboard keys to action expressions in Desmos. Add "@action_bind_key <list of keys>" in a text box before an action to add a key binding.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Desmos Keybind Assist
// @namespace    http://tampermonkey.net/
// @version      2026-01-06
// @description  Enables you to bind keyboard keys to action expressions in Desmos. Add "@action_bind_key <list of keys>" in a text box before an action to add a key binding.
// @author       lemon
// @license      MIT
// @match        https://www.desmos.com/calculator*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=desmos.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function waitForCalc() {
        if (window.Calc) setTimeout(initKeybinds, 1000);
        else setTimeout(waitForCalc, 100);
    }
    function initKeybinds() {
        console.log("[keybind assist] desmos detected, scanning for action bindings...");

        const exprs = Calc.getExpressions();
        const bindings = {}; // key: action id

        for (let i = 0; i < exprs.length; i++) {
            const expr = exprs[i];
            if (expr.type === "text" && expr.text.startsWith("@action_bind_key")) {
                const args = expr.text.trim().split(/\s+/);
                if (args.length < 2) continue;

                const actionExpr = exprs[i + 1];
                if (actionExpr && actionExpr.type === "expression") {
                    let keys = args.slice(1);
                    console.log(`[keybind assist] found binding: key ${keys.join(", ")} --> expression id ${actionExpr.id}:\n${actionExpr.latex}`)
                    for (let k of keys) {
                        k = k.toLowerCase();
                        if (k === "space") k = " ";
                        bindings[k] = actionExpr.id;
                    }
                }
            }
        }
        document.addEventListener("keydown", (e) => {
            const key = e.key.toLowerCase();
            if (bindings[key]) {
                const exprId = bindings[key];
                Calc.controller.dispatch({
                    type: "action-single-step",
                    id: exprId
                });
                console.log(`[keybind assist] key ${key} pressed: triggered action id ${exprId}`);
            }
        });
    }
    waitForCalc();
})();