SHARE
TWEET

Untitled

a guest Jun 15th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.event.KeyEvent;
  2.  
  3. public class HotkeyManager extends Thread {
  4.     public static void register() {
  5.         User32.RegisterHotKey(null, 1, 0x000, KeyEvent.VK_F);
  6.         new HotkeyManager().start();
  7.     }
  8.  
  9.     public HotkeyManager() {
  10.  
  11.     }
  12.  
  13.     @Override
  14.     public void run() {
  15.         MSG msg = new MSG();
  16.         while (true) {
  17.               while (User32.PeekMessage(msg, null, 0, 0, User32.PM_REMOVE)) {
  18.                    if (msg.message == User32.WM_HOTKEY) {
  19.                        System.out.println("Hotkey pressed with id: " + msg.wParam);
  20.                    }
  21.               }
  22.  
  23.               try {
  24.                 Thread.sleep(300);
  25.             } catch (InterruptedException e) {
  26.                 // TODO Auto-generated catch block
  27.                 e.printStackTrace();
  28.             }
  29.         }
  30.     }
  31. }
  32.      
  33. import com.sun.jna.Native;
  34. import com.sun.jna.NativeLibrary;
  35. import com.sun.jna.Pointer;
  36. import com.sun.jna.win32.W32APIOptions;
  37.  
  38. public class User32 {
  39.     static {
  40.         Native.register(NativeLibrary.getInstance("user32", W32APIOptions.DEFAULT_OPTIONS));
  41.     }
  42.  
  43.     public static final int MOD_ALT = 0x0001;
  44.     public static final int MOD_CONTROL = 0x0002;
  45.     public static final int MOD_SHIFT = 0x0004;
  46.     public static final int MOD_WIN = 0x0008;
  47.     public static final int WM_HOTKEY = 0x0312;
  48.     public static final int PM_REMOVE = 0x0001;
  49.  
  50.     public static native boolean RegisterHotKey(Pointer hWnd, int id, int fsModifiers, int vk);
  51.     public static native boolean UnregisterHotKey(Pointer hWnd, int id);
  52.     public static native boolean PeekMessage(MSG lpMsg, Pointer hWnd, int wMsgFilterMin, int wMsgFilterMax, int wRemoveMsg);
  53.  
  54. }
  55.      
  56. import java.util.Arrays;
  57. import java.util.List;
  58.  
  59. import com.sun.jna.Pointer;
  60. import com.sun.jna.Structure;
  61.  
  62. public class MSG extends Structure {
  63.     public Pointer hWnd;
  64.     public int lParam;
  65.     public int message;
  66.     public int time;
  67.     public int wParam;
  68.     public int x;
  69.     public int y;
  70.  
  71.     @Override
  72.     protected List getFieldOrder() {
  73.         return Arrays.asList(new String[]{"hWnd", "lParam", "message", "time", "wParam", "x", "y"});
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top