From 6e73cd813db78e9a5d3356c75155242ec96d38e1 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 31 Oct 2026 09:58:57 +0800 Subject: [PATCH 1/2] Add a walk key that tilts the keyboard's sticks part of the way A keyboard key is all or nothing, so the game only ever saw a full tilt from it and the keyboard could not walk. While the key named by walk_key is held, the two keyboard stick samples are scaled to walk_scale, the same thing yuzu and eden do with their modifier setting; the gamepad's axes and the mouse camera keep their own values. 0.51 rather than half: a sample of 32767 times 0.5 is 16383, one short of the 16384 the game's stick-direction bits need, so half a tilt would leave the direction clear while the axis moved. No key is on by default, because Shift is B in the default bindings. The Controls tab lists the keys players pick, steps the scale in hundredths and names the action a key also presses. Co-authored-by: deepseek-v4.1-flash-1017 --- runtime/cfg.c | 7 +++++-- runtime/input.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ runtime/ui/menu.cpp | 33 ++++++++++++++++++++++++++++++ runtime/ui_text.c | 14 +++++++++++++ runtime/ui_text.h | 1 + totk.ini.example | 8 ++++++++ 6 files changed, 111 insertions(+), 2 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index d9c4cd61..5f978bee 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -50,7 +50,8 @@ static const char *const known[] = { "SHADER_MEMO", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", - "WB_EXACT", "WB_FENCE_LIMIT", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", + "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS", + "ZSTD_CHECK", "ZSTD_NATIVE", }; // The totk.ini section a key cfg_set may write goes to when the file lacks it (totk.ini.example's sections). @@ -66,7 +67,8 @@ static const struct { const char *key, *section; } sections[] = { {"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, {"VOLUME", "audio"}, {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, - {"PAD_LAYOUT", "input"}, {"STICK_DEADZONE", "input"}, {"MOUSE_SENS", "input"}, {"INPUT_RECORD", "input"}, + {"PAD_LAYOUT", "input"}, {"STICK_DEADZONE", "input"}, {"MOUSE_SENS", "input"}, + {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, {"BIND_A", "input"}, {"BIND_B", "input"}, {"BIND_X", "input"}, {"BIND_Y", "input"}, {"BIND_LCLICK", "input"}, {"BIND_RCLICK", "input"}, {"BIND_L", "input"}, {"BIND_R", "input"}, {"BIND_ZL", "input"}, {"BIND_ZR", "input"}, {"BIND_PLUS", "input"}, {"BIND_MINUS", "input"}, {"BIND_LEFT", "input"}, {"BIND_UP", "input"}, @@ -115,6 +117,7 @@ static const CfgMeta metas[] = { {"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "auto", "auto|nintendo|xbox|xbox_prompts", LIVE}, {"STICK_DEADZONE", "8", "0..50", LIVE}, {"MOUSE_SENS", "1", "0..10", LIVE}, + {"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE}, {"MENU_LANG", "", "en|ru", LIVE}, // Advanced: scheduling {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART}, diff --git a/runtime/input.c b/runtime/input.c index 86391739..9508fe97 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -236,6 +236,52 @@ static float mouse_sens_get(void) { } +// ---- walk key: the keyboard's stick samples scaled down while a key is held (TOTK_WALK_KEY, TOTK_WALK_SCALE) +// A keyboard key is all or nothing, so every keyboard-driven stick sample is +/-32767 and the game only ever sees a +// full tilt. Holding this key scales those two samples, which gives the half tilt a gamepad gets from its analog range, +// so the keyboard can walk as well as run. yuzu and eden do the same with their modifier and modifier_scale settings. +// Only the keyboard is scaled: the gamepad's axes and the mouse camera sample their own values. +static int walk_key = KEY_NONE; // TOTK_WALK_KEY as a KEY_*, KEY_NONE = off; live (cfg_version) +static float walk_scale = 0.51f; // TOTK_WALK_SCALE, 0.2 to 0.9; live (cfg_version) +static unsigned walk_seen = ~0u; + +// The ini takes the key names the Controls tab writes; a hand-written line may spell them in any case. +static int name_matches(const char *name, const char *key) { + for (; *name && *key; name++, key++) { + unsigned char a = (unsigned char)*name, b = (unsigned char)*key; + if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); + if (b >= 'A' && b <= 'Z') b = (unsigned char)(b - 'A' + 'a'); + if (a != b) return 0; + } + return *name == *key; +} + +static void walk_get(void) { + unsigned ver = cfg_version(); + if (ver == walk_seen) return; + walk_seen = ver; + const char *name = cfg_value("TOTK_WALK_KEY"); // "none" (the default) turns the key off + int key = KEY_NONE; + if (*name && !name_matches(name, "none")) { + for (int k = KEY_A; k < KEY_COUNT && key == KEY_NONE; k++) + if (input_key_name(k) && name_matches(name, input_key_name(k))) key = k; + if (key == KEY_NONE) rt_log("input: walk_key: unknown key '%s', the keyboard sticks keep their full tilt", name); + } + // 0.51 rather than 0.5: a keyboard sample is 32767, and 32767 * 0.5 = 16383 falls one short of stick_bits' + // STICK_DIR (16384), which would leave the game's stick-direction bits clear while the walk key is held. The value + // in the settings table carries that default; this code only rejects what the table's range does not allow. + const char *sv = cfg_value("TOTK_WALK_SCALE"); + char *end; + double d = strtod(sv, &end); + double s = end == sv || *end || !(d >= 0.2 && d <= 0.9) ? strtod(cfg_meta("TOTK_WALK_SCALE")->def, NULL) : d; + if (key != walk_key || (float)s != walk_scale) { + walk_key = key; + walk_scale = (float)s; + rt_log("input: walk key %s, keyboard sticks at %.0f%% of their tilt (mouse and gamepad unchanged)", + key != KEY_NONE ? input_key_name(key) : "off", s * 100); + } +} + // The mouse camera as a right-stick sample, held for the whole presented frame. Under `lock`. static void mouse_camera(int32_t out[2]) { static uint64_t frame = ~0ull, t0; @@ -456,9 +502,13 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx was_captured = cap; b |= input_map_buttons(down); int32_t k[2]; + walk_get(); + int walk = walk_key != KEY_NONE && down[walk_key]; input_map_stick(down, 0, k); + if (walk) k[0] = (int32_t)(k[0] * walk_scale), k[1] = (int32_t)(k[1] * walk_scale); pick(&sl[0], &sl[1], k[0], k[1]); input_map_stick(down, 1, k); + if (walk) k[0] = (int32_t)(k[0] * walk_scale), k[1] = (int32_t)(k[1] * walk_scale); pick(&sr[0], &sr[1], k[0], k[1]); mouse_camera(k); pick(&sr[0], &sr[1], k[0], k[1]); diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 0e02e4bf..cbe200e3 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -668,6 +668,37 @@ static const char *pad_layout_note(void) { return note; } +// The action a key presses, or -1: the walk key is a key of its own, but Shift is B in the default bindings, and a +// walk key that also presses something walks and does that something at the same time. +static int key_action(int key) { + for (int a = 0; a < ACTION_COUNT; a++) { + uint8_t keys[2]; + int n = input_map_keys_of(&input_map, a, keys, 2); + for (int i = 0; i < n; i++) + if (keys[i] == key) return a; + } + return -1; +} + +// The walk key as the handful of keys a player is likely to pick. "none" rather than an empty value closes it: an +// unset key falls back to the default in the settings table, the same as a fresh installation. totk.ini takes any name +// input_map.c knows (walks of the keyboard sticks, not of the gamepad). A key the current bindings also use says which +// action it presses. +static void row_walk_key(void) { + static const char *const keys[] = {"none", "lshift", "lalt", "lctrl", "tab", "capslock"}; + const char *names[sizeof keys / sizeof *keys] = {menu_text(UI_MENU_OFF), "Shift", "Alt", "Ctrl", "Tab", "Caps Lock"}; + const char *cur = cfg_value("TOTK_WALK_KEY"); + static char note[64]; + const char *n = nullptr; + for (int k = KEY_A; k < KEY_COUNT && !n; k++) { + if (!input_key_name(k) || _stricmp(input_key_name(k), cur)) continue; + int a = key_action(k); + if (a >= 0 && input_action_name(a)) snprintf(note, sizeof note, menu_text(UI_MENU_WALK_KEY_TAKEN), input_action_name(a)), n = note; + } + opt_choice_list("TOTK_WALK_KEY", UI_MENU_WALK_KEY, keys, names, sizeof keys / sizeof *keys, UI_MENU_WALK_KEY_DESC, + -1, n); +} + static void tab_controls(float scale) { opt_begin("##controls", scale); static const UiTextId layout[] = {UI_MENU_PAD_LAYOUT_AUTO, UI_MENU_PAD_LAYOUT_NINTENDO, UI_MENU_PAD_LAYOUT_XBOX, @@ -679,6 +710,8 @@ static void tab_controls(float scale) { opt_toggle("TOTK_RUMBLE_TRIGGERS", UI_MENU_RUMBLE_TRIGGERS, UI_MENU_RUMBLE_TRIGGERS_DESC); opt_slider("TOTK_STICK_DEADZONE", UI_MENU_STICK_DEADZONE, 0, 50, UI_MENU_STICK_DEADZONE_DESC, 1, "%.0f %%"); opt_slider("TOTK_MOUSE_SENS", UI_MENU_MOUSE_SENS, 0, 10, UI_MENU_MOUSE_SENS_DESC, 0.1f, "%.1f"); + row_walk_key(); + opt_slider("TOTK_WALK_SCALE", UI_MENU_WALK_SCALE, 0.2f, 0.9f, UI_MENU_WALK_SCALE_DESC, 0.01f, "%.2f"); binds_rows(scale); opt_end(); binds_after_page(); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index f697e68c..716cafb0 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -136,6 +136,13 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_STICK_DEADZONE_DESC] = "How far a gamepad stick moves before the game sees it. Raise it if the character or the camera moves on its own.", [UI_MENU_MOUSE_SENS] = "Mouse camera sensitivity", [UI_MENU_MOUSE_SENS_DESC] = "How fast the mouse turns the camera.", + [UI_MENU_WALK_KEY] = "Walk key", + [UI_MENU_WALK_KEY_DESC] = "Hold it to tilt the keyboard's sticks part of the way, so the keyboard walks instead of " + "runs. The walk scale sets how far. A controller and the mouse are not affected.", + [UI_MENU_WALK_KEY_TAKEN] = "also %s", + [UI_MENU_WALK_SCALE] = "Walk scale", + [UI_MENU_WALK_SCALE_DESC] = "The tilt the keyboard's sticks get while the walk key is held. Half is a walk, the " + "full tilt a run.", [UI_BINDS_HELP] = "Select a cell and press A or Enter, then the key, mouse button or controller button. Delete or " "X clears a cell. A key in use moves here; a controller button swaps with the one it replaces.", @@ -397,6 +404,13 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_STICK_DEADZONE_DESC] = "Насколько нужно отклонить стик геймпада, чтобы игра это заметила. Увеличьте, если персонаж или камера двигаются сами.", [UI_MENU_MOUSE_SENS] = "Чувствительность мыши", [UI_MENU_MOUSE_SENS_DESC] = "Как быстро мышь поворачивает камеру.", + [UI_MENU_WALK_KEY] = "Клавиша шага", + [UI_MENU_WALK_KEY_DESC] = "Удерживайте: стики клавиатуры отклоняются частично, и персонаж идёт, а не бежит. " + "Насколько — задаёт сила шага. Контроллер и мышь не затрагиваются.", + [UI_MENU_WALK_KEY_TAKEN] = "также %s", + [UI_MENU_WALK_SCALE] = "Сила шага", + [UI_MENU_WALK_SCALE_DESC] = "Отклонение стиков клавиатуры при удержании клавиши шага. Половина — шаг, полное " + "отклонение — бег.", [UI_BINDS_HELP] = "Выберите ячейку и нажмите A или Enter, затем клавишу, кнопку мыши или кнопку контроллера. " "Delete или X очищает ячейку. Занятая клавиша переходит сюда; кнопка контроллера меняется " diff --git a/runtime/ui_text.h b/runtime/ui_text.h index a2088960..f63cb61c 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -68,6 +68,7 @@ typedef enum { UI_MENU_PAD_LAYOUT_PROMPTS_MISSING, UI_MENU_PAD_LAYOUT_PROMPTS_RESTART, UI_MENU_STICK_DEADZONE, UI_MENU_STICK_DEADZONE_DESC, UI_MENU_MOUSE_SENS, UI_MENU_MOUSE_SENS_DESC, + UI_MENU_WALK_KEY, UI_MENU_WALK_KEY_DESC, UI_MENU_WALK_KEY_TAKEN, UI_MENU_WALK_SCALE, UI_MENU_WALK_SCALE_DESC, // Controls tab: the remapping rows (ui/binds_page.cpp) UI_BINDS_HELP, UI_BINDS_COL_KEY1, UI_BINDS_COL_KEY2, UI_BINDS_COL_PAD, diff --git a/totk.ini.example b/totk.ini.example index 6d3d5f1e..220d6289 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -192,6 +192,14 @@ # stick_deadzone = 8 # Mouse camera sensitivity (0 to 10; 0 = no mouse camera and no cursor capture). # mouse_sens = 1 +# The keyboard's sticks are all or nothing, so the game only sees a full tilt from them; while this key is held the +# keyboard-driven stick values shrink and the keyboard can walk instead of run. Key names as in the bind_ lines below, +# none turns it off. 0.2 to 0.9, the default holds for anything else. Only the keyboard is scaled: the mouse camera and +# the gamepad are unchanged. The Controls tab sets both. The default stops just above half because a keyboard sample of +# 32767 times 0.5 is one short of the 16384 the game's stick-direction bits need, and below that the keyboard stick +# reads as a partial tilt only. +# walk_key = lshift +# walk_scale = 0.51 # Write the input of this run to a file that replays as a script (docs/controls.md, "Scripted input"). Default: off. # input_record = input.txt # Remap buttons: the Controls tab of the settings menu (F1) writes these lines. One line per Switch button or stick From 24c4a27e0b5297b70d438239ed8b4162bc7571f8 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 31 Oct 2026 17:02:22 +0800 Subject: [PATCH 2/2] Keep the game's input blocked until the sticks are released The menu and the software keyboard hold the game's input until everything they saw is released again, and that release test counted buttons only. With the walk key a keyboard stick moves part way without setting a direction bit, so the movement went through the moment the menu or the text entry closed; the four axes count as held now. The walk key's conflict note reads the bindings from a snapshot rather than the live table that the polling thread rebuilds, and it looks at every key of an action instead of the two its cells show, so a key bound third still names the action it presses. Co-authored-by: deepseek-v4.1-flash-1017 --- runtime/input.c | 13 +++++++++---- runtime/ui/menu.cpp | 14 +++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/runtime/input.c b/runtime/input.c index 9508fe97..108b967b 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -555,15 +555,20 @@ void input_menu(int open) { ikb_cursor_update(); } -// 1 while flag (g_block, g_menu) blocks the game's input; state 2 ends at the first poll with nothing held. -static int blocked(volatile int32_t *flag, uint64_t buttons) { +// 1 while flag (g_block, g_menu) blocks the game's input; state 2 ends at the first poll with nothing held. A stick +// axis counts as held as well: with the walk key the keyboard's sticks move part way without setting a direction bit, so +// a gate that watched the buttons alone would let that movement through the moment the menu or the text entry closes. +static int blocked(volatile int32_t *flag, uint64_t buttons, const int32_t *axes) { int32_t k = *flag; - return k && !(k == 2 && !buttons && plat_cas32(flag, 0, 2) == 2); + int held = buttons != 0; + for (int i = 0; i < 4; i++) held |= axes[i] != 0; + return k && !(k == 2 && !held && plat_cas32(flag, 0, 2) == 2); } void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { poll_merged(buttons, lx, ly, rx, ry); - int text = blocked(&g_block, *buttons), menu = blocked(&g_menu, *buttons); + const int32_t axes[4] = {*lx, *ly, *rx, *ry}; + int text = blocked(&g_block, *buttons, axes), menu = blocked(&g_menu, *buttons, axes); if (!text && !menu) return; *buttons = 0; *lx = *ly = *rx = *ry = 0; } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index cbe200e3..8617b158 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -669,11 +669,13 @@ static const char *pad_layout_note(void) { } // The action a key presses, or -1: the walk key is a key of its own, but Shift is B in the default bindings, and a -// walk key that also presses something walks and does that something at the same time. -static int key_action(int key) { +// walk key that also presses something walks and does that something at the same time. The bindings come from a +// snapshot, and every key of an action is looked at: binds_refresh rebuilds the live table on the polling thread while +// the menu draws, and an action may hold more than the two keys its cells show. +static int key_action(const InputMap *m, int key) { for (int a = 0; a < ACTION_COUNT; a++) { - uint8_t keys[2]; - int n = input_map_keys_of(&input_map, a, keys, 2); + uint8_t keys[INPUT_BINDING_MAX]; + int n = input_map_keys_of(m, a, keys, INPUT_BINDING_MAX); for (int i = 0; i < n; i++) if (keys[i] == key) return a; } @@ -690,9 +692,11 @@ static void row_walk_key(void) { const char *cur = cfg_value("TOTK_WALK_KEY"); static char note[64]; const char *n = nullptr; + InputMap m; + input_map_snapshot(&m); for (int k = KEY_A; k < KEY_COUNT && !n; k++) { if (!input_key_name(k) || _stricmp(input_key_name(k), cur)) continue; - int a = key_action(k); + int a = key_action(&m, k); if (a >= 0 && input_action_name(a)) snprintf(note, sizeof note, menu_text(UI_MENU_WALK_KEY_TAKEN), input_action_name(a)), n = note; } opt_choice_list("TOTK_WALK_KEY", UI_MENU_WALK_KEY, keys, names, sizeof keys / sizeof *keys, UI_MENU_WALK_KEY_DESC,