Changed keyboard hook library for better platform support.
This commit is contained in:
parent
fb3c830b79
commit
b77399dee8
14
pom.xml
14
pom.xml
@ -86,9 +86,9 @@
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>lc.kra.system</groupId>
|
||||
<artifactId>system-hook</artifactId>
|
||||
<version>2.2</version>
|
||||
<groupId>com.1stleg</groupId>
|
||||
<artifactId>jnativehook</artifactId>
|
||||
<version>2.0.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@ -102,14 +102,6 @@
|
||||
<id>jaudiotagger-repository</id>
|
||||
<url>https://dl.bintray.com/ijabz/maven</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>system-hook-mvn-repo</id>
|
||||
<url>https://raw.github.com/kristian/system-hook/mvn-repo/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
@ -1,8 +1,5 @@
|
||||
package musicplayer;
|
||||
|
||||
import lc.kra.system.keyboard.GlobalKeyboardHook;
|
||||
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
|
||||
import lc.kra.system.keyboard.event.GlobalKeyEvent;
|
||||
import musicplayer.callbacks.LibraryCallbackInterface;
|
||||
import musicplayer.callbacks.PlayerCallbackInterface;
|
||||
import musicplayer.db.DatabaseManager;
|
||||
@ -15,6 +12,10 @@ import musicplayer.swingmodels.PlaylistTableModel;
|
||||
import musicplayer.util.Icons;
|
||||
import musicplayer.util.LibraryUtils;
|
||||
import musicplayer.util.PlaylistUtils;
|
||||
import org.jnativehook.GlobalScreen;
|
||||
import org.jnativehook.NativeHookException;
|
||||
import org.jnativehook.keyboard.NativeKeyEvent;
|
||||
import org.jnativehook.keyboard.NativeKeyListener;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.HyperlinkEvent;
|
||||
@ -36,6 +37,8 @@ import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface {
|
||||
@ -68,11 +71,23 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
|
||||
}, "seekbar");
|
||||
seekBarUpdater.start();
|
||||
try {
|
||||
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();
|
||||
keyboardHook.addKeyListener(new GlobalKeyboardShortcuts());
|
||||
Thread hookShutdown = new Thread(keyboardHook::shutdownHook);
|
||||
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
|
||||
logger.setLevel(Level.WARNING);
|
||||
logger.setUseParentHandlers(false);
|
||||
GlobalScreen.registerNativeHook();
|
||||
GlobalScreen.isNativeHookRegistered();
|
||||
GlobalKeyboardShortcuts shortcuts = new GlobalKeyboardShortcuts();
|
||||
GlobalScreen.addNativeKeyListener(shortcuts);
|
||||
Thread hookShutdown = new Thread(() -> {
|
||||
try {
|
||||
GlobalScreen.removeNativeKeyListener(shortcuts);
|
||||
GlobalScreen.unregisterNativeHook();
|
||||
} catch (NativeHookException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
Runtime.getRuntime().addShutdownHook(hookShutdown);
|
||||
} catch(RuntimeException | UnsatisfiedLinkError ex){
|
||||
} catch(RuntimeException | NativeHookException |UnsatisfiedLinkError ex){
|
||||
System.out.println("Keyboard hook failed, global shortcuts will not work this session.");
|
||||
System.out.println(ex.getMessage());
|
||||
System.out.println(Arrays.toString(ex.getStackTrace()));
|
||||
@ -573,20 +588,29 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
|
||||
public void mouseExited(MouseEvent e) {}
|
||||
}
|
||||
|
||||
private class GlobalKeyboardShortcuts extends GlobalKeyAdapter {
|
||||
final int modifierKey = 164;
|
||||
private class GlobalKeyboardShortcuts implements NativeKeyListener {
|
||||
boolean modified = false;
|
||||
final int playPause = 192;
|
||||
final int stop = 222;
|
||||
final int previous = 219;
|
||||
final int next = 221;
|
||||
boolean modified = false;
|
||||
|
||||
@Override
|
||||
public void keyPressed(GlobalKeyEvent event) {
|
||||
if(event.getVirtualKeyCode() == modifierKey)
|
||||
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
|
||||
if(nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_ALT_L)
|
||||
modified = true;
|
||||
else if(modified){
|
||||
switch(event.getVirtualKeyCode()){
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
|
||||
if(nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_ALT_L)
|
||||
modified = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
|
||||
if(modified){
|
||||
switch(nativeKeyEvent.getRawCode()){
|
||||
case playPause:
|
||||
if(player.isPlaying()){
|
||||
player.pause();
|
||||
@ -610,11 +634,5 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(GlobalKeyEvent event) {
|
||||
if(event.getVirtualKeyCode() == modifierKey)
|
||||
modified = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user