Skip to content

Normalize key codes and show arrow names in move prompt - #13

Merged
su8 merged 2 commits into
su8:mainfrom
lasers:fix/key-names
Oct 23, 2026
Merged

su8 merged 2 commits into
su8:mainfrom
lasers:fix/key-names

Conversation

@lasers

@lasers lasers commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #12 — this branch is built on top of it, so until #12 is merged the diff below also shows its commit. Only the last commit (0f7dba8) is new here.

Follow-up to #12, cleaning up the rest of key handling:

  • Move prompt shows arrow names. Arrow keys bound as movement were printed as a broken byte (Move (�/�/�/�)). A new keyName() helper prints ↑ ↓ → ← (and Esc, Space, Enter, …).
  • Same key codes on every platform. Windows readKey() now returns the same KEY_UP/KEY_DOWN/KEY_RIGHT/KEY_LEFT codes as Unix instead of 0xE0xx. Other special keys are ignored like Esc.
  • Whole escape sequences are read. Keys like Home, F1, F5 or Ctrl+arrow no longer leave stray bytes behind that count as extra key presses.
  • Piped input works again. When stdin isn't a terminal, readKey() reads through std::cin instead of quitting (readNumber() may already have buffered the input there).
  • Unbound keys don't explore the current room again. Before, pressing any key that wasn't bound (e.g. x or Esc) replayed the current room: a free trap or fight.

Testing

Built with -Wall -Wextra -Wpedantic -Wconversion -Wshadow (no new warnings) and driven in a pseudo-terminal on macOS: arrows in both ESC [ and ESC O forms, Home/F1/F5/Ctrl+→/Delete/Esc ignored, upper-case P/Q, piped input played to the end, stdin from /dev/null exits cleanly.

The Windows branch is not tested. It would be great if someone could check arrow keys there.

readKey() now turns off ECHO and reads an arrow key's escape sequence
while still in raw mode, so one arrow press is one key and macOS no
longer echoes "[C". Keys are compared as ints and lowercased at setup,
so upper-case and arrow keys match. A closed stdin exits instead of
looping forever.
- Windows readKey() returns the same KEY_UP/DOWN/RIGHT/LEFT codes as
  Unix; other special keys are ignored like Esc.
- Unix readKey() reads the whole escape sequence, so Home, F1 or
  Ctrl+arrow no longer leave stray bytes that count as key presses.
- Piped (non-terminal) stdin is read through std::cin again instead of
  quitting.
- The move prompt names keys with keyName() (arrows show as ←↑→↓
  instead of a broken byte).
- An unbound key no longer explores the current room again.
@su8

su8 commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Hi @lasers ,

Thank you for fixing termios , just tested and it works in Windows 11. Now will try debian/Free/Net BSD to see if this pull request works in these Unix OS's.

Thanks again !

Cheers mate

@su8

su8 commented Oct 23, 2026

Copy link
Copy Markdown
Owner

It's working in the Unix and WIndows 11, merging.

Thank you so much mate :)

Cheers

@su8
su8 merged commit 680e880 into su8:main Oct 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants