Commit cd5544f2345a48e0bb452355855c95eb9b2a451b

Authored by pfrison
1 parent c5abccad

PercTeacher added keyboard controls

PercTeacher/Sources/ActionList.java
@@ -123,6 +123,7 @@ public class ActionList { @@ -123,6 +123,7 @@ public class ActionList {
123 String str = ""; 123 String str = "";
124 for(Action action : actions) 124 for(Action action : actions)
125 str += action.toString() + "\n"; 125 str += action.toString() + "\n";
  126 + str = str.substring(0, str.length() - 1); // remove last \n
126 return str; 127 return str;
127 } 128 }
128 } 129 }
PercTeacher/Sources/Interface.java
@@ -4,10 +4,13 @@ import java.awt.Component; @@ -4,10 +4,13 @@ import java.awt.Component;
4 import java.awt.FlowLayout; 4 import java.awt.FlowLayout;
5 import java.awt.Font; 5 import java.awt.Font;
6 import java.awt.GridLayout; 6 import java.awt.GridLayout;
  7 +import java.awt.KeyEventDispatcher;
  8 +import java.awt.KeyboardFocusManager;
7 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener; 10 import java.awt.event.ActionListener;
9 import java.awt.event.FocusEvent; 11 import java.awt.event.FocusEvent;
10 import java.awt.event.FocusListener; 12 import java.awt.event.FocusListener;
  13 +import java.awt.event.KeyEvent;
11 14
12 import javax.swing.BorderFactory; 15 import javax.swing.BorderFactory;
13 import javax.swing.BoxLayout; 16 import javax.swing.BoxLayout;
@@ -37,6 +40,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @@ -37,6 +40,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener {
37 private JButton revertButton; 40 private JButton revertButton;
38 private JButton exportButton; 41 private JButton exportButton;
39 private JButton importButton; 42 private JButton importButton;
  43 + private boolean keyboardWait = false;
40 44
41 private ActionList actionList; 45 private ActionList actionList;
42 46
@@ -47,6 +51,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @@ -47,6 +51,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener {
47 populateWindow(); 51 populateWindow();
48 pack(); 52 pack();
49 setLocationRelativeTo(null); // center window 53 setLocationRelativeTo(null); // center window
  54 + KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new CustomDispatcher());
  55 + getContentPane().requestFocusInWindow();
50 } 56 }
51 57
52 private void populateWindow() { 58 private void populateWindow() {
@@ -93,8 +99,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @@ -93,8 +99,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener {
93 moveCommandsPanel.add(label); 99 moveCommandsPanel.add(label);
94 } else { 100 } else {
95 JButton button = new JButton(new ImageIcon(getClass().getResource("/icons/move" + String.valueOf(j) + String.valueOf(i) + ".png"))); 101 JButton button = new JButton(new ImageIcon(getClass().getResource("/icons/move" + String.valueOf(j) + String.valueOf(i) + ".png")));
96 - moveButtons[i][j] = button;  
97 button.addActionListener(this); 102 button.addActionListener(this);
  103 + moveButtons[i][j] = button;
98 moveCommandsPanel.add(button); 104 moveCommandsPanel.add(button);
99 } 105 }
100 } 106 }
@@ -176,6 +182,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @@ -176,6 +182,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener {
176 182
177 @Override 183 @Override
178 public void actionPerformed(ActionEvent e) { 184 public void actionPerformed(ActionEvent e) {
  185 + keyboardWait = true;
179 for(int i=0; i<3; i++) { 186 for(int i=0; i<3; i++) {
180 for(int j=0; j<3; j++) { 187 for(int j=0; j<3; j++) {
181 if(e.getSource() == moveButtons[i][j]) { 188 if(e.getSource() == moveButtons[i][j]) {
@@ -211,6 +218,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @@ -211,6 +218,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener {
211 actionList = newActionList; 218 actionList = newActionList;
212 } 219 }
213 updateLogs(); 220 updateLogs();
  221 + keyboardWait = false;
214 } 222 }
215 223
216 private void updateLogs() { 224 private void updateLogs() {
@@ -220,11 +228,49 @@ public class Interface extends JFrame implements ActionListener, FocusListener { @@ -220,11 +228,49 @@ public class Interface extends JFrame implements ActionListener, FocusListener {
220 228
221 @Override 229 @Override
222 public void focusGained(FocusEvent e) { 230 public void focusGained(FocusEvent e) {
  231 + keyboardWait = true;
223 if(e.getSource() == stepsField) { 232 if(e.getSource() == stepsField) {
224 stepsField.setBorder(defaultJTextFieldBorder); 233 stepsField.setBorder(defaultJTextFieldBorder);
225 stepsField.setCaretPosition(stepsField.getDocument().getLength()); 234 stepsField.setCaretPosition(stepsField.getDocument().getLength());
226 } 235 }
227 } 236 }
228 @Override 237 @Override
229 - public void focusLost(FocusEvent e) {} 238 + public void focusLost(FocusEvent e) {
  239 + keyboardWait = false;
  240 + }
  241 +
  242 + private class CustomDispatcher implements KeyEventDispatcher {
  243 + @Override
  244 + public boolean dispatchKeyEvent(KeyEvent e) {
  245 + if(keyboardWait && Interface.this.getFocusOwner() == stepsField && e.getKeyCode() == KeyEvent.VK_ENTER) {
  246 + Interface.this.getContentPane().requestFocusInWindow();
  247 + return true;
  248 + }
  249 + if(e.getID() != KeyEvent.KEY_RELEASED || keyboardWait)
  250 + return false;
  251 + if(e.getKeyCode() == KeyEvent.VK_NUMPAD1)
  252 + actionPerformed(new ActionEvent(moveButtons[2][0], 0, null));
  253 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD2)
  254 + actionPerformed(new ActionEvent(moveButtons[2][1], 0, null));
  255 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD3)
  256 + actionPerformed(new ActionEvent(moveButtons[2][2], 0, null));
  257 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD4)
  258 + actionPerformed(new ActionEvent(moveButtons[1][0], 0, null));
  259 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD6)
  260 + actionPerformed(new ActionEvent(moveButtons[1][2], 0, null));
  261 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD7)
  262 + actionPerformed(new ActionEvent(moveButtons[0][0], 0, null));
  263 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD8)
  264 + actionPerformed(new ActionEvent(moveButtons[0][1], 0, null));
  265 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD9)
  266 + actionPerformed(new ActionEvent(moveButtons[0][2], 0, null));
  267 + else if(e.getKeyCode() == KeyEvent.VK_SPACE)
  268 + actionPerformed(new ActionEvent(nextButton, 0, null));
  269 + else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
  270 + actionPerformed(new ActionEvent(revertButton, 0, null));
  271 + else if(e.getKeyCode() == KeyEvent.VK_NUMPAD0)
  272 + actionPerformed(new ActionEvent(waitButton, 0, null));
  273 + return true;
  274 + }
  275 + }
230 } 276 }
PercTeacher/Sources/Main.java
@@ -2,6 +2,7 @@ import javax.swing.JFrame; @@ -2,6 +2,7 @@ import javax.swing.JFrame;
2 2
3 /* TODO list : 3 /* TODO list :
4 * - data to send to the arduino : do action, revert action 4 * - data to send to the arduino : do action, revert action
  5 + * - textfields to tweak delays (+ button set to default)
5 */ 6 */
6 7
7 public class Main { 8 public class Main {
PercTeacher/Sources/icons/Thumbs.db
No preview for this file type