Skip to content
Closed
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
11 changes: 11 additions & 0 deletions src/lib/VncScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ const VncScreen: React.ForwardRefRenderFunction<VncScreenHandle, Props> = (props
connected.current = state;
};

const refocusCanvasOnCaptureElem = () => {
const canvas = document.querySelector('canvas');
Copy link
Owner

Choose a reason for hiding this comment

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

If there's another canvas on the page before the noVNC canvas, this line will select that won't it?

Maybe we could do something like

const canvases = Array.from(document.querySelectorAll('canvas'));
const canvas = canvases.filter(({ id }) => id === 'noVNC_mouse_capture_elem');

By the way, have you tested this approach? It does feel a little hacky, but since a lot of people have been facing it I'm happy to merge it (provided it doesn't break anything of course 😅)

canvas?.addEventListener('mouseleave', (e: MouseEvent) => {
const toElement = e.relatedTarget as HTMLElement | null;
if (toElement && toElement.id === 'noVNC_mouse_capture_elem') {
canvas.focus();
}
});
};

const _onConnect = (e: NoVncEvents['connect']) => {
if (onConnect) {
onConnect(e);
Expand Down Expand Up @@ -235,6 +245,7 @@ const VncScreen: React.ForwardRefRenderFunction<VncScreenHandle, Props> = (props
});

setConnected(true);
refocusCanvasOnCaptureElem();
} catch (err) {
logger.error(err);
}
Expand Down