System-level hotkeys are a set of shortcut keys. No matter which program the current system focus is in, the program can capture and process the event by pressing the key. This function is very useful in applications, such as
"Win + L" automatically locks the screen, and the default "ctrl + alt + Z" in QQ automatically opens the current message window.
Although the event listening mechanism in Java is powerful, it is powerless when the system focus is out of the program.
The bottom layer of Java is jvm, so java alone cannot perceive the situation of jvm and the next operating system. Therefore, java alone cannot implement this function.
To implement this function, you must call the system hook function. Therefore, you must call jni in java. However, it is difficult for friends who are not familiar with system functions or other programming languages.
Previously, similar functions were implemented by calling the system's hook functions through c and then calling them through jni. Anything you write can meet simple requirements, therefore, the function and program structure are relatively simple. Later, I found a component "jintelliitype" on a foreign website to help us encapsulate the vast majority of functions, and also adopted the event listening mechanism in java in the structure, we only need to register it in the program. The following is a simple example:
Java code
Import com. melloware. jintelliitype. HotkeyListener;
Import com. melloware. jintelliitype. jintelliitype;
Public class HotKey implements HotkeyListener {
Static final int KEY_1 = 88;
Static final int KEY_2 = 89;
Static final int KEY_3 = 90;
/**
* This method monitors registered system hotkey events.
*
* @ Param key: The identifier of the triggered hot key.
*/
Public void onHotKey (int key ){
Switch (key ){
Case KEY_1:
System. out. println ("ctrl + alt + I press .........");
Break;
Case KEY_2:
System. out. println ("ctrl + alt + O press .........");
Break;
Case KEY_3:
System. out. println ("the System exits ..........");
Destroy ();
}
}
/**
* Cancel registration and exit
*/
Void destroy (){
Jintelliitype. getInstance (). unregisterHotKey (KEY_1 );
Jintelliitype. getInstance (). unregisterHotKey (KEY_2 );
Jintelliitype. getInstance (). unregisterHotKey (KEY_3 );
System. exit (0 );
}
/**
* Initialize the hotkey and register the listening event
*/
Void initHotkey (){
// The KEY_1 parameter indicates the identifier of the key combination, and the second parameter indicates the key combination. If not, it is 0. The key corresponds to ctrl + alt + I
Jintelliitype. getInstance (). registerHotKey (KEY_1, jintelliitype. MOD_CONTROL + jintelliitype. MOD_ALT,
(Int) 'I ');
Jintelliitype. getInstance (). registerHotKey (KEY_2, jintelliitype. MOD_CONTROL + jintelliitype. MOD_ALT,
(Int) 'O ');
Jintelliitype. getInstance (). registerHotKey (KEY_3, jintelliitype. MOD_CONTROL + jintelliitype. MOD_ALT,
(Int) 'X ');
Jintelliitype. getInstance (). addHotKeyListener (this );
}
Public static void main (String [] args ){
HotKey key = new HotKey ();
Key. initHotkey ();
// The following describes the tasks that have been executed for a long time.
While (true ){
Try {
Thread. sleep (10000 );
} Catch (Exception ex ){
Break;
}
}
}
}
It introduces a component that has encapsulated most of the functions for us: jintelliitype, and also uses the event listening mechanism in java in the structure. We only need to register it in the program. Easy to use.