From b77399dee8399ec78c0caff6f56522280a724a15 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 15 Mar 2016 04:56:17 +0000 Subject: [PATCH] Changed keyboard hook library for better platform support. --- pom.xml | 14 ++---- src/main/java/musicplayer/PlayerGUI.java | 58 ++++++++++++++++-------- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 656248f..d91fd24 100644 --- a/pom.xml +++ b/pom.xml @@ -86,9 +86,9 @@ 4.12 - lc.kra.system - system-hook - 2.2 + com.1stleg + jnativehook + 2.0.2 @@ -102,14 +102,6 @@ jaudiotagger-repository https://dl.bintray.com/ijabz/maven - - system-hook-mvn-repo - https://raw.github.com/kristian/system-hook/mvn-repo/ - - true - always - - \ No newline at end of file diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index fa78262..d5d9043 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -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; - } } }