Skip to content

Commit b5bf07b

Browse files
Let EditorToolbar use the global KeyboardFocusManager
For some toolbar buttons, when it is clicked while shift is pressed, its function changes. When handling the click event, this information is directly taken from KeyEvent.isShiftDown(). However, to also show the proper tooltip *before* clicking, EditorToolbar listened to key events on the main text area, to know when shift is (not) pressed. This approach means that pressing shift while the text area is not focused will not change the tooltip, and creates some unwanted coupling between the toolbar and the text area. This commit changes this approach to instead use the global KeyboardFocusManager. Any key presses pass through there before being dispatched to the currently focused component, so this makes sure that any shift presses are caught, as well as making EditorToolbar a bit more self-contained.
1 parent 3d084a2 commit b5bf07b

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

app/src/processing/app/Editor.java

-3
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,6 @@ public void windowDeactivated(WindowEvent e) {
337337
// listener = new EditorListener(this, textarea);
338338
pane.add(box);
339339

340-
// get shift down/up events so we can show the alt version of toolbar buttons
341-
textarea.addKeyListener(toolbar);
342-
343340
pane.setTransferHandler(new FileDropHandler());
344341

345342
// System.out.println("t1");

app/src/processing/app/EditorToolbar.java

+7-19
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import javax.swing.event.MouseInputListener;
2828
import java.awt.*;
2929
import java.awt.event.KeyEvent;
30-
import java.awt.event.KeyListener;
3130
import java.awt.event.MouseEvent;
3231

3332
import static processing.app.I18n.tr;
@@ -36,7 +35,7 @@
3635
/**
3736
* run/stop/etc buttons for the ide
3837
*/
39-
public class EditorToolbar extends JComponent implements MouseInputListener, KeyListener {
38+
public class EditorToolbar extends JComponent implements MouseInputListener, KeyEventDispatcher {
4039

4140
/**
4241
* Rollover titles for each button.
@@ -136,6 +135,7 @@ public EditorToolbar(Editor editor, JMenu menu) {
136135

137136
addMouseListener(this);
138137
addMouseMotionListener(this);
138+
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
139139
}
140140

141141
private void loadButtons() {
@@ -440,24 +440,12 @@ public Dimension getMaximumSize() {
440440
return new Dimension(3000, BUTTON_HEIGHT);
441441
}
442442

443-
444-
public void keyPressed(KeyEvent e) {
445-
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
446-
shiftPressed = true;
447-
repaint();
448-
}
449-
}
450-
451-
452-
public void keyReleased(KeyEvent e) {
453-
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
454-
shiftPressed = false;
443+
public boolean dispatchKeyEvent(final KeyEvent e) {
444+
if (shiftPressed != e.isShiftDown()) {
445+
shiftPressed = !shiftPressed;
455446
repaint();
456447
}
448+
// Return false to continue processing this keyEvent
449+
return false;
457450
}
458-
459-
460-
public void keyTyped(KeyEvent e) {
461-
}
462-
463451
}

0 commit comments

Comments
 (0)