Skip to content

[Web] Fix cut-copy-paste #105845

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adamscott
Copy link
Member

@adamscott adamscott commented Apr 27, 2025

Believe it or not, copy-cut-paste on the Web platform seems to be completely broken. (I mean cut/copy/paste from outside to the game or from the game to outside.) Try it out. This PR fixes this.

Capture.video.du.2025-04-27.15-24-28.mp4

The issue is caused by the fact that we use event.preventDefault() for every key input. This makes it impossible for the browser to actually trigger and dispatch the ClipboardEvent needed to read the browser's clipboard. It is only available on transient user activation due to security considerations.

So, I've added exceptions for {Ctrl,Cmd}-{X,C,V} (ctrl/cmd depends on the host). Keyboard-based copy/cut/paste aren't prevented by default, and this fixes the issues with copy/pasting.

So, it will be possible to copy/paste code from and to the browser. Very useful for code editors.

@adamscott adamscott added this to the 4.5 milestone Apr 27, 2025
@adamscott adamscott requested a review from a team as a code owner April 27, 2025 22:03
@adamscott adamscott force-pushed the web-fix-copy-and-paste branch from 75b421f to b2dd7fd Compare April 27, 2025 22:04
@adamscott adamscott changed the title [Web] Fix copy-cut-paste on the Web platform [Web] Fix copy-cut-paste Apr 27, 2025
@adamscott adamscott force-pushed the web-fix-copy-and-paste branch from b2dd7fd to 4f2e3c2 Compare April 27, 2025 22:04
@adamscott adamscott changed the title [Web] Fix copy-cut-paste [Web] Fix cut-copy-paste Apr 27, 2025
Comment on lines +521 to +532
switch (event.key) {
// Preventing default for clipboard shortcuts prevent
// the Clipboard API to actually being triggered.
// This makes it impossible to use {Ctrl,Cmd}-{X,C,V} keyboard shortcuts.
case 'x':
case 'c':
case 'v':
return false;
default:
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The pressing of the Shift key may change the key of the event too. For example, pressing B generates key: "b", while simultaneously pressing Shift generates key: "B".

From https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/shiftKey and it seems relevant.

What about other shortcuts like Ctrl+Insert and Shift+Insert (idk if they are available everywhere but I can use them on Linux)?

@adamscott adamscott force-pushed the web-fix-copy-and-paste branch from 4f2e3c2 to 57c5bbb Compare April 28, 2025 19:46
@adamscott adamscott force-pushed the web-fix-copy-and-paste branch from 57c5bbb to 3cc99a0 Compare April 28, 2025 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants