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:
- Make a scene with a
LineEdit. - Export it to HTML5
- Run it in a browser (I use
php -Swhen I’m in a hurry or too lazy, to avoidfile://issues) - Copy text from any source but the game (eg: the URL bar of the browser)
- 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)
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!
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).
For browser that supports the Clipboard API
readTextfunction (chrome) yes. Just callOS.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 toOS::set_clipboard, passing in the data of the actual operating system’s clipboard, overriding Godot’s internal clipboard. This needs to happen before_gui_inputis called.The actual operating system’s clipboard can be read from the
pasteevent, 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_PASTEin the fashion ofmouseleaveand others ? Since such notifications don’t seem to be able to transport parameters, I’m not sure.So far, I’ve got :
pasteeventMany of the comments in
OS_JavaScript, like that one, are remnants fromOS_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
keydownevent, but we only get the that data in thepasteevent, which occurs after thekeydownevent. So it has to be hacked. Selection API doesn’t apply to us because we render text manually using FreeType + WebGL