From cd5544f2345a48e0bb452355855c95eb9b2a451b Mon Sep 17 00:00:00 2001 From: pfrison Date: Wed, 27 Feb 2019 03:08:34 +0100 Subject: [PATCH] PercTeacher added keyboard controls --- PercTeacher/Sources/ActionList.java | 1 + PercTeacher/Sources/Interface.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- PercTeacher/Sources/Main.java | 1 + PercTeacher/Sources/icons/Thumbs.db | Bin 13824 -> 0 bytes 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/PercTeacher/Sources/ActionList.java b/PercTeacher/Sources/ActionList.java index de7b6fb..3d7393a 100644 --- a/PercTeacher/Sources/ActionList.java +++ b/PercTeacher/Sources/ActionList.java @@ -123,6 +123,7 @@ public class ActionList { String str = ""; for(Action action : actions) str += action.toString() + "\n"; + str = str.substring(0, str.length() - 1); // remove last \n return str; } } diff --git a/PercTeacher/Sources/Interface.java b/PercTeacher/Sources/Interface.java index d320264..03a8312 100644 --- a/PercTeacher/Sources/Interface.java +++ b/PercTeacher/Sources/Interface.java @@ -4,10 +4,13 @@ import java.awt.Component; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; +import java.awt.KeyEventDispatcher; +import java.awt.KeyboardFocusManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; import javax.swing.BorderFactory; import javax.swing.BoxLayout; @@ -37,6 +40,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { private JButton revertButton; private JButton exportButton; private JButton importButton; + private boolean keyboardWait = false; private ActionList actionList; @@ -47,6 +51,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener { populateWindow(); pack(); setLocationRelativeTo(null); // center window + KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new CustomDispatcher()); + getContentPane().requestFocusInWindow(); } private void populateWindow() { @@ -93,8 +99,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener { moveCommandsPanel.add(label); } else { JButton button = new JButton(new ImageIcon(getClass().getResource("/icons/move" + String.valueOf(j) + String.valueOf(i) + ".png"))); - moveButtons[i][j] = button; button.addActionListener(this); + moveButtons[i][j] = button; moveCommandsPanel.add(button); } } @@ -176,6 +182,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @Override public void actionPerformed(ActionEvent e) { + keyboardWait = true; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(e.getSource() == moveButtons[i][j]) { @@ -211,6 +218,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { actionList = newActionList; } updateLogs(); + keyboardWait = false; } private void updateLogs() { @@ -220,11 +228,49 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @Override public void focusGained(FocusEvent e) { + keyboardWait = true; if(e.getSource() == stepsField) { stepsField.setBorder(defaultJTextFieldBorder); stepsField.setCaretPosition(stepsField.getDocument().getLength()); } } @Override - public void focusLost(FocusEvent e) {} + public void focusLost(FocusEvent e) { + keyboardWait = false; + } + + private class CustomDispatcher implements KeyEventDispatcher { + @Override + public boolean dispatchKeyEvent(KeyEvent e) { + if(keyboardWait && Interface.this.getFocusOwner() == stepsField && e.getKeyCode() == KeyEvent.VK_ENTER) { + Interface.this.getContentPane().requestFocusInWindow(); + return true; + } + if(e.getID() != KeyEvent.KEY_RELEASED || keyboardWait) + return false; + if(e.getKeyCode() == KeyEvent.VK_NUMPAD1) + actionPerformed(new ActionEvent(moveButtons[2][0], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD2) + actionPerformed(new ActionEvent(moveButtons[2][1], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD3) + actionPerformed(new ActionEvent(moveButtons[2][2], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD4) + actionPerformed(new ActionEvent(moveButtons[1][0], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD6) + actionPerformed(new ActionEvent(moveButtons[1][2], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD7) + actionPerformed(new ActionEvent(moveButtons[0][0], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD8) + actionPerformed(new ActionEvent(moveButtons[0][1], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD9) + actionPerformed(new ActionEvent(moveButtons[0][2], 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_SPACE) + actionPerformed(new ActionEvent(nextButton, 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) + actionPerformed(new ActionEvent(revertButton, 0, null)); + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD0) + actionPerformed(new ActionEvent(waitButton, 0, null)); + return true; + } + } } diff --git a/PercTeacher/Sources/Main.java b/PercTeacher/Sources/Main.java index 004d9e3..b30c1bb 100644 --- a/PercTeacher/Sources/Main.java +++ b/PercTeacher/Sources/Main.java @@ -2,6 +2,7 @@ import javax.swing.JFrame; /* TODO list : * - data to send to the arduino : do action, revert action + * - textfields to tweak delays (+ button set to default) */ public class Main { diff --git a/PercTeacher/Sources/icons/Thumbs.db b/PercTeacher/Sources/icons/Thumbs.db index 1b3090a..1d7752f 100644 Binary files a/PercTeacher/Sources/icons/Thumbs.db and b/PercTeacher/Sources/icons/Thumbs.db differ -- libgit2 0.21.2