在Node.js中复制到剪贴板? [英] Copy to clipboard in Node.js?
问题描述
有没有办法可以在Node.js中复制到剪贴板?任何模块或想法是什么?我在桌面应用程序上使用Node.js.希望能够清除为什么我希望它能够实现这一点。
Is there a way you can copy to clipboard in Node.js? Any modules or ideas what so ever? I'm using Node.js on a desktop application. Hopefully that clears up why I want it to be able to achieve this.
推荐答案
剪贴板不是操作系统所固有的。它是操作系统正在运行的任何窗口系统的构造。因此,如果您希望这在X上工作,则需要绑定到Xlib和/或XCB。节点的Xlib绑定实际存在: https://github.com/mixu/nwm 。虽然我不确定它是否允许您访问X剪贴板,但您最终可能会编写自己的剪贴板。你需要单独的窗口绑定。
A clipboard is not inherent to an operating system. It's a construct of whatever window system the operating system happens to be running. So if you wanted this to work on X for example, you would need bindings to Xlib and/or XCB. Xlib bindings for node actually exist: https://github.com/mixu/nwm. Although I'm not sure whether it gives you access to the X clipboard, you might end up writing your own. You'll need separate bindings for windows.
编辑:如果你想做一些hacky,你也可以使用xclip:
edit: If you want to do something hacky, you could also use xclip:
var exec = require('child_process').exec;
var getClipboard = function(func) {
exec('/usr/bin/xclip -o -selection clipboard', function(err, stdout, stderr) {
if (err || stderr) return func(err || new Error(stderr));
func(null, stdout);
});
};
getClipboard(function(err, text) {
if (err) throw err;
console.log(text);
});
这篇关于在Node.js中复制到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!