【发布时间】:2016-04-29 14:41:54
【问题描述】:
我在 LibGDX 中开发游戏,游戏中有登录界面和注册界面。 HTML版本的游戏有剪贴板的沙盒环境,意思是:
从游戏中复制的任何内容,不能粘贴到游戏外& 从外部复制的任何内容都不能粘贴到游戏的文本字段中
我只是想复制文本,有什么方法可以将沙盒剪贴板与系统剪贴板合并?
我想要的是: 当用户在文本字段中执行 Ctr+V 时,它应该从文本字段中的系统剪贴板中抓取文本,当用户按下 Ctr+C 时:它应该将文本放入系统剪贴板中
我正在尝试什么:
public class HtmlLauncher extends GwtApplication {
private static HtmlLauncher instance;
public void onModuleLoad() {
instance = this;
setLoadingListener(new LoadingListener() {
@Override
public void beforeSetup() {}
@Override
public void afterSetup() {
setupCopyListener();
}
});
}
native void setupCopyListener()
/*-{
var htmlLauncher_onCopy = $entry(@com.myapp.game.client.HtmlLauncher::addToClipboard());
$wnd.addEventListener("copy", htmlLauncher_onCopy, false);
}-*/
;
public static void addToClipboard() {
instance.copy();
}
private void copy() {
//getClipboard().setContents("");
consoleLog("copied");
}
}
谁能帮帮我:
- 如何将参数抓取到事件(抓取复制的文本)
- 只有在 DOM 中发生复制事件时才会触发,我如何获取系统剪贴板
编辑(5 月 2 日,尝试过 JustACluelessNewbie 的建议): 继承剪贴板:
public class MyClipboard implements com.badlogic.gdx.utils.Clipboard{
private String cachedContent = "";
public MyClipboard() {
createTextArea();
}
@Override
public String getContents() {
String contents = getClipBoard();
return (contents == null) ? cachedContent : cachedContent = contents;
}
@Override
public void setContents(String content) {
cachedContent = content == null ? "" : content;
setClipBoard(content);
}
public static native void createTextArea() /*-{
var textArea = document.createElement('textarea');
textArea.style.position='fixed';
textArea.style.top=0;
textArea.style.left=0;
textArea.style.width='2em';
textArea.style.height='2em';
textArea.style.padding=0;
textArea.style.border='none';
textArea.style.outline='none';
textArea.style.boxShadow='none';
textArea.style.background='transparent';
$wnd._copy=textArea;
}-*/;
public static native String getClipBoard() /*-{
if(window.clipboardData){
return window.clipboardData.getData('Text');
}else {
document.body.appendChild($wnd._copy);
try{
$wnd._copy.value = "";
$wnd._copy.focus();
$wnd._copy.select();
console.log(document.queryCommandSupported("paste")); //prints true
var result = document.execCommand('paste');
console.log(result); //prints false
return $wnd._copy.value;
}catch(err){
return null;
}finally{
document.body.removeChild($wnd._copy);
}
}
}-*/;
public static native void setClipBoard(String content) /*-{
document.body.appendChild($wnd._copy);
try{
$wnd._copy.value = content;
$wnd._copy.select();
var result = document.execCommand('copy');
console.log("after exec copy "+result)
}catch(err){
console.log("error:"+err);
}finally{
document.body.removeChild($wnd._copy);
}
}-*/ ;
}
- 我在最新的 chrome 上运行,它说它支持粘贴命令,但是 不粘贴
- 它被复制到系统剪贴板,我可以在剪贴板中看到,但它 按 Ctr+V 或右键粘贴时不粘贴
【问题讨论】:
标签: javascript gwt libgdx clipboard