godot: Game clipboard is not shared with system clipboard when running HTML5 exports

Operating system or device, Godot version, GPU Model and driver (if graphics related):

  • Debian and Ubuntu, on Chromium and Firefox.
  • Godot 2.1.4 stable
  • HTML5 exports only (work with regular binaries)

Issue description:

System clipboard contents cannot be pasted in controls such as LineEdit. The game has its own clipboard contents and it is not shared with the system.

OS.get_clipboard() stays empty unless we copy text from the game itself.

This is confusing for users because HTML input fields do share their clipboard with the system.

Steps to reproduce:

  1. Make a scene with a LineEdit.
  2. Export it to HTML5
  3. Run it in a browser (I use php -S when I’m in a hurry or too lazy, to avoid file:// issues)
  4. Copy text from any source but the game (eg: the URL bar of the browser)
  5. Try to paste it in the LineEdit.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 23 (13 by maintainers)

Most upvoted comments

For future people who click on this issue and don’t need a LOT of clipboard access, here is my workaround that I’ll be using for my own Godot HTML5 games using the ‘prompt’ function: reddit.com/r/godot/comments/17rsemr/how_to_access_the_clipboard_as_a_html5web_godot

I hope it helps someone out there!

This is a pretty clever solution 🙂 It could be interesting to expose as an export preset option (something like Emulate Clipboard).

For future people who click on this issue and don’t need a LOT of clipboard access, here is my workaround that I’ll be using for my own Godot HTML5 games using the ‘prompt’ function: https://www.reddit.com/r/godot/comments/17rsemr/how_to_access_the_clipboard_as_a_html5web_godot/

I hope it helps someone out there!

I have been trying to detect the pressing of CTRL + V with an input event and then read from the javascript clipboard but have not had luck. Let me know if this is actually impossible so I can stop trying lmao.

Not really, due to the order in which DOM events are fired, we need to handle the CTRL (or COMMAND on Mac) and V keys separately, and prevent their default (to avoid browsers using those input in other ways), and the paste event is sent after the CTRL and V, and only if those keys are not being prevented their default behaviour. Your best bet, is to tell the users to use the “Edit -> Paste” menu entry in their browser. That should always work (though I agree is really bad UX).

do you think it’s possible to trigger a copy or paste event using an in-engine GUI button

For browser that supports the Clipboard API readText function (chrome) yes. Just call OS.get_clipboard() during the pressed signal of your button.

There’s no need to implement copy/paste, this is implemented per Control class in _gui_input. What needs to happen is a call to OS::set_clipboard, passing in the data of the actual operating system’s clipboard, overriding Godot’s internal clipboard. This needs to happen before _gui_input is called.

The actual operating system’s clipboard can be read from the paste event, but it occurs too late. Additionally, that event is only fired when hitting Ctrl+V, so it can’t work for right click → Paste either.

document.execCommand('paste') might work, but is only available to WebExtensions.

The new Async Clipboard API might work, but isn’t available yet.

Thanks so much for the invaluable help, @eska014 !


Would it be appropriate to trigger a MainLoop::NOTIFICATION_WM_PASTE in the fashion of mouseleave and others ? Since such notifications don’t seem to be able to transport parameters, I’m not sure.

So far, I’ve got :

  1. Listen to the paste event
  2. In that listener, get the pasted string
  3. Store that pasted string somewhere
  4. Trigger an internal event (notification?)
  5. Hack something around here maybe
  6. ???
  7. Publish!

Many of the comments in OS_JavaScript, like that one, are remnants from OS_Android, from which that implementation was copied as a base.

The issue with clipboard sync is pasting. Godot needs the OS clipboard data during the keydown event, but we only get the that data in the paste event, which occurs after the keydown event. So it has to be hacked. Selection API doesn’t apply to us because we render text manually using FreeType + WebGL