The bug looked like this in Goxvi on macOS: in Telegram, paste an image, send it, then type Hello. The message comes out as “ello”. With Apple’s built-in ABC keyboard, the same steps give “Hello”. Similar reports came now and then from other apps, with no obvious pattern.
Goxvi is a Vietnamese input method. On macOS it is built on InputMethodKit, Apple’s framework for input methods. (The Windows version is a different program on a different framework; its design is described in an earlier post.) An input method sits between the keyboard and the app. It receives each key press first and decides whether to handle it, so a lost letter is very likely its fault.
My first guess about the cause was wrong. This post is about how a small, privacy-safe debug log found the real cause, and about the rule that came out of it: if an input method swallows a key, it becomes responsible for that key reaching the right place.
A few terms first
- Client. The text field the input method is currently typing into. In InputMethodKit, each key event arrives with the client it is meant for. Goxvi asks the client where the caret is (
selectedRange) and tells it what text to insert. IMKTextInput - Controller. InputMethodKit creates a separate controller object for each client. Each Goxvi controller keeps its own state: the word being typed so far, and the mode it chose for it.
- Activate and deactivate. When a text field gains focus, InputMethodKit activates its client and calls
activateServer:on the controller. When it loses focus, it callsdeactivateServer:. - Engine. Goxvi’s Vietnamese typing logic, shared with the Windows version. It turns keys into text, for example
ddinto “đ”. - Direct mode. Goxvi inserts characters straight into the document with
insertText:replacementRange:. When you typedd, it inserts “d”, then replaces it with “đ”. This is Goxvi’s normal mode, and it needs to know where the caret is so it can replace the right characters. - Marked mode. The input method shows the word in progress as marked text, a temporary run of text the app displays but has not accepted yet, set with
setMarkedText:selectionRange:replacementRange:. The app accepts it when the word ends. Goxvi falls back to this mode when it cannot read the caret, because then it has no position to replace characters at. It also uses it in terminal apps such as Terminal and iTerm2.
The handler for each key, handleEvent:client:, returns a Boolean. IMKServerInput In Goxvi, returning YES means “I handled this key, the app should not see it”, a swallowed key. Returning NO lets the app process the key as if no input method were there. That is what ABC effectively does with a letter.
The wrong first guess
The first theory was that Telegram ignored an insertText: call right after sending, so the letter Goxvi inserted in Direct mode never appeared. It fit the symptom, but nothing had confirmed it.
Before changing anything, I added a debug log. On macOS, os_log writes to the system log, and log stream shows it live:
log stream --predicate 'subsystem == "vn.goxvi.debug"'
This runs in Terminal while you reproduce the bug, and prints one line per log call. The log has one strict rule: it records numbers only. That means caret positions, lengths, modes, kinds of key and object addresses, never the text typed or the characters pressed, because an input method sees everything a person types, including messages and passwords. The log is compiled out of release builds.
GOXVI_DLOG("key ctl=%p client=%p readable=%d loc=%lu len=%lu marked=%d composing=%d mode=%d", ...);
ctl is the Goxvi controller object and client the text field, so the log shows which field received each key.
What the log showed
The log, recorded on November 28, 2026, told a different story from the theory:
- After sending the image, the first key went to a different client from the chat field. That client had no caret: its
selectedRangecame back with locationNSNotFound. - With no readable caret, Goxvi chose Marked mode, set “H” as marked text, and swallowed the key.
- About 5 ms later, that client was deactivated, and the marked text disappeared with it. Then the real chat field was activated.
- The second key,
e, went to the chat field and worked normally.
So Telegram was not ignoring an insert. The first key landed on a short-lived client that existed for a few milliseconds after sending. Goxvi put the letter into that client’s marked text, and the client was thrown away with the letter in it.
This also explained ABC. ABC does not swallow a plain letter. It lets the app handle the key, and Telegram passed it on to the chat field once that field was ready. Goxvi had swallowed the key, so Telegram had nothing to pass on.
Fix 1: give an unchanged first letter back to the app
Goxvi now copies ABC’s behaviour in this one narrow situation. It returns NO, and lets the app handle the key, only when all of these are true:
- this is the first key the client has received since it was activated;
- the caret could not be read at the start of the word (Marked mode);
- the Vietnamese engine left the letter unchanged, so “H” is still just “H”.
// mac/src/composition-session.h
inline bool isUnchangedFirstLetter(KeyKind kind, const KeyOutcome& outcome, wchar_t typed) {
return kind == KeyKind::Letter && typed != 0 && outcome.eaten && !outcome.commitText &&
outcome.markedText.size() == 1 && outcome.markedText.front() == typed;
}
// handleVietnameseKey, Marked mode
if (firstKeyOfClient && _wordStartUnreadable && isUnchangedFirstLetter(kind, outcome, ch)) {
InputModeState::instance().handoff.put(ch, event.timestamp);
return false;
}
The controller sets a flag, _noKeySinceActivate, in activateServer: and clears it on the first key; firstKeyOfClient is the copy taken at the start of that key. In Marked mode the conditions are narrow. In a field that never reports a caret, every later word still uses marked text as before, so the change only touches the one key that was at risk.
Fix 2: let the next field continue the word
The first fix made “Hello” work but revealed a second problem. In Telegram, typing dd right after sending gave “dd” instead of “đ”.
The reason was that Telegram typed the handed-back “d” into the chat field itself, without going through the input method. The chat field’s own controller started at the second key and had never seen the first, so it treated the second d as the start of a new word.
The fix is a small one-shot handoff shared by every controller in the Goxvi process. The short-lived client’s controller stores the letter it gave back, and when it did so. The next client’s first key takes it:
// mac/src/letter-handoff.h
static constexpr double kMaxAgeSeconds = 3.0;
std::optional<wchar_t> take(double now) {
const wchar_t letter = letter_;
letter_ = 0; // one shot: always cleared
if (letter == 0 || now < at_ || now - at_ > kMaxAgeSeconds) return std::nullopt;
return letter;
}
The new controller only uses the letter if it is no more than 3 seconds old, the new field reports a caret with nothing selected, and the character right before that caret really is that letter. The last check is the real guard. If you clicked somewhere else or the app typed something different, the letter is not there and the handoff is ignored. The 3-second window covers a slow second key; the log measured 0.53 s between the two d presses. now < at_ ignores a clock that went backwards.
When both checks pass, Goxvi feeds the letter into its engine as the start of the word, so the next d turns it into “đ”, replacing one character.
A broader change in Direct mode
The same commit changed Direct mode as well, and there the rule is wider. In Direct mode, Goxvi now leaves the unchanged first letter of every word for the app to type, not only the first key after a focus change. Goxvi then follows the word from the next key, using the caret position. Typing ddi after a space still gives “đi”: the app types the first d, and Goxvi replaces it with “đ” on the second.
This makes Goxvi behave like ABC for the most common key, the first letter of a word, in every app that uses Direct mode. It is a deliberate change, but a broad one, and it is the part of this fix with the most remaining checks (below).
How it was checked
Unit tests cover the pure logic, without macOS:
CompositionSessionTest.UnchangedFirstLetterDetectionchecks that only a plain, unchanged letter counts.LetterHandoffTest.FreshLetterIsTakenOncechecks that a stored letter can be taken once, and only once.LetterHandoffTest.StaleOrEmptyGivesNothingchecks that an old letter, an empty handoff and a clock that went backwards all give nothing.DirectInsertTrackerTest.FirstLetterOfWordPassesThroughToAppchecks the Direct-mode rule.
The InputMethodKit part was checked by hand on November 28, 2026. In Telegram, after pasting and sending an image, Hello now arrives complete and dd gives “đ”. Typing in Chrome’s address bar was checked too. The debug log confirmed that the first key was handed back to the app and that the chat field resumed the word.
What remains
Some fields never report a caret at all. In those, the first word typed after the field gains focus can lose its Vietnamese change on the first letter (dd stays “dd”), because there is no caret to check the handoff against. Later words in the same field use marked text and work normally. I chose this over the alternative, which loses the letter completely.
Some manual checks are still open: Numbers, Chrome’s address bar while it shows an inline suggestion, and fast typing such as vieetj or ddi in VS Code and Chrome, which exercises the broader Direct-mode rule. The occasional failures in other apps have not been traced individually, so I cannot yet say this fix covers all of them.
What I took from it
- Measure before fixing. The plausible theory was wrong. A log that prints only numbers was enough to see which client got each key.
- Swallowing a key is a responsibility. When an input method is not sure where text is going, the safest default is what the system keyboard does: let the app have the key.
- Know how wide each change is. In Marked mode the fix touches one key in one situation. In Direct mode it changes every word’s first letter, so that part needs the broadest testing.
- Check the document, not your memory. The handoff is used only when the letter is actually in front of the caret.
Conclusion
When a letter vanishes, find out which text client received the key before changing how keys are inserted. In Goxvi’s case, the key went to a client that lived for 5 milliseconds. Handing an unchanged first letter back to the app, as ABC does, and letting the next field pick the word up again fixed both the missing letter and the Vietnamese change that depended on it.