Advertisement
Guest User

Untitled

a guest
Jan 21st, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1.  
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import org.jnativehook.GlobalScreen;
  7. import org.jnativehook.NativeHookException;
  8. import org.jnativehook.keyboard.NativeKeyEvent;
  9. import org.jnativehook.keyboard.NativeKeyListener;
  10. import org.jnativehook.mouse.NativeMouseEvent;
  11. import org.jnativehook.mouse.NativeMouseInputListener;
  12. import org.jnativehook.mouse.NativeMouseWheelEvent;
  13. import org.jnativehook.mouse.NativeMouseWheelListener;
  14.  
  15. public class Listener implements NativeKeyListener, NativeMouseInputListener, NativeMouseWheelListener  
  16. {
  17.     private String macroPathFile;
  18.     private String pauseKey;
  19.    
  20.    
  21.     public void nativeKeyPressed(NativeKeyEvent e)
  22.     {
  23.         System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
  24.         try {
  25.             writeState("KP:" + NativeKeyEvent.getKeyText(e.getKeyCode()));
  26.         } catch (IOException ex) {
  27.             Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
  28.         }
  29.     }
  30.  
  31.    
  32.     public void nativeKeyReleased(NativeKeyEvent e)
  33.     {
  34.         System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
  35.     }
  36.  
  37.    
  38.     public void nativeKeyTyped(NativeKeyEvent e)
  39.     {
  40.         System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
  41.     }
  42.    
  43.    
  44.     public void nativeMouseWheelMoved(NativeMouseWheelEvent e)
  45.     {
  46.         System.out.println("Mouse Wheel Moved: " + e.getWheelRotation());
  47.     }
  48.    
  49.    
  50.     public void nativeMouseClicked(NativeMouseEvent e)
  51.     {
  52.         System.out.println("Mouse Clicked: " + e.getClickCount());
  53.     }
  54.  
  55.    
  56.     public void nativeMousePressed(NativeMouseEvent e)
  57.     {
  58.         System.out.println("Mouse Pressed: " + e.getButton());
  59.     }
  60.  
  61.    
  62.     public void nativeMouseReleased(NativeMouseEvent e)
  63.     {
  64.         System.out.println("Mouse Released: " + e.getButton());
  65.     }
  66.  
  67.    
  68.     public void nativeMouseMoved(NativeMouseEvent e)
  69.     {
  70.         System.out.println("Mouse Moved: " + e.getX() + ", " + e.getY());
  71.     }
  72.  
  73.    
  74.     public void nativeMouseDragged(NativeMouseEvent e)
  75.     {
  76.         System.out.println("Mouse Dragged: " + e.getX() + ", " + e.getY());
  77.        
  78.     }
  79.    
  80.     public void setMacroAttribut(String macroPathFile, String pauseKey)
  81.     {
  82.         this.macroPathFile = macroPathFile;
  83.         this.pauseKey = pauseKey;
  84.     }
  85.    
  86.     public  void writeState(String text) throws IOException
  87.     {
  88.         FileWriter file = new FileWriter(macroPathFile, true);
  89.         file.write("\n");
  90.         file.append(text + "\n\r");
  91.         file.close();
  92.     }
  93.  
  94.  
  95.     public void start() throws NativeHookException
  96.     {
  97.         GlobalScreen.registerNativeHook();
  98.        
  99.         // Construct the example object.
  100.         Listener run = new Listener();
  101.  
  102.         // Add the appropriate listeners.
  103.         GlobalScreen.addNativeMouseListener(run);
  104.         GlobalScreen.addNativeMouseMotionListener(run);
  105.         GlobalScreen.addNativeMouseWheelListener(run);
  106.         GlobalScreen.addNativeKeyListener(run);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement