📄 keybindings.java
字号:
final String[] tmpValues = keyBindingValue.split(DELIM_EXP);
final boolean[] specVisited = new boolean[SPECIAL_KEYS.length]; // flag for speed optimization
boolean funcVisited = false;
final StringBuffer displayValue = new StringBuffer(keyBindingValue.length() + 2); // allocate display string
displayValue.append('\t');
for (int i = 0; i < tmpValues.length; i++)
{
final String value = tmpValues[i];
boolean matched = false;
// process special keys first
for(int j = 0; j < SPECIAL_KEYS.length; j++)
{
if(!specVisited[j] && SPECIAL_KEYS[j].equalsIgnoreCase(value))
{
swtAccelerator = swtAccelerator | SPECIAL_VALUES[j];
// special-case meta; a generalized solution would be warranted if:
// a) additional special modifiers persist or
// b) other platforms have special labeling requirements or
// c) SWT changes its lower-level API so shortcut labels are no longer custom drawn on Win etc.
if(SPECIAL_KEYS[j].equalsIgnoreCase("Meta"))
displayValue.append(Constants.isOSX ? "Cmd" : "Ctrl").append(DELIM);
else
displayValue.append(SPECIAL_KEYS[j]).append(DELIM);
// mark flags
specVisited[j] = true;
matched = true;
break;
}
}
if(matched)
continue;
// special treatment for function keys
if(!funcVisited)
{
final Matcher funcMatcher = FUNC_EXP.matcher(value);
if(funcMatcher.find() && funcMatcher.start() == 0 && funcMatcher.end() == value.length())
{
final int funcVal = Integer.parseInt(funcMatcher.group(2));
// SWT.F1 is (1 << 24) + 10
swtAccelerator = swtAccelerator | ((1 << 24) + (9 + funcVal));
displayValue.append(funcMatcher.group(0)).append(DELIM);
funcVisited = true;
matched = true;
}
}
if(matched)
continue;
final Matcher valMatcher = SANCTIONED_EXP.matcher(value);
if(valMatcher.find() && valMatcher.start() == 0)
{
final char c = valMatcher.group().charAt(0);
// avoid possible duplicates (\t is index 0)
final int subStrIndex = displayValue.indexOf(c + DELIM);
if(subStrIndex == 1 || (subStrIndex > 1 && displayValue.substring(subStrIndex - 1, subStrIndex).equals(DELIM)))
continue;
swtAccelerator = swtAccelerator | c;
displayValue.append(c).append(DELIM);
}
}
if(funcVisited || specVisited[0] || specVisited[1] || specVisited[2] || specVisited[3] || specVisited[4]) // special case - be a bit careful for now
return new KeyBindingInfo(displayValue.substring(0, displayValue.length() - 1), swtAccelerator);
else
return new KeyBindingInfo(null, SWT.NONE);
}
/**
* <p>
* Removes the keyboard accelerator for a SWT MenuItem
* </p>
* @param menu SWT MenuItem
* @param localizationKey The MenuItem's localization key for the localization resource bundle
*/
public static void removeAccelerator(final MenuItem menu, String localizationKey)
{
setAccelerator(menu, new KeyBindingInfo("", SWT.NONE));
Messages.setLanguageText(menu, localizationKey);
}
/**
* <p>
* Sets the keyboard accelerator for a SWT MenuItem.
* </p>
* <p>
* There is a specific order of accelerator setting in consideration with different platforms and localizations. Specifically:<br />
* <ol>
* <li>If a localized keybinding value exists for the current locale and platform, it is used</li>
* <li>If the above is not found, this method looks for a keybinding value for the current locale without platform specificity</li>
* <li>If the above is not found, this method looks for a keybinding value for the default locale and the currently running platform</li>
* <li>If the above is not found, this method looks for a keybinding value for the default locale without platform specificity</li>
* <li>If the above is not found, no accelerator is set for the MenuItem</li>
* </ol>
* </p>
* @param menu SWT MenuItem
* @param localizationKey The MenuItem's localization key for the localization resource bundle
*/
public static void setAccelerator(final MenuItem menu, String localizationKey)
{
localizationKey += ".keybinding";
final String platformSpecificKey = localizationKey + getPlatformKeySuffix();
// first, check for platform-specific, localization-specific binding
if(MessageText.keyExists(platformSpecificKey))
{
setAccelerator(menu, parseKeyBinding(MessageText.getString(platformSpecificKey)));
}
else if(MessageText.keyExists(localizationKey)) // platform-independent, localization-specific binding
{
setAccelerator(menu, parseKeyBinding(MessageText.getString(localizationKey)));
}
else if(!MessageText.isCurrentLocale(MessageText.LOCALE_DEFAULT))
{
// default locale
// platform-specific first
if(MessageText.keyExistsForDefaultLocale(platformSpecificKey))
{
setAccelerator(menu, parseKeyBinding(MessageText.getDefaultLocaleString(platformSpecificKey)));
}
else if(MessageText.keyExistsForDefaultLocale(localizationKey))
{
// default locale, platform-independent
setAccelerator(menu, parseKeyBinding(MessageText.getDefaultLocaleString(localizationKey)));
}
}
}
/**
* Helper method to set a keyboard accelerator for a MenuItem. If kbInfo is SWT.NONE, no accelerator will be set.
* @param menu SWT MenuItem
* @param kbInfo KeyBindingInfo object, which contains the SWT accelerator value and its display name
*/
private static void setAccelerator(final MenuItem menu, final KeyBindingInfo kbInfo)
{
if(kbInfo.accelerator != SWT.NONE)
{
menu.setAccelerator(kbInfo.accelerator);
// SWT on OS X now uses native drawing
if(!Constants.isOSX && !menu.getText().endsWith(kbInfo.name))
menu.setText(menu.getText() + kbInfo.name);
}
}
/**
* Runs simple tests on KeyBindings keybinding values
* @param args Command-line arguments; they are not used
*/
public static void main(final String[] args)
{
System.out.println(parseKeyBinding("Ctrl+1").name); // meta+1
System.out.println(parseKeyBinding("Ctrl+F12").name); // meta+f12
System.out.println(parseKeyBinding("Ctrl+F4").name); // meta+f4
System.out.println("Meta+Shift+O");
System.out.println(parseKeyBinding("Ctrl+Shift+O").accelerator); // meta+shift+o
System.out.println(parseKeyBinding("Shift+Ctrl+O").accelerator); // meta+shift+o
System.out.println(SWT.MOD1 | SWT.SHIFT | 'O'); // meta+shift+o
System.out.println("Meta+Shift+o");
System.out.println(SWT.MOD1 | SWT.SHIFT | 'o'); // meta+shift+o
}
/**
* <p>
* A basic bean object containing the SWT accelerator and its display name. This is because on platforms like Windows, vanilla SWT MenuItem must be
* provided the textual representation (display name) of the accelerator in order for it to be visible to the users (as opposed to having it handled by a higher-
* level API like JFace or native rendering).
* </p>
*/
private static class KeyBindingInfo
{
/**
* The display name of the accelerator
*/
private final String name;
/**
* The SWT keyboard accelerator value
*/
private final int accelerator;
/**
* Constructs a new KeyBindingInfo object with the given accelerator name and accelerator value
* @param name Display name
* @param accelerator SWT accelerator value
*/
private KeyBindingInfo(final String name, final int accelerator)
{
this.name = name;
this.accelerator = accelerator;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -