node-webkit js 复制粘贴

来源:互联网 发布:淘宝网婴幼儿童车 编辑:程序博客网 时间:2024/05/29 21:30

在mac环境的nw中,command+c/v无法使用(windows环境下ctrl+c/v是正常的),为了能复制粘贴,有了下面的解决方案

document.onkeydown = function(event){    if (event.ctrlKey && event.keyCode == 67) {        document.execCommand('copy');    }    else if (event.ctrlKey && event.keyCode == 86) {        if(document.execCommand('paste')){            return false;        }    }}

这里用ctrl+c/v键统一了window/mac下的复制粘贴

document.execCommand('copy');//copy命令可以使用document.execCommand('paste');//paste命令windows/mac nw环境均有效,windows的chrome环境无效(没测mac的chrome环境)if(document.execCommand('paste')){//这个是兼容处理    return false;}

因为document.execCommand()在执行支持的命令时返回true,不支持时返回false;
因此把命令写在if内,如果支持,就return false掉默认的(否则windows nw下会粘贴两次);如果不支持,就执行默认动作(windows chrome);

如果是在iframe中,只需要把代码中的document替换为iframe的document对象即可

//此处ue.document为文本编辑器ueditor生成的编辑区域iframe的document对象ue.document.onkeydown = function(event){    if (event.ctrlKey && event.keyCode == 67) {        ue.document.execCommand('copy');    }    else if (event.ctrlKey && event.keyCode == 86) {        if(ue.document.execCommand('paste')){            return false;        }    }}
0 0