public class User32 {
static {
Native . register ( NativeLibrary. getInstance ( "user32" , W32APIOptions. DEFAULT_OPTIONS ) ) ;
}
public static final int MOD_ALT = 0x0001 ;
public static final int MOD_CONTROL = 0x0002 ;
public static final int MOD_SHIFT = 0x0004 ;
public static final int MOD_WIN = 0x0008 ;
public static final int WM_HOTKEY = 0x0312 ;
public static native boolean RegisterHotKey ( Pointer hWnd, int id, int fsModifiers, int vk ) ;
public static native boolean UnregisterHotKey ( Pointer hWnd, int id ) ;
public static native boolean PeekMessage ( MSG lpMsg, Pointer hWnd, int wMsgFilterMin, int wMsgFilterMax, int wRemoveMsg ) ;
public static class MSG extends Structure {
public Pointer hWnd ;
public int message ;
public int wParam ;
public int lParam ;
public int time ;
public int x ;
public int y ;
}
}
// register the combination WIN + F
RegisterHotKey ( null , 1 , 0x8, KeyEvent . VK_F ) ;
MSG msg = new MSG ( ) ;
while ( listen ) {
while ( PeekMessage ( msg, null , 0 , 0 , PM_REMOVE ) ) {
if ( msg. message == WM_HOTKEY ) {
System . out . println ( "Yattaaaa. Hotkey with id" + msg. wParam ) ;
}
}
Thread . sleep ( 300 ) ;
}
public interface X11 extends Library {
public static X11 Lib = ( X11 ) Native . loadLibrary ( "X11" , X11. class ) ;
public static final int GrabModeAsync = 1 ;
public static final int KeyPress = 2 ;
public static final int ShiftMask = ( 1 ) ;
public static final int LockMask = ( 1 << 1 ) ;
public static final int ControlMask = ( 1 << 2 ) ;
public static final int Mod1Mask = ( 1 << 3 ) ;
public static final int Mod2Mask = ( 1 << 4 ) ;
public static final int Mod3Mask = ( 1 << 5 ) ;
public static final int Mod4Mask = ( 1 << 6 ) ;
public static final int Mod5Mask = ( 1 << 7 ) ;
public Pointer XOpenDisplay ( String name ) ;
public NativeLong XDefaultRootWindow ( Pointer display ) ;
public byte XKeysymToKeycode ( Pointer display, long keysym ) ;
public int XGrabKey ( Pointer display, int code, int modifiers, NativeLong root, int ownerEvents, int pointerMode, int keyBoardMode ) ;
public int XUngrabKey ( Pointer display, int code, int modifiers, NativeLong root ) ;
public int XNextEvent ( Pointer display, XEvent event ) ;
public int XPending ( Pointer display ) ;
public int XCloseDisplay ( Pointer display ) ;
public static class XEvent extends Union {
public int type ;
public XKeyEvent xkey ;
public NativeLong [ ] pad = new NativeLong [ 24 ] ;
}
public static class XKeyEvent extends Structure {
public int type ; // of event
public NativeLong serial ; // # of last request processed by server
public int send_event ; // true if this came from a SendEvent request
public Pointer display ; // public display the event was read from
public NativeLong window ; // "event" window it is reported relative to
public NativeLong root ; // root window that the event occurred on
public NativeLong subwindow ; // child window
public NativeLong time ; // milliseconds
public int x, y ; // pointer x, y coordinates in event window
public int x_root, y_root ; // coordinates relative to root
public int state ; // key or button mask
public int keycode ; // detail
public int same_screen ; // same screen flag
}
}
// for letters and numbers, the symbol number also corresponds to the value in KeyEvent
byte code = XKeysymToKeycode ( display, KeyEvent . VK_F ) ;
// this hack is found in the global hotkey plugin in the deadbeef player
for ( int flags = 0 ; flags < 16 ; i ++ ) {
int ret = modifiers ;
if ( ( flags & 1 ) ! = 0 )
ret | = LockMask ;
if ( ( flags & 2 ) ! = 0 )
ret | = Mod2Mask ;
if ( ( flags & 4 ) ! = 0 )
ret | = Mod3Mask ;
if ( ( flags & 8 ) ! = 0 )
ret | = Mod5Mask ;
XGrabKey ( display, code, ret, root, 1 , GrabModeAsync, GrabModeAsync ) ;
// XUngrabKey (display, code, ret, root);
}
while ( listening ) {
while ( Lib. XPending ( display ) > 0 ) {
Lib. XNextEvent ( display, event ) ;
if ( event. type == KeyPress ) {
// read our event from union XEvent
// JNA does not know which field to fill in the union,
// so he needs to tell which of the fields to count.
XKeyEvent xkey = ( XKeyEvent ) event. readField ( "xkey" ) ;
// clear garbage modifiers
int state = xkey. state & ( ShiftMask | ControlMask | Mod1Mask | Mod4Mask ) ;
System . out . println ( "Yattaaaa, hotkey with code:" + xkey. keycode + "and modifiers:" + state ) ;
}
}
Thread . sleep ( 300 ) ;
}
public interface Carbon extends Library {
public static Carbon Lib = ( Carbon ) Native . loadLibrary ( "Carbon" , Carbon. class ) ;
public static final int cmdKey = 0x0100 ;
public static final int shiftKey = 0x0200 ;
public static final int optionKey = 0x0800 ;
public static final int controlKey = 0x1000 ;
// OS_TYPE concatenates string characters into int
private static final int kEventClassKeyboard = OS_TYPE ( "keyb" ) ;
private static final int typeEventHotKeyID = OS_TYPE ( "hkid" ) ;
private static final int kEventParamDirectObject = OS_TYPE ( "----" ) ;
public Pointer GetEventDispatcherTarget ( ) ;
public int InstallEventHandler ( Pointer inTarget, EventHandlerProcPtr inHandler, int inNumTypes, EventTypeSpec [ ] inList, Pointer inUserData, PointerByReference outRef ) ;
public int RegisterEventHotKey ( int inHotKeyCode, int inHotKeyModifiers, EventHotKeyID. ByValue inHotKeyID, Pointer inTarget, int inOptions, PointerByReference outRef ) ;
public int GetEventParameter ( Pointer inEvent, int inName, int inDesiredType, Pointer outActualType, int inBufferSize, IntBuffer outActualSize, EventHotKeyID outData ) ;
public int RemoveEventHandler ( Pointer inHandlerRef ) ;
public int UnregisterEventHotKey ( Pointer inHotKey ) ;
public class EventTypeSpec extends Structure {
public int eventClass ;
public int eventKind ;
}
public static class EventHotKeyID extends Structure {
public int signature ;
public int id ;
public static class ByValue extends EventHotKeyID implements Structure. ByValue {
}
}
public static interface EventHandlerProcPtr extends Callback {
public int callback ( Pointer inHandlerCallRef, Pointer inEvent, Pointer inUserData ) ;
}
}
eventHandlerReference = new PointerByReference ( ) ;
// actually the handler itself
keyListener = new EventHandler ( ) ;
// JNA magic to create an array from one structure
EventTypeSpec [ ] eventTypes = ( EventTypeSpec [ ] ) ( new EventTypeSpec ( ) . ToArray ( 1 ) ) ;
eventTypes [ 0 ] . eventClass = kEventClassKeyboard ;
eventTypes [ 0 ] . eventKind = kEventHotKeyPressed ;
int status = lib. InstallEventHandler ( Lib. GetEventDispatcherTarget ( ) , keyListener, 1 , eventTypes, null , eventHandlerReference ) ;
private class EventHandler implements Carbon. EventHandlerProcPtr {
public int callback ( Pointer inHandlerCallRef, Pointer inEvent, Pointer inUserData ) {
EventHotKeyID eventHotKeyID = new EventHotKeyID ( ) ;
// get event parameters
int ret = lib. GetEventParameter ( inEvent, kEventParamDirectObject, typeEventHotKeyID, null , eventHotKeyID. Size ( ) , null , eventHotKeyID ) ;
if ( ret ! = 0 ) {
logger. warning ( "Could not get event parameters. Error code:" + ret ) ;
} else {
// Get and handle event id here
int eventId = eventHotKeyID. id ;
logger. info ( "Received event id:" + eventId ) ;
}
return 0 ;
}
}
EventHotKeyID. ByValue hotKeyReference = new EventHotKeyID. ByValue ( ) ;
hotKeyReference. id = 1 ;
hotKeyReference. signature = OS_TYPE ( "hk01" ) ;
PointerByReference gMyHotKeyRef = new PointerByReference ( ) ;
int status = lib. RegisterEventHotKey ( code, modifiers, hotKeyReference, Lib. GetEventDispatcherTarget ( ) , 0 , gMyHotKeyRef ) ;
Source: https://habr.com/ru/post/124567/
All Articles