Skip to content

Commit b988176

Browse files
committed
Accelerator: refine meaning of the caret (^)
On OS X, it was previously impossible to define a plugin's shortcut as "control KEY" for any key. The control was always replaced with meta. Ultimately, I consider this limitation to be a bug. To fix it, the caret character now expands to control on non-Mac platforms, and expands to meta (i.e., the command key) on Mac platforms. 99% of the time, this is the desired behavior for keyboard shortcuts. However, if you actually want a plugin to always use the control key across all platforms, you can now spell it out by writing "control" or "ctrl" and it will no longer be replaced by meta on OS X. Technically, this is a backwards incompatible change in behavior which one could argue warrants a bump to 3.0.0 according to SemVer. However: * It is not a backwards-incompatible change to the API proper. * It is unlikely that any downstream code other than ImageJ2 will be affected by the change. * The fix is trivial (replace spelled out "control " and "ctrl " strings with "^" in affected plugins). * The previous behavior was inflexible and undesirable. So we'll leave this as a "bug-fix" instead.
1 parent 3ae109d commit b988176

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

src/main/java/org/scijava/input/Accelerator.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public String toString() {
108108
* <ul>
109109
* <li>"control" may be shortened to "ctrl"</li>
110110
* <li>"altGraph" may be shortened to "altGr"</li>
111-
* <li>The caret character ('^') is expanded to "control "</li>
111+
* <li>The caret character ('^') is expanded to "control " on non-Mac
112+
* platforms, and "meta " (i.e., the Mac command key) on Mac platforms.</li>
112113
* </ul>
113114
* Here are some example strings:
114115
* <ul>
@@ -125,8 +126,9 @@ public String toString() {
125126
public static Accelerator create(final String acc) {
126127
if (acc == null || acc.isEmpty()) return null;
127128

128-
// allow use of caret for control (e.g., "^X" to represent "control X")
129-
final String a = acc.replaceAll(Pattern.quote("^"), "control ");
129+
// allow use of caret for control/command
130+
// (e.g., "^X" to mean "control X" or "meta X")
131+
final String a = acc.replaceAll(Pattern.quote("^"), expandedCaret());
130132

131133
final String[] components = a.split(" ");
132134

@@ -149,12 +151,6 @@ else if (components[i].equalsIgnoreCase("control") ||
149151
else if (components[i].equalsIgnoreCase("shift")) shift = true;
150152
}
151153

152-
// replace control with meta, if applicable
153-
if (ctrl && !meta && isCtrlReplacedWithMeta()) {
154-
ctrl = false;
155-
meta = true;
156-
}
157-
158154
final InputModifiers modifiers =
159155
new InputModifiers(alt, altGr, ctrl, meta, shift, false, false, false);
160156

@@ -170,4 +166,10 @@ public static boolean isCtrlReplacedWithMeta() {
170166
return System.getProperty("os.name").startsWith("Mac");
171167
}
172168

169+
// -- Helper methods --
170+
171+
private static String expandedCaret() {
172+
return isCtrlReplacedWithMeta() ? "meta " : "control ";
173+
}
174+
173175
}

0 commit comments

Comments
 (0)