Skip to content

Commit

Permalink
better following spec
Browse files Browse the repository at this point in the history
  • Loading branch information
unxed committed Jan 28, 2025
1 parent 7e493e1 commit 7e4d03b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions far2l/src/vt/vtshell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#define FOCUS_CHANGED_SEQ_INACTIVE "\x1b[O"

const char *VT_TranslateSpecialKey(const WORD key, bool ctrl, bool alt, bool shift, unsigned char keypad = 0, WCHAR uc = 0);
std::string VT_TranslateKeyToKitty(const KEY_EVENT_RECORD &KeyEvent, int kitty_kb_flags);
std::string VT_TranslateKeyToKitty(const KEY_EVENT_RECORD &KeyEvent, int kitty_kb_flags, unsigned char keypad);

#if 0 //change to 1 to enable verbose I/O reports to stderr
static void DbgPrintEscaped(const char *info, const char *s, size_t l)
Expand Down Expand Up @@ -815,7 +815,7 @@ class VTShell : VTOutputReader::IProcessor, VTInputReader::IProcessor, IVTShell
}

if (_kitty_kb_flags) {
std::string as_kitty = VT_TranslateKeyToKitty(KeyEvent, _kitty_kb_flags);
std::string as_kitty = VT_TranslateKeyToKitty(KeyEvent, _kitty_kb_flags, _keypad);
if (as_kitty != "") { return as_kitty; }
}

Expand Down
53 changes: 42 additions & 11 deletions far2l/src/vt/vtshell_translation_kitty.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "headers.hpp"
#include <string>

const char *VT_TranslateSpecialKey(const WORD key, bool ctrl, bool alt, bool shift, unsigned char keypad = 0, WCHAR uc = 0);

/**
References:
Expand Down Expand Up @@ -42,7 +44,7 @@


//const WORD key, bool ctrl, bool alt, bool shift, unsigned char keypad, WCHAR uc
std::string VT_TranslateKeyToKitty(const KEY_EVENT_RECORD &KeyEvent, int flags)
std::string VT_TranslateKeyToKitty(const KEY_EVENT_RECORD &KeyEvent, int flags, unsigned char keypad)
{
std::string out;
int shifted = 0;
Expand All @@ -62,6 +64,14 @@ std::string VT_TranslateKeyToKitty(const KEY_EVENT_RECORD &KeyEvent, int flags)
const bool alt = (KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) != 0;
const bool shift = (KeyEvent.dwControlKeyState & (SHIFT_PRESSED)) != 0;

// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/#disambiguate-escape-codes
const bool disambiguate = (
(ctrl && !alt && !shift) || // Ctrl+Key
(!ctrl && alt && !shift) || // Alt+Key
(ctrl && alt && !shift) || // Ctrl+Alt+Key
(!ctrl && alt && shift) // Shift+Alt+Key
);


// if mode 8 is not set, we should not report releases of some keys
// see https://github.com/kovidgoyal/kitty/issues/8212
Expand Down Expand Up @@ -98,26 +108,47 @@ std::string VT_TranslateKeyToKitty(const KEY_EVENT_RECORD &KeyEvent, int flags)
(KeyEvent.wVirtualKeyCode == VK_OEM_COMMA) || // ,<
(KeyEvent.wVirtualKeyCode == VK_OEM_PERIOD) || // .>
(KeyEvent.wVirtualKeyCode == VK_OEM_2) // /?
) && ctrl|alt) ||
) && disambiguate) ||
// Keypad
(KeyEvent.wVirtualKeyCode >= VK_NUMPAD0 &&
KeyEvent.wVirtualKeyCode <= VK_NUMPAD9 &&
KeyEvent.wVirtualKeyCode != VK_NUMPAD5) || // See https://github.com/kovidgoyal/kitty/issues/8256
(KeyEvent.wVirtualKeyCode == VK_DECIMAL) ||
(KeyEvent.wVirtualKeyCode == VK_SEPARATOR) ||
(KeyEvent.wVirtualKeyCode == VK_CLEAR) || // Fixme: workaround; far2l is not sending VK_CLEAR in tty at all
((KeyEvent.wVirtualKeyCode == VK_RETURN) && (KeyEvent.dwControlKeyState & ENHANCED_KEY)) || // keypad Enter
// Other key combinations that can not be represented in legacy encoding
// See https://github.com/kovidgoyal/kitty/issues/8255
// and https://github.com/kovidgoyal/kitty/issues/8263
((KeyEvent.wVirtualKeyCode == VK_RETURN) && (ctrl|alt|shift)) ||
((KeyEvent.wVirtualKeyCode == VK_TAB) && (ctrl|alt|shift)) ||
((KeyEvent.wVirtualKeyCode == VK_BACK) && (ctrl|alt|shift)) ||
((KeyEvent.wVirtualKeyCode == VK_SPACE) && (ctrl|alt))
// (KeyEvent.uChar.UnicodeChar && (ctrl|alt)) // redundancy
((KeyEvent.wVirtualKeyCode == VK_RETURN) && (KeyEvent.dwControlKeyState & ENHANCED_KEY)) // keypad Enter
))
);

if ((flags & 1) && !kitty) {

// Other key combinations that can not be represented in legacy encoding
// See https://github.com/kovidgoyal/kitty/issues/8263

// Get legacy representation
const char *spec = VT_TranslateSpecialKey(
KeyEvent.wVirtualKeyCode, ctrl, alt, shift, keypad, KeyEvent.uChar.UnicodeChar);

if (!spec) {
// No representation in legacy mode. Use kitty.
kitty = true;
} else if (!((spec[0] >= 0x00 && spec[0] <= 0x1F) || (spec[0] == 0x7F))) {
// Special char, not text. Use legacy.
} else if (
((KeyEvent.wVirtualKeyCode == VK_RETURN) && !(ctrl|alt|shift)) ||
((KeyEvent.wVirtualKeyCode == VK_TAB) && !(ctrl|alt|shift)) ||
((KeyEvent.wVirtualKeyCode == VK_BACK) && !(ctrl|alt|shift))
) {
// Key is enter/tab/backspace press without modifiers. Use legacy.
} else {
kitty = true;
}

if (!kitty) {
return spec;
}
}

if (!kitty) {
return std::string();
}
Expand Down

0 comments on commit 7e4d03b

Please sign in to comment.