Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(modifier key): reset modifier keys when browser tab loses focus/is hidden #759

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/tools/src/eventListeners/keyboard/keyDownListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,23 @@ function keyListener(evt: KeyboardEvent): void {
triggerEvent(eventDetail.element, Events.KEY_DOWN, eventDetail);

document.addEventListener('keyup', _onKeyUp);
document.addEventListener('visibilitychange', _onVisibilityChange);

// Todo: handle combination of keys
state.element.removeEventListener('keydown', keyListener);
}

/**
* Whenever the visibility (i.e. tab focus) changes such that the tab is NOT the
* active tab, reset the modifier key.
*/
function _onVisibilityChange(): void {
document.removeEventListener('visibilitychange', _onVisibilityChange);
if (document.visibilityState === 'hidden') {
resetModifierKey();
}
}

function _onKeyUp(evt: KeyboardEvent): void {
const eventDetail: KeyUpEventDetail = {
renderingEngineId: state.renderingEngineId,
Expand All @@ -81,6 +93,7 @@ function _onKeyUp(evt: KeyboardEvent): void {

// Remove our temporary handlers
document.removeEventListener('keyup', _onKeyUp);
document.removeEventListener('visibilitychange', _onVisibilityChange);
state.element.addEventListener('keydown', keyListener);

// Restore `state` to `defaultState`
Expand Down