java监听剪贴板_用java实现windows剪贴板监视

import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.FlavorEvent; import java.awt.datatransfer.FlavorListener; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ClipboardMonitor implements FlavorListener { public Display display ; public Shell shell ; public Clipboard systemClipboard = null ; public Text text = null ; public StringBuffer buffer ; public void flavorsChanged(FlavorEvent e){ try { Clipboard clipboard = (Clipboard)e.getSource() ; Transferable content = clipboard.getContents(this); if (content == null) { System.out.println("Content is empty!"); return; } if (!content.isDataFlavorSupported(DataFlavor.stringFlavor)) { System.out.println("StringFlavor not supported!"); return; } try { final String data = (String)content.getTransferData(DataFlavor.stringFlavor); // System.out.println("Content: /"" + data + "/""); display.asyncExec(new Runnable(){ public void run() { text.append(data) ; } }) ; } catch (UnsupportedFlavorException ex_) { ex_.printStackTrace(); } catch (IOException ex_) { ex_.printStackTrace(); } } catch (IllegalStateException ex) { ex.printStackTrace(); } } public void init() { buffer = new StringBuffer() ; systemClipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); display = new Display() ; shell = new Shell(display , SWT.ON_TOP | SWT.TITLE | SWT.MAX | SWT.CLOSE | SWT.MIN) ; shell.setText("ClipBoard Monitor") ; text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL); text.setSize(500 , 300) ; GridData data = new GridData(GridData.FILL_BOTH); text.setLayoutData(data); shell.setSize(800, 600) ; shell.pack() ; systemClipboard.addFlavorListener(this) ; // display.asyncExec(this) ; // Display.getDefault().syncExec(this) ; shell.open() ; shell.forceFocus() ; while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ClipboardMonitor instance = new ClipboardMonitor() ; instance.init() ; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值