--- a/src/Prompt.tsx
+++ b/src/Prompt.tsx
@@ -1,124 +1,51 @@
import { getUniqueExtCmdAutocomplete } from "../lib/nethackInterface";
-import {
- ESC,
- ExtCmdPrompt as ExtCmdPromptType,
- GetlinPrompt as GetlinPromptType,
- Prompt as PromptType,
- YNPrompt,
-} from "./nethack";
+import { ESC, type Prompt as PromptType, type YNPrompt } from "./nethack";
import { useOnInput } from "./useNethack";
-const Kbd = ({
- children,
- className,
-}: {
- children: string;
- className?: string;
-}) => {
- const onInput = useOnInput();
- return (
-
- );
-};
-
-const YNQuestion = ({ question, choices, def }: YNPrompt) => {
- return (
-
- );
-};
-
-const ExtCmdPrompt = ({ cmd }: ExtCmdPromptType) => {
- const autocomplete = getUniqueExtCmdAutocomplete(cmd.toLowerCase());
- const onInput = useOnInput();
- const resetPrompt = () => {
- onInput(ESC);
- };
- return (
-
- {autocomplete && (
-
- #{autocomplete.name} ({autocomplete.description})
-
+const YNQuestion = ({ question, choices, def }: YNPrompt) => (
+
+
+ {question}
+ {choices && (
+ <>
+ {" ["}
+ {choices.split("").map((char, index) => (
+
+ {char === ESC ? "Esc" : char}
+
+ ))}
+ {"]"}
+ >
)}
-
- {"#"}
- {
- onInput(e.target.value);
- }}
- onKeyDown={(e) => {
- e.stopPropagation();
- if (e.key == "Enter") {
- onInput({ submit: true });
- } else if (e.key == "Escape") {
- resetPrompt();
- }
- }}
- />
-
- );
-};
+
+);
-const GetlinPrompt = ({ prompt, answer }: GetlinPromptType) => {
+const PromptInput = ({ label, value }: { label: string; value: string }) => {
const onInput = useOnInput();
- const resetPrompt = () => {
- onInput(ESC);
- };
return (
-
-
- {prompt + " "}
- {
- onInput(e.target.value);
- }}
- onKeyDown={(e) => {
- e.stopPropagation();
- if (e.key == "Enter") {
- onInput({ submit: true });
- } else if (e.key == "Escape") {
- resetPrompt();
- }
- }}
- />
-
-
+
onInput(event.target.value)}
+ onKeyDown={(event) => {
+ if (event.nativeEvent.isComposing) return;
+ if (event.key === "Enter") {
+ event.preventDefault();
+ onInput({ submit: true });
+ } else if (event.key === "Escape") {
+ event.preventDefault();
+ onInput(ESC);
+ }
+ }}
+ />
);
};
@@ -126,9 +53,27 @@
switch (prompt.type) {
case "yn":
return
;
- case "extcmd":
- return
;
+ case "extcmd": {
+ const command = prompt.cmd.toLowerCase();
+ const autocomplete = getUniqueExtCmdAutocomplete(command);
+ return (
+
+ {autocomplete && (
+
+ #{autocomplete.name} ({autocomplete.description})
+
+ )}
+
+
+ );
+ }
case "getlin":
- return
;
+ return (
+
+ );
}
};
--- a/src/TextWindow.tsx
+++ b/src/TextWindow.tsx
@@ -1,25 +1,25 @@
import { Text } from "./Common";
import { NHMenuWindow, NHTextWindow } from "./nethack";
-import { useOnInput } from "./useNethack";
export const TextWindow = ({
window_,
}: {
window_: NHTextWindow | NHMenuWindow;
}) => {
- const onInput = useOnInput();
+ const informational = !("selectionMode" in window_) ||
+ window_.selectionMode === null || window_.selectionMode === "PICK_NONE";
return (
{
- onInput(" ");
- }}
+ tabIndex={-1}
+ data-window-kind={informational ? "information" : "selection"}
>
{window_.text.map(({ str, attr }, i) => (
{str}
))}
+ {informational &&
Press Space to continue.
}
);
};
--- a/src/nethack.ts
+++ b/src/nethack.ts
@@ -34,6 +34,38 @@
const ENTER = "\n";
const BACKSPACE = "\x08";
+const movementKeys = new Map([
+ ["ArrowLeft", "h"], ["ArrowDown", "j"], ["ArrowUp", "k"], ["ArrowRight", "l"],
+ ["Numpad1", "b"], ["Numpad2", "j"], ["Numpad3", "n"], ["Numpad4", "h"],
+ ["Numpad5", "."], ["Numpad6", "l"], ["Numpad7", "y"], ["Numpad8", "k"], ["Numpad9", "u"],
+]);
+
+function terminalKey(event: KeyboardEvent, direction: boolean) {
+ const { key, code, ctrlKey, altKey, metaKey, shiftKey } = event;
+ const altGraph = event.getModifierState("AltGraph");
+ if (event.defaultPrevented || event.isComposing || metaKey) return;
+ if (key === "Escape") return ESC;
+ if (key === "Enter") return ENTER;
+ if (key === "Backspace") return BACKSPACE;
+ if (direction && !ctrlKey && !altKey) {
+ const movement = movementKeys.get(code) ?? movementKeys.get(key);
+ if (movement) return shiftKey ? movement.toUpperCase() : movement;
+ }
+ if (ctrlKey && !altGraph) {
+ if (key.toLowerCase() === "c" && window.getSelection()?.isCollapsed === false) return;
+ const ascii = key.length === 1 ? key.toUpperCase().charCodeAt(0) : 0;
+ if (ascii >= 64 && ascii <= 95) return String.fromCharCode(ascii - 64);
+ return;
+ }
+ if (altKey && !altGraph) {
+ const character = /^[ -~]$/.test(key) ? key :
+ /^Key[A-Z]$/.test(code) ? (shiftKey ? code[3] : code[3].toLowerCase()) : "";
+ if (character) return String.fromCharCode(character.charCodeAt(0) + 128);
+ return;
+ }
+ if (/^[ -~]$/.test(key)) return key;
+}
+
export class NHMenuWindow {
constructor(public id: number) {}
type = "NHW_MENU" as const;
@@ -42,10 +74,12 @@
text: { str: string; attr: Attr }[] = [];
menuEntries: NHMenuEntry[] = [];
prompt = "";
+ selectionMode: How | null = null;
customAcceleratorIndex = 0;
customAccelerators = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
startMenu() {
this.menuEntries = [];
+ this.selectionMode = null;
this.customAcceleratorIndex = 0;
}
endMenu(prompt: string) {
@@ -104,7 +138,7 @@
}
case "PICK_ONE": {
const { char, count } = await this.getCharWithCount(getChar);
- if (char == ESC || char == " ") {
+ if (char === ESC || char === " " || char === ENTER) {
this.displayed = false;
return [];
}
@@ -122,7 +156,11 @@
while (!exited) {
const { char, count } =
await this.getCharWithCount(getChar);
- if (char == ESC || char == " ") {
+ if (char === ESC) {
+ this.displayed = false;
+ return [];
+ }
+ if (char === " " || char === ENTER) {
exited = true;
} else if (char == ",") {
for (const entry of this.menuEntries) {
@@ -374,7 +412,6 @@
export class NetHack implements NetHackInterface {
start(playerName: string) {
startNethack(this, {
- LOG_PROCESSED_CALLS: true,
commandLineArguments: ["-u", playerName],
});
}
@@ -443,38 +480,33 @@
resolve_(input);
this.resolveInput = () => {};
};
- const callback = (e: KeyboardEvent) => {
- const resolve = (input: Input) => {
- e.preventDefault();
- window.removeEventListener("keydown", callback);
- this.waitingForInput = false;
- resolve_(input);
- this.resolveInput = () => {};
- };
- if (e.key.length == 1) {
- if (e.ctrlKey) {
- resolve(
- String.fromCharCode(
- e.key.toUpperCase().charCodeAt(0) -
- "A".charCodeAt(0) +
- 1,
- ),
- );
- } else if (e.altKey) {
- resolve(String.fromCharCode(e.key.charCodeAt(0) + 128));
- } else {
- resolve(e.key);
+ const callback = (event: KeyboardEvent) => {
+ const target = event.target;
+ if (!(target instanceof Element) || target.closest("a, button, input, select, textarea, [contenteditable]")) return;
+ const terminal = target.closest("#nethack-terminal");
+ if (!terminal) return;
+ const prompt = this.prompt;
+ if (prompt?.type === "getlin" || prompt?.type === "extcmd") return;
+ const direction = prompt?.type === "poskey" ||
+ (prompt?.type === "yn" && !prompt.choices && /\bdirection\b/i.test(prompt.question));
+ if (!direction && !event.defaultPrevented && !event.isComposing &&
+ !event.metaKey && !event.altKey && !event.ctrlKey) {
+ const down = event.key === "ArrowDown" || event.key === "PageDown";
+ const up = event.key === "ArrowUp" || event.key === "PageUp";
+ if (down || up) {
+ const window = [...terminal.querySelectorAll(".temporary-windows > *")].at(-1);
+ if (window) {
+ event.preventDefault();
+ const distance = event.key.startsWith("Page") ? window.clientHeight : 40;
+ window.scrollBy(0, down ? distance : -distance);
+ return;
+ }
}
}
- if (e.key == "Escape") {
- resolve(ESC);
- }
- if (e.key == "Enter") {
- resolve(ENTER);
- }
- if (e.key == "Backspace") {
- resolve(BACKSPACE);
- }
+ const key = terminalKey(event, direction);
+ if (key === undefined) return;
+ event.preventDefault();
+ this.resolveInput(key);
};
window.addEventListener("keydown", callback);
});
@@ -709,6 +741,7 @@
async selectMenu(windowId: number, how: How) {
const win = this.windows[windowId] as NHMenuWindow;
win.displayed = true;
+ win.selectionMode = how;
return await win.selectMenu(how, this.getChar.bind(this));
}
async messageMenu() {
--- a/src/settings.ts
+++ b/src/settings.ts
@@ -36,7 +36,7 @@
playerName: "Adventurer",
nethackrc: nethackrcContents,
compactStatus: window.matchMedia("(max-width: 600px)").matches,
- saveOnHide: true,
+ saveOnHide: false,
gridButtons: [
"look", "cast", "fire",
"run", "drop-type", "inven",