Skip to content

Fix/stale key state - #1

Merged
MichaelBuessemeyer merged 4 commits into
mainfrom
fix/stale-key-state
Jul 31, 2026
Merged

Fix/stale key state#1
MichaelBuessemeyer merged 4 commits into
mainfrom
fix/stale-key-state

Conversation

@MichaelBuessemeyer

@MichaelBuessemeyer MichaelBuessemeyer commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

This PR fixes some problems with the keystrokes library.

Bugs fixed:

  • Phantom /Stale keys:
    To reproduce:
  1. open annotation
  2. create a tree with some nodes
  3. test "1" keyboard shortcut -> should toggle tree visibility
  4. edit the tree name, while doing that, use a key combo in the name that first uses the plain key, then add shift to it, release the initial key and then shift (e.g. down ß, down shift, up ß, up shift) -> keystrokes never notices the release of the ß key -> as the event has a modified key  prop due to the newly added shift.
  5. => all single keyboard shortcut no longer work.
  • Combos sequences now time out (do not live forever as before) -> Press ctrl + k, wait 30 sec, press b -> on master should active the brush tool as shortcut ctrl + k, b was triggered. Now this times out and b -> make branch point should trigger. See added tests.
  • exposed function to reset tracked keys state of the lib externally

Changes

The changes are:

  • Phantom keys are fixed by tracking the pressed keys not via the key property of the event which might change while it is pressed due to additionally pressed modifier keys. Keys now have an identifier prop which is code -> The key stable prop of such an event.
  • Some additional protection against capturing browser autofill / IME keystrokes
  • Each sequence or so now has a timeout prop (time when it was triggered -> so it can time out on trigger)
  • Added tests to ensure no regressions and to hopefully reduce iterations until the behaviour is as we need it in WK instead of releasing over and over

Companion PR on WK side: scalableminds/webknossos#9842

@Reviewer I looked at the tests and the dist script. They should be fine. So code + readme is the only thing to look for imo and no testing needed. We need to test with in conjunction with the WK pr

…asing all keys helper function, make combos time out, dont error on key == undefined events
@MichaelBuessemeyer MichaelBuessemeyer self-assigned this Jul 30, 2026
@MichaelBuessemeyer MichaelBuessemeyer added the bug Something isn't working label Jul 30, 2026

@MichaelBuessemeyer MichaelBuessemeyer left a comment

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.

some hints hopefully helping the reviewing process

Comment on lines +86 to +93
// Composition keydowns (IME) and the placeholder keys some autofill
// implementations emit never receive a matching keyup, so tracking them could
// only ever strand a key. Note we never filter keyups: those can only ever
// remove state, and dropping one is what strands a key in the first place.
const cannotBeReleased = (event: KeyboardEvent) =>
event.isComposing === true ||
event.key === 'Process' ||
event.key === 'Unidentified'

@MichaelBuessemeyer MichaelBuessemeyer Jul 30, 2026

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.

guarding IME / browser autofill inputs

Comment on lines +133 to +150
// blur does not cover every way a page can stop receiving key events, and any
// keyup delivered elsewhere in the meantime is lost.
const visibilityHandler = () => {
const doc = getDoc() as Partial<Document>
if (doc.visibilityState === 'hidden') handlerWrapper()
}

addEventListener('blur', handlerWrapper)
return () => removeEventListener('blur', handlerWrapper)
addEventListener('pagehide', handlerWrapper)
// visibilitychange is fired at the document, but it bubbles, so listening on
// the window keeps the document listener order untouched.
addEventListener('visibilitychange', visibilityHandler)

return () => {
removeEventListener('blur', handlerWrapper)
removeEventListener('pagehide', handlerWrapper)
removeEventListener('visibilitychange', visibilityHandler)
}

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.

new clean up handlers to reset recorded keys when page is left / blurred

> = (handler) => {
try {
const handlerWrapper = (e: KeyboardEvent) => {
if (cannotBeReleased(e)) return

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.

guard against autofill / ime events

Comment on lines +76 to +79
export const releaseAllKeys: typeof globalKeystrokes.releaseAllKeys = (
...args
) => getGlobalKeystrokes().releaseAllKeys(...args)

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.

just let the lib expose releaseAllKeys for WK to be able to execute this if useful / needed

@philippotto philippotto left a comment

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.

I'm not really familiar with the code. Only skimmed the diff now. lgtm 👍

@MichaelBuessemeyer
MichaelBuessemeyer merged commit ce9fb90 into main Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants