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>
|
<version>4.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>lc.kra.system</groupId>
|
<groupId>com.1stleg</groupId>
|
||||||
<artifactId>system-hook</artifactId>
|
<artifactId>jnativehook</artifactId>
|
||||||
<version>2.2</version>
|
<version>2.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -102,14 +102,6 @@
|
|||||||
<id>jaudiotagger-repository</id>
|
<id>jaudiotagger-repository</id>
|
||||||
<url>https://dl.bintray.com/ijabz/maven</url>
|
<url>https://dl.bintray.com/ijabz/maven</url>
|
||||||
</repository>
|
</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>
|
</repositories>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,8 +1,5 @@
|
|||||||
package musicplayer;
|
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.LibraryCallbackInterface;
|
||||||
import musicplayer.callbacks.PlayerCallbackInterface;
|
import musicplayer.callbacks.PlayerCallbackInterface;
|
||||||
import musicplayer.db.DatabaseManager;
|
import musicplayer.db.DatabaseManager;
|
||||||
@ -15,6 +12,10 @@ import musicplayer.swingmodels.PlaylistTableModel;
|
|||||||
import musicplayer.util.Icons;
|
import musicplayer.util.Icons;
|
||||||
import musicplayer.util.LibraryUtils;
|
import musicplayer.util.LibraryUtils;
|
||||||
import musicplayer.util.PlaylistUtils;
|
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.*;
|
||||||
import javax.swing.event.HyperlinkEvent;
|
import javax.swing.event.HyperlinkEvent;
|
||||||
@ -36,6 +37,8 @@ import java.nio.file.Path;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface {
|
public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface {
|
||||||
@ -68,11 +71,23 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
|
|||||||
}, "seekbar");
|
}, "seekbar");
|
||||||
seekBarUpdater.start();
|
seekBarUpdater.start();
|
||||||
try {
|
try {
|
||||||
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();
|
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
|
||||||
keyboardHook.addKeyListener(new GlobalKeyboardShortcuts());
|
logger.setLevel(Level.WARNING);
|
||||||
Thread hookShutdown = new Thread(keyboardHook::shutdownHook);
|
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);
|
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("Keyboard hook failed, global shortcuts will not work this session.");
|
||||||
System.out.println(ex.getMessage());
|
System.out.println(ex.getMessage());
|
||||||
System.out.println(Arrays.toString(ex.getStackTrace()));
|
System.out.println(Arrays.toString(ex.getStackTrace()));
|
||||||
@ -573,20 +588,29 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
|
|||||||
public void mouseExited(MouseEvent e) {}
|
public void mouseExited(MouseEvent e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class GlobalKeyboardShortcuts extends GlobalKeyAdapter {
|
private class GlobalKeyboardShortcuts implements NativeKeyListener {
|
||||||
final int modifierKey = 164;
|
boolean modified = false;
|
||||||
final int playPause = 192;
|
final int playPause = 192;
|
||||||
final int stop = 222;
|
final int stop = 222;
|
||||||
final int previous = 219;
|
final int previous = 219;
|
||||||
final int next = 221;
|
final int next = 221;
|
||||||
boolean modified = false;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyPressed(GlobalKeyEvent event) {
|
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
|
||||||
if(event.getVirtualKeyCode() == modifierKey)
|
if(nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_ALT_L)
|
||||||
modified = true;
|
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:
|
case playPause:
|
||||||
if(player.isPlaying()){
|
if(player.isPlaying()){
|
||||||
player.pause();
|
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