Commit cd5544f2345a48e0bb452355855c95eb9b2a451b
1 parent
c5abccad
PercTeacher added keyboard controls
Showing
4 changed files
with
50 additions
and
2 deletions
Show diff stats
PercTeacher/Sources/ActionList.java
PercTeacher/Sources/Interface.java
... | ... | @@ -4,10 +4,13 @@ import java.awt.Component; |
4 | 4 | import java.awt.FlowLayout; |
5 | 5 | import java.awt.Font; |
6 | 6 | import java.awt.GridLayout; |
7 | +import java.awt.KeyEventDispatcher; | |
8 | +import java.awt.KeyboardFocusManager; | |
7 | 9 | import java.awt.event.ActionEvent; |
8 | 10 | import java.awt.event.ActionListener; |
9 | 11 | import java.awt.event.FocusEvent; |
10 | 12 | import java.awt.event.FocusListener; |
13 | +import java.awt.event.KeyEvent; | |
11 | 14 | |
12 | 15 | import javax.swing.BorderFactory; |
13 | 16 | import javax.swing.BoxLayout; |
... | ... | @@ -37,6 +40,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { |
37 | 40 | private JButton revertButton; |
38 | 41 | private JButton exportButton; |
39 | 42 | private JButton importButton; |
43 | + private boolean keyboardWait = false; | |
40 | 44 | |
41 | 45 | private ActionList actionList; |
42 | 46 | |
... | ... | @@ -47,6 +51,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener { |
47 | 51 | populateWindow(); |
48 | 52 | pack(); |
49 | 53 | setLocationRelativeTo(null); // center window |
54 | + KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new CustomDispatcher()); | |
55 | + getContentPane().requestFocusInWindow(); | |
50 | 56 | } |
51 | 57 | |
52 | 58 | private void populateWindow() { |
... | ... | @@ -93,8 +99,8 @@ public class Interface extends JFrame implements ActionListener, FocusListener { |
93 | 99 | moveCommandsPanel.add(label); |
94 | 100 | } else { |
95 | 101 | JButton button = new JButton(new ImageIcon(getClass().getResource("/icons/move" + String.valueOf(j) + String.valueOf(i) + ".png"))); |
96 | - moveButtons[i][j] = button; | |
97 | 102 | button.addActionListener(this); |
103 | + moveButtons[i][j] = button; | |
98 | 104 | moveCommandsPanel.add(button); |
99 | 105 | } |
100 | 106 | } |
... | ... | @@ -176,6 +182,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { |
176 | 182 | |
177 | 183 | @Override |
178 | 184 | public void actionPerformed(ActionEvent e) { |
185 | + keyboardWait = true; | |
179 | 186 | for(int i=0; i<3; i++) { |
180 | 187 | for(int j=0; j<3; j++) { |
181 | 188 | if(e.getSource() == moveButtons[i][j]) { |
... | ... | @@ -211,6 +218,7 @@ public class Interface extends JFrame implements ActionListener, FocusListener { |
211 | 218 | actionList = newActionList; |
212 | 219 | } |
213 | 220 | updateLogs(); |
221 | + keyboardWait = false; | |
214 | 222 | } |
215 | 223 | |
216 | 224 | private void updateLogs() { |
... | ... | @@ -220,11 +228,49 @@ public class Interface extends JFrame implements ActionListener, FocusListener { |
220 | 228 | |
221 | 229 | @Override |
222 | 230 | public void focusGained(FocusEvent e) { |
231 | + keyboardWait = true; | |
223 | 232 | if(e.getSource() == stepsField) { |
224 | 233 | stepsField.setBorder(defaultJTextFieldBorder); |
225 | 234 | stepsField.setCaretPosition(stepsField.getDocument().getLength()); |
226 | 235 | } |
227 | 236 | } |
228 | 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
PercTeacher/Sources/icons/Thumbs.db
No preview for this file type