Skip to content

Add support for DECBKM (Backarrow Key Mode)#13894

Merged
5 commits merged intomicrosoft:mainfrom
j4james:feature-decbkm
Sep 6, 2022
Merged

Add support for DECBKM (Backarrow Key Mode)#13894
5 commits merged intomicrosoft:mainfrom
j4james:feature-decbkm

Conversation

@j4james
Copy link
Copy Markdown
Collaborator

@j4james j4james commented Aug 31, 2022

This PR adds support for the DECBKM sequence, which provides a way for
applications to specify whether the Backspace key should generate a
BS character or a DEL character.

On the original VT100 terminals, the Backarrow key generated a BS,
and there was a separate Delete key that generated DEL. However, on
the VT220 and later, there was only the Backarrow key. By default it
generated DEL, but from the VT320 onwards there was an option to make
it generate BS, and the DECBKM sequence provided a way to control
that behavior programmatically.

On modern terminals, the Backspace key serves as the equivalent of the
VT Backarrow, and typically generates DEL, while Ctrl+Backspace
generates BS. When DECBKM is enabled (for those that support it),
that behavior is reversed, i.e. Backspace generates BS and
Ctrl+Backspace generates DEL.

This PR also gets the other Backspace modifiers more closely aligned
with the expected behavior for modern terminals. The Shift modifier
typically has no effect, and the Alt modifier typically prefixes the
generated sequence with an ESC character.

While not strictly related to DECBKM, I noticed while testing that the
_SetInputMode method was doing unnecessary work that was specific to
the FocusEvent mode. I've now moved that additional processing into
the EnableFocusEventMode method, which I think makes things somewhat
simpler.

Validation Steps Performed

I've tested the basic DECBKM functionality in Vttest, and I've
manually tested all the modifier key combinations to make sure they
match what most modern terminals generate.

I've also added a unit test that confirms that the expected sequences
are generated correctly when the DECBKM mode is toggled.

Closes #13884

@ghost ghost added Issue-Task It's a feature request, but it doesn't really need a major design. Product-Conhost For issues in the Console codebase labels Aug 31, 2022
// It's like a "filter" left to right. Due to the early return via
// !IsConsolePty, once you're at the !enable part, IsConsolePty can only be
// true anymore.
return !_api.IsConsolePty() || !_api.IsVtInputEnabled();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers, if you didn't read the PR description, the additional check here that was specific to the FocusEvent mode has now been moved down into the EnableFocusEventMode method. I think this reduces some of the confusion that was introduced here, and also avoids unnecessary overhead in the other input mode operations.

This is isn't strictly related to DECBKM mode, though (other than as a minor optimisation), so if you'd prefer not to include this change in the PR, it wouldn't be a problem to revert it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes way more sense. Thanks!

Copy link
Copy Markdown
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a single complaint. Thank you!

// It's like a "filter" left to right. Due to the early return via
// !IsConsolePty, once you're at the !enable part, IsConsolePty can only be
// true anymore.
return !_api.IsConsolePty() || !_api.IsVtInputEnabled();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes way more sense. Thanks!

@DHowett DHowett added the AutoMerge Marked for automatic merge by the bot when requirements are met label Sep 6, 2022
@ghost
Copy link
Copy Markdown

ghost commented Sep 6, 2022

Hello @DHowett!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 276405a into microsoft:main Sep 6, 2022
@ghost
Copy link
Copy Markdown

ghost commented Sep 13, 2022

🎉Windows Terminal Preview v1.16.252 has been released which incorporates this pull request.:tada:

Handy links:

@j4james j4james deleted the feature-decbkm branch September 13, 2022 21:10
dscho added a commit to dscho/terminal that referenced this pull request Mar 16, 2026
The reverse VT input path in _DoControlCharacter mapped the byte 0x08
to a Ctrl+Backspace key event (VK_BACK with LEFT_CTRL_PRESSED and
character 0x7F). This was introduced in PR microsoft#3935 (Jan 2020) to make
Ctrl+Backspace delete whole words, which fixed issue microsoft#755. At that time,
the forward path (TerminalInput) also sent 0x08 for Ctrl+Backspace, so
the mapping was internally consistent, if a bit unusual.

In September 2022, PR microsoft#13894 rewrote the forward path to properly
implement DECBKM (Backarrow Key Mode). Under the default DECBKM
setting, TerminalInput now sends 0x08 for plain Backspace and 0x7F for
Ctrl+Backspace. The reverse path was never updated to match, breaking
the roundtrip: a Backspace keypress encodes to 0x08, which decodes back
as Ctrl+Backspace.

The Cygwin project is working on using OpenConsole.exe for its ConPTY
support, and a proposed patch series contains an ugly workaround for
this bug that bypasses the normal input pipe and injects raw
WriteConsoleInput events whenever 0x08 appears in the stream
(https://inbox.sourceware.org/cygwin-patches/20260312113923.1528-4-takashi.yano@nifty.ne.jp/).
Fixing the bug here avoids the need to accept that workaround.

The fix assigns VK_BACK directly and clears writeCtrl, following the
same pattern as the existing L'\x1b' case. This avoids the VkKeyScanW
roundtrip through _GenerateKeyFromChar, which was the root cause: it
reverse-mapped 0x7F to Ctrl+Backspace (since that is the keyboard
combination that produces 0x7F), and then writeCtrl added
LEFT_CTRL_PRESSED back after modifierState was zeroed.

Note that the 0x7F handler (line 221) has the inverse problem: it maps
0x7F to plain Backspace, but the forward path now uses 0x7F for
Ctrl+Backspace. That is a separate issue left for a follow-up.

Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
dscho added a commit to dscho/terminal that referenced this pull request Mar 16, 2026
This is the companion to the previous commit which fixed 0x08. The
forward path in TerminalInput (since PR microsoft#13894, DECBKM support) sends
0x7F for Ctrl+Backspace, but the reverse path in _DoControlCharacter
was mapping 0x7F to a plain Backspace event (VK_BACK with character
0x08, no modifiers).

The deleted comment acknowledged the mapping was a known compromise: it
noted that "The windows telnet expects to send x7f as DELETE, not
backspace" but chose to treat it as Backspace anyway because the old
Windows telnetd could not move the cursor, making the distinction
between delete-forward and backspace moot. That rationale no longer
applies now that the forward path explicitly uses 0x7F for
Ctrl+Backspace.

The fix maps 0x7F to VK_BACK with LEFT_CTRL_PRESSED, restoring
roundtrip symmetry with the forward path: Ctrl+Backspace encodes to
0x7F and now decodes back to Ctrl+Backspace.

Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
dscho pushed a commit to dscho/msys2-runtime that referenced this pull request Mar 27, 2026
In Windows 11, pseudo console has a weird behaviour that the Ctrl-H
is translated into Ctrl-Backspace (not Backspace). The analysis in
https://inbox.sourceware.org/cygwin-patches/2f8628d2-b79a-95a6-480d-7508375958d5@gmx.de/
shows that the reverse VT input path in conhost's
`_DoControlCharacter()` maps the byte 0x08 to a Ctrl+Backspace key event
(VK_BACK with LEFT_CTRL_PRESSED and character 0x7F). This was introduced
in PR microsoft/terminal#3935 (Jan 2020) to make
Ctrl+Backspace delete whole words. In September 2022, PR
microsoft/terminal#13894 rewrote the forward
path to properly implement DECBKM (Backarrow Key Mode), but the reverse
path was never updated to match, breaking the roundtrip.

Similary, Backspace (0x7f) is translated into Ctrl-H. Due to this
behaviour, `inrec_eq()` in `cons_master_thread()` fails to compare
backspace/Ctrl-H events in the input record sequence. This patch is a
workaround for the issue that replaces Ctrl-H with backspace (0x7f),
which will be translated into Ctrl-H in pseudo console.

Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Task It's a feature request, but it doesn't really need a major design. Product-Conhost For issues in the Console codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for DECBKM (Backarrow Key Mode)

3 participants