Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 3rd, 2013  |  syntax: None  |  size: 2.14 KB  |  views: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.Toolkit;
  2. import java.awt.datatransfer.Clipboard;
  3. import java.awt.datatransfer.Transferable;
  4. import java.util.Stack;
  5.  
  6. import org.jnativehook.GlobalScreen;
  7. import org.jnativehook.NativeHookException;
  8. import org.jnativehook.NativeInputEvent;
  9. import org.jnativehook.keyboard.NativeKeyEvent;
  10. import org.jnativehook.keyboard.NativeKeyListener;
  11.  
  12. public class Test3 implements NativeKeyListener {
  13.  
  14. Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
  15. Stack<Transferable> clipStack = new Stack<>();
  16.  
  17. public static void main(String[] args) {
  18.     try {
  19.         GlobalScreen.registerNativeHook();
  20.     } catch (NativeHookException ex) {
  21.         System.err
  22.                 .println("There was a problem registering the native hook.");
  23.         System.err.println(ex.getMessage());
  24.  
  25.         System.exit(1);
  26.     }
  27.     Test2 t2 = new Test2();
  28.     // Construct the example object and initialze native hook.
  29.     GlobalScreen.getInstance().addNativeKeyListener(t2);
  30. }
  31.  
  32. @Override
  33. public void nativeKeyPressed(NativeKeyEvent ev) {
  34.     // Copy
  35.     if (ev.getKeyCode() == NativeKeyEvent.VK_C
  36.             && NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
  37.                     "Ctrl")) {
  38.         // Clip the pop
  39.         try {
  40.             Thread.sleep(100);
  41.         } catch (InterruptedException e) {
  42.             e.printStackTrace();
  43.         }
  44.         clipStack.push(sysClip.getContents(null));
  45.  
  46.         System.out.println("Ctrl+C : Stack(" + clipStack.size() + ")");
  47.     }
  48.     // Paste
  49.     if (ev.getKeyCode() == NativeKeyEvent.VK_V
  50.             && NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
  51.                     "Ctrl")) {
  52.         // Clip the pop
  53.         try {
  54.             Thread.sleep(100);
  55.         } catch (InterruptedException e) {
  56.             e.printStackTrace();
  57.         }
  58.         if (clipStack.size() > 1) {
  59.             sysClip.setContents(clipStack.pop(), null);
  60.         } else
  61.             sysClip.setContents(clipStack.peek(), null);
  62.  
  63.         System.out.println("Ctrl+V : Stack(" + clipStack.size() + ")");
  64.     }
  65. }
  66.  
  67. @Override
  68. public void nativeKeyReleased(NativeKeyEvent e) {
  69.  
  70. }
  71.  
  72. @Override
  73. public void nativeKeyTyped(NativeKeyEvent e) {
  74.  
  75. }
  76.  
  77. }
clone this paste RAW Paste Data