Eclipse:选择自动复制到剪贴板 [英] Eclipse: selection autocopy to clipboard

查看:288
本文介绍了Eclipse:选择自动复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢Emacs功能,可以自动将选择内容复制到剪贴板。是否可以在Eclipse上执行相同操作?

I love an Emacs feature to copy selection to clipboard automatically. Is it possible to do the same on Eclipse?

环境:Windows XP,Helios

Environment: Windows XP, Helios

推荐答案

要将一个字符串从Eclipse复制到剪贴板,可以使用

To copy a String from Eclipse to the clipboard, you can use

void copyToClipboard (String toClipboard, Display display){
    String toClipboard = "my String";
    Clipboard clipboard = new Clipboard(display);
    TextTransfer [] textTransfer = {TextTransfer.getInstance()};
    clipboard.setContents(new Object [] {toClipboard}, textTransfer);
    clipboard.dispose();
}

然后,您可以从 MouseAdapter KeyAdapter ,具体取决于您要从中获取字符串的位置。在你的情况下,它可以是 MouseAdapter ,它监听双击,获取文本的当前光标位置,标记该字,然后将该字符串添加到剪贴板。

Then you can call this method from a MouseAdapter or KeyAdapter, depending on where you want to get your String from. In your case it could be MouseAdapter, which listens to doubleclicks, gets the current cursor position of the text, marks the word and then adds the String to the clipboard.

编辑回答一个问题:您可以设置自己的 MouseAdapater 并将其附加到您喜欢的按钮,文本字段或whateer 。这是一个按钮的例子:

edit to answer a question: You can set up your own MouseAdapater and attach it to buttons, text fields or whateer you like. Here's an example for a button:

    Button btnGo1 = new Button(parent, SWT.NONE);
    btnGo1.setText("Go");
    btnGo1.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
        //do what you want to do in here
        }
    });

如果要实现mouseUp和mouseDown事件,也可以添加 MouseListener 而不是适配器。适配器的唯一优点是,您不必重写界面的其他方法。

If you want to implement mouseUp and mouseDown events, too, you can just add MouseListenerinstead of the Adapter. The only advantage of the Adapter is, that you don't have to override the other methods of the interface.

由于原来的问题是自动获取编辑器的文本:从编辑器中获取选择的方式说明了这里

Since the original question was to automatically get the selection of the text of an editor: the way to get the selection from an editor is explained here.

这篇关于Eclipse:选择自动复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆