diff --git a/pom.xml b/pom.xml index b061566..72ee09e 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,11 @@ junit 4.12 + + lc.kra.system + system-hook + 2.1 + @@ -97,6 +102,14 @@ 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 97b6e2f..831cf30 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -1,5 +1,8 @@ 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; @@ -14,10 +17,7 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import java.awt.*; -import java.awt.event.ItemEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; +import java.awt.event.*; import java.io.File; import java.io.IOException; import java.nio.file.Path; @@ -55,6 +55,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf } }, "seekbar"); seekBarUpdater.start(); + GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(); + keyboardHook.addKeyListener(new GlobalKeyboardShortcuts()); refreshLibrary(); } @@ -499,4 +501,49 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf @Override public void mouseExited(MouseEvent e) {} } + + private class GlobalKeyboardShortcuts extends GlobalKeyAdapter { + final int modifierKey = 93; + final int playPause = 116; + final int stop = 117; + final int previous = 118; + final int next = 119; + boolean modified = false; + + @Override + public void keyPressed(GlobalKeyEvent event) { + if(event.getVirtualKeyCode() == modifierKey) + modified = true; + else if(modified){ + switch(event.getVirtualKeyCode()){ + case playPause: + if(player.isPlaying()){ + player.pause(); + setSongHighlighting(player.getCurrentSong()); // Resume won't function if a different song is selected. + } + else{ + if (playList.getRowCount() > 0) { + if (playList.getSelectedRowCount() > 0) + player.playSong(playlistTableModel.getSong(playList.getSelectedRow())); + else + player.playSong(playlistTableModel.getFirst()); + } + } + break; + case stop: + player.stop(); break; + case previous: + playPreviousSong(); break; + case next: + playNextSong(); break; + } + } + } + + @Override + public void keyReleased(GlobalKeyEvent event) { + if(event.getVirtualKeyCode() == modifierKey) + modified = false; + } + } }