Java Code Examples for java.awt.event.KeyEvent#ALT_MASK

The following examples show how to use java.awt.event.KeyEvent#ALT_MASK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionButtonUI.java    From consulo with Apache License 2.0 6 votes vote down vote up
private int getMnemonicCharIndex(ActionButton button, AnAction action, String text) {
  final int mnemonicIndex = button.getPresentation().getDisplayedMnemonicIndex();
  if (mnemonicIndex != -1) {
    return mnemonicIndex;
  }
  final ShortcutSet shortcutSet = action.getShortcutSet();
  final Shortcut[] shortcuts = shortcutSet.getShortcuts();
  for (int i = 0; i < shortcuts.length; i++) {
    Shortcut shortcut = shortcuts[i];
    if (shortcut instanceof KeyboardShortcut) {
      KeyboardShortcut keyboardShortcut = (KeyboardShortcut)shortcut;
      if (keyboardShortcut.getSecondKeyStroke() == null) { // we are interested only in "mnemonic-like" shortcuts
        final KeyStroke keyStroke = keyboardShortcut.getFirstKeyStroke();
        final int modifiers = keyStroke.getModifiers();
        if ((modifiers & KeyEvent.ALT_MASK) != 0) {
          return (keyStroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED)
                 ? text.indexOf(keyStroke.getKeyChar())
                 : text.indexOf(KeyEvent.getKeyText(keyStroke.getKeyCode()));
        }
      }
    }
  }
  return -1;
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean usableKeyOnMac(int key, int mask) {
    //All permutations fail for Q except ctrl
    if (key == KeyEvent.VK_Q) {
        return false;
    }

    boolean isMeta = ((mask & KeyEvent.META_MASK) != 0) || ((mask & KeyEvent.CTRL_DOWN_MASK) != 0);

    boolean isAlt = ((mask & KeyEvent.ALT_MASK) != 0) || ((mask & KeyEvent.ALT_DOWN_MASK) != 0);

    boolean isOnlyMeta = isMeta && ((mask & ~(KeyEvent.META_DOWN_MASK | KeyEvent.META_MASK)) == 0);

    //Mac OS consumes keys Command+ these keys - the app will never see
    //them, so CTRL should not be remapped for these
    if (isOnlyMeta) {
        return (key != KeyEvent.VK_H) && (key != KeyEvent.VK_SPACE) && (key != KeyEvent.VK_TAB);
    }
    if ((key == KeyEvent.VK_D) && isMeta && isAlt) {
        return false;
    }
    if (key == KeyEvent.VK_SPACE && isMeta && ((mask & KeyEvent.CTRL_MASK) != 0)) {
        // http://lists.apple.com/archives/java-dev/2010/Aug/msg00002.html
        return false;
    }
    return true;
}
 
Example 3
Source File: EditorActionUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static String appendKeyMnemonic(StringBuilder sb, KeyStroke key) {
    String sk = org.openide.util.Utilities.keyToString(key);
    int mods = key.getModifiers();
    if ((mods & KeyEvent.CTRL_MASK) != 0) {
        sb.append("Ctrl+"); // NOI18N
    }
    if ((mods & KeyEvent.ALT_MASK) != 0) {
        sb.append("Alt+"); // NOI18N
    }
    if ((mods & KeyEvent.SHIFT_MASK) != 0) {
        sb.append("Shift+"); // NOI18N
    }
    if ((mods & KeyEvent.META_MASK) != 0) {
        sb.append("Meta+"); // NOI18N
    }

    int i = sk.indexOf('-'); //NOI18N
    if (i != -1) {
        sk = sk.substring(i + 1);
    }
    sb.append(sk);

    return sb.toString();
}
 
Example 4
Source File: ResourceBundleSupport.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the keystroke stored at the given resourcebundle key.
 * <p/>
 * The keystroke will be composed of a simple key press and a keystroke mask pattern. The pattern should be specified
 * via the words "shift", "alt", "ctrl", "meta" or "menu". Menu should be used to reference the platform specific menu
 * shortcut. For the sake of safety, menu should only be combined with "shift" and/or "alt" for menu keystrokes.
 * <p/>
 * The keystrokes character key should be either the symbolic name of one of the KeyEvent.VK_* constants or the
 * character for that key.
 * <p/>
 * For the 'A' key, the resource bundle would therefore either contain "VK_A" or "a".
 * <pre>
 * a.resourcebundle.key=VK_A
 * an.other.resourcebundle.key=a
 * </pre>
 *
 * @param key the resourcebundle key
 * @return the keystroke
 * @see java.awt.Toolkit#getMenuShortcutKeyMask()
 */
public KeyStroke getOptionalKeyStroke( final String key ) {
  try {
    String name = getOptionalString( key );
    if ( StringUtils.isEmpty( name ) ) {
      return null;
    }

    boolean noneSelected = false;
    int mask = 0;
    final StringTokenizer strtok = new StringTokenizer( name );
    while ( strtok.hasMoreTokens() ) {
      final String token = strtok.nextToken();
      if ( "shift".equals( token ) ) {
        mask |= KeyEvent.SHIFT_MASK;
      } else if ( "alt".equals( token ) ) {
        mask |= KeyEvent.ALT_MASK;
      } else if ( "ctrl".equals( token ) ) {
        mask |= KeyEvent.CTRL_MASK;
      } else if ( "meta".equals( token ) ) {
        mask |= KeyEvent.META_MASK;
      } else if ( "menu".equals( token ) ) {
        mask |= getMenuKeyMask();
      } else if ( "none".equals( token ) ) {
        noneSelected = true;
      } else {
        name = token;
      }
    }
    if ( noneSelected ) {
      mask = 0;
    } else if ( mask == 0 ) {
      mask = getMenuKeyMask();
    }
    //noinspection MagicConstant
    return KeyStroke.getKeyStroke( createMnemonic( name ).intValue(), mask );
  } catch ( MissingResourceException mre ) {
    return null;
  }
}
 
Example 5
Source File: SearchFieldKeyListener.java    From otroslogviewer with Apache License 2.0 5 votes vote down vote up
@Override
public void keyPressed(KeyEvent e) {
  if (e.getKeyCode() == 10) {
    String text = searchTextField.getText().trim();
    if (text.length() == 0) {
      return;
    }
    if (0 == e.getModifiers()) {
      searchAction.performSearch(text, SearchDirection.FORWARD);
    } else if (KeyEvent.ALT_MASK == e.getModifiers()) {
      searchAction.performSearch(text, SearchDirection.REVERSE);
    }
  }
}
 
Example 6
Source File: KeyShortCutEditPanel.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
private void updateCurrentSelectedForKey(final KeyEvent evt) {
  final int index = this.tableKeyShortcuts.getSelectedRow();
  if (index >= 0) {
    final KeyShortcut oldShortcut = this.listOfKeys.get(index);
    final int keyCode = evt.getKeyCode();
    final int modifiers = evt.getModifiers() & (KeyEvent.META_MASK | KeyEvent.SHIFT_MASK | KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK);
    final KeyShortcut newShortCut = new KeyShortcut(oldShortcut.getID(), keyCode, modifiers);
    this.listOfKeys.set(index, newShortCut);
    for (final TableModelListener l : this.listeners) {
      l.tableChanged(new TableModelEvent(this, index));
    }
  }

  updateForSelected();
}
 
Example 7
Source File: KeyShortCutEditPanel.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
private void updateCurrentSelectedForKey (final KeyEvent evt) {
  final int index = this.tableKeyShortcuts.getSelectedRow();
  if (index>=0){
    final KeyShortcut oldShortcut = this.listOfKeys.get(index);
    final int keyCode = evt.getKeyCode();
    final int modifiers = evt.getModifiers() & (KeyEvent.META_MASK | KeyEvent.SHIFT_MASK | KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK);
    final KeyShortcut newShortCut = new KeyShortcut(oldShortcut.getID(),keyCode,modifiers);
    this.listOfKeys.set(index, newShortCut);
    for(final TableModelListener l:this.listeners){
      l.tableChanged(new TableModelEvent(this,index));
    }
  }
  
  updateForSelected();
}
 
Example 8
Source File: ShortcutListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void keyPressed(KeyEvent e) {
    assert (e.getSource() instanceof JTextField);

    if(((e.getModifiers() & (KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK | KeyEvent.CTRL_MASK | KeyEvent.META_MASK)) == 0) &&
            (e.getKeyCode() == KeyEvent.VK_DOWN || 
            e.getKeyCode() == KeyEvent.VK_UP ||
            e.getKeyCode() == KeyEvent.VK_ESCAPE)) {
        return ;
    }
    
    textField = (JTextField) e.getSource();
    KeyStroke keyStroke = createKeyStroke(e);

    boolean add = e.getKeyCode() != KeyEvent.VK_SHIFT &&
            e.getKeyCode() != KeyEvent.VK_CONTROL &&
            e.getKeyCode() != KeyEvent.VK_ALT &&
            e.getKeyCode() != KeyEvent.VK_META &&
            e.getKeyCode() != KeyEvent.VK_ALT_GRAPH;

    if (!(enterConfirms && keyStroke.equals(enterKS))) {
        if (keyStroke.equals(backspaceKS) && !key.equals("")) {
            // delete last key
            int i = key.lastIndexOf(' '); //NOI18N
            if (i < 0) {
                key = ""; //NOI18N
            } else {
                key = key.substring(0, i);
            }
            textField.setText(key);
        } else {
            // add key
            addKeyStroke(keyStroke, add);
        }

        e.consume();
    }
}
 
Example 9
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean addModifiersPortable(StringBuilder buf, int modifiers) {
    boolean b = false;

    if ((modifiers & KeyEvent.SHIFT_MASK) != 0) {
        buf.append('S');
        b = true;
    }

    if (Utilities.isMac() && ((modifiers & KeyEvent.META_MASK) != 0) || !Utilities.isMac() && ((modifiers & KeyEvent.CTRL_MASK) != 0)) {
        buf.append('D');
        b = true;
    }

    if (Utilities.isMac() && ((modifiers & KeyEvent.CTRL_MASK) != 0) || !Utilities.isMac() && ((modifiers & KeyEvent.ALT_MASK) != 0)) {
        buf.append('O');
        b = true;
    }
    // mac alt fallback
    if (Utilities.isMac() && ((modifiers & KeyEvent.ALT_MASK) != 0)) {
        buf.append('A');
        b = true;
    }
    // META fallback, see issue #224362
    if (!Utilities.isMac() && ((modifiers & KeyEvent.META_MASK) != 0)) {
        buf.append('M');
        b = true;
    }

    return b;
}
 
Example 10
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Adds characters for modifiers to the buffer.
* @param buf buffer to add to
* @param modif modifiers to add (KeyEvent.XXX_MASK)
* @return true if something has been added
*/
private static boolean addModifiers(StringBuilder buf, int modif) {
    boolean b = false;

    if ((modif & KeyEvent.CTRL_MASK) != 0) {
        buf.append("C"); // NOI18N
        b = true;
    }

    if ((modif & KeyEvent.ALT_MASK) != 0) {
        buf.append("A"); // NOI18N
        b = true;
    }

    if ((modif & KeyEvent.SHIFT_MASK) != 0) {
        buf.append("S"); // NOI18N
        b = true;
    }

    if ((modif & KeyEvent.META_MASK) != 0) {
        buf.append("M"); // NOI18N
        b = true;
    }

    if ((modif & CTRL_WILDCARD_MASK) != 0) {
        buf.append("D");
        b = true;
    }

    if ((modif & ALT_WILDCARD_MASK) != 0) {
        buf.append("O");
        b = true;
    }

    return b;
}
 
Example 11
Source File: HotkeyData.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns hotkey modifiers.
 *
 * @return hotkey modifiers
 */
public int getModifiers ()
{
    return ( isCtrl ? SwingUtils.getSystemShortcutModifier () : 0 ) |
            ( isAlt ? KeyEvent.ALT_MASK : 0 ) | ( isShift ? KeyEvent.SHIFT_MASK : 0 );
}
 
Example 12
Source File: ExecuteTestAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** creates new ExecuteTestAction instance */
public ExecuteTestAction() {
    super(MENU, MENU, new Action.Shortcut(KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK, KeyEvent.VK_L));
}
 
Example 13
Source File: Catbert.java    From sagetv with Apache License 2.0 4 votes vote down vote up
public static int[] getKeystrokeFromString(String s)
{
  if (s == null || s.length() == 0)
    return new int[] { 0, 0 };
  if ("+".equals(s))
    return new int[] { KeyEvent.VK_PLUS, 0 };
  int code = 0;
  int mods = 0;
  if (s.endsWith("+"))
  {
    code = KeyEvent.VK_PLUS;
    s = s.substring(0, s.length() - 1);
  }
  else
  {
    int lastPlus = s.lastIndexOf('+');
    String codestr;
    if (lastPlus != -1)
    {
      codestr = s.substring(lastPlus + 1);
      s = s.substring(0, lastPlus);
    }
    else
    {
      codestr = s;
      s = "";
    }
    Integer o = keystrokeNameMap.get(codestr);
    if (o != null)
    {
      code = o.intValue();
    }
    else
    {
      code = codestr.toUpperCase().charAt(0);
    }
  }
  String ctrlName = KeyEvent.getKeyModifiersText(KeyEvent.CTRL_MASK);
  String altName = KeyEvent.getKeyModifiersText(KeyEvent.ALT_MASK);
  String shiftName = KeyEvent.getKeyModifiersText(KeyEvent.SHIFT_MASK);
  String metaName = KeyEvent.getKeyModifiersText(KeyEvent.META_MASK);
  StringTokenizer toker = new StringTokenizer(s, "+");
  while (toker.hasMoreTokens())
  {
    String modName = toker.nextToken();
    if (modName.equalsIgnoreCase(ctrlName))
      mods = mods | KeyEvent.CTRL_MASK;
    else if (modName.equalsIgnoreCase(altName))
      mods = mods | KeyEvent.ALT_MASK;
    else if (modName.equalsIgnoreCase(shiftName))
      mods = mods | KeyEvent.SHIFT_MASK;
    else if (modName.equalsIgnoreCase(metaName))
      mods = mods | KeyEvent.META_MASK;
  }
  return new int[] { code, mods };
}
 
Example 14
Source File: KeyShortcut.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
public boolean isAlt() {
  return (this.modifiers & KeyEvent.ALT_MASK) != 0;
}
 
Example 15
Source File: PreferencesPanel.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
private int getScalingModifiers() {
  return (this.checkBoxScalingALT.isSelected() ? KeyEvent.ALT_MASK : 0)
          | (this.checkBoxScalingCTRL.isSelected() ? KeyEvent.CTRL_MASK : 0)
          | (this.checkBoxScalingMETA.isSelected() ? KeyEvent.ALT_MASK : 0)
          | (this.checkBoxScalingSHIFT.isSelected() ? KeyEvent.SHIFT_MASK : 0);
}
 
Example 16
Source File: MindMapSettingsPanel.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
public MindMapPanelConfig makeConfig() {
  final MindMapPanelConfig result = new MindMapPanelConfig(this.etalon, false);

  result.setPaperColor(this.colorButtonBackgroundColor.getValue());
  result.setGridColor(this.colorButtonGridColor.getValue());
  result.setCollapsatorBackgroundColor(this.colorButtonCollapsatorFill.getValue());
  result.setCollapsatorBorderColor(this.colorButtonCollapsatorBorder.getValue());
  result.setConnectorColor(this.colorButtonConnectorColor.getValue());
  result.setJumpLinkColor(this.colorButtonJumpLink.getValue());
  result.setSelectLineColor(this.colorButtonSelectFrameColor.getValue());

  result.setRootBackgroundColor(this.colorButtonRootFill.getValue());
  result.setRootTextColor(this.colorButtonRootText.getValue());
  result.setFirstLevelBackgroundColor(this.colorButton1stLevelFill.getValue());
  result.setFirstLevelTextColor(this.colorButton1stLevelText.getValue());
  result.setOtherLevelBackgroundColor(this.colorButton2ndLevelFill.getValue());
  result.setOtherLevelTextColor(this.colorButton2ndLevelText.getValue());

  result.setGridStep(getInt(this.spinnerGridStep));
  result.setConnectorWidth(getFloat(this.spinnerConnectorWidth));
  result.setCollapsatorSize(getInt(this.spinnerCollapsatorSize));
  result.setCollapsatorBorderWidth(getFloat(this.spinnerCollapsatorWidth));
  result.setJumpLinkWidth(getFloat(this.spinnerJumpLinkWidth));
  result.setSelectLineWidth(getFloat(this.spinnerSelectionFrameWidth));
  result.setSelectLineGap(getInt(this.spinnerSelectionFrameGap));
  result.setElementBorderWidth(getFloat(this.spinnerBorderWidth));

  result.setShowGrid(this.checkBoxShowGrid.isSelected());
  result.setDropShadow(this.checkBoxDropShadow.isSelected());

  result.setSmartTextPaste(this.checkBoxSmartTextPaste.isSelected());

  result.setFirstLevelHorizontalInset(this.slider1stLevelHorzGap.getValue());
  result.setFirstLevelVerticalInset(this.slider1stLevelVertGap.getValue());

  result.setOtherLevelHorizontalInset(this.slider2ndLevelHorzGap.getValue());
  result.setOtherLevelVerticalInset(this.slider2ndLevelVertGap.getValue());

  result.setRenderQuality(GetUtils.ensureNonNull((RenderQuality) comboBoxRenderQuality.getSelectedItem(), RenderQuality.DEFAULT));

  result.setFont(this.theFont);

  final int scaleModifier = (this.checkBoxScalingModifierALT.isSelected() ? KeyEvent.ALT_MASK : 0)
          | (this.checkBoxScalingModifierCTRL.isSelected() ? KeyEvent.CTRL_MASK : 0)
          | (this.checkBoxScalingModifierSHFT.isSelected() ? KeyEvent.SHIFT_MASK : 0)
          | (this.checkBoxScalingModifierMETA.isSelected() ? KeyEvent.META_MASK : 0);

  result.setScaleModifiers(scaleModifier);

  for (final Map.Entry<String, KeyShortcut> e : this.mapKeyShortCuts.entrySet()) {
    result.setKeyShortCut(e.getValue());
  }

  return result;
}
 
Example 17
Source File: CreateTestsAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** creates new CreateTestsAction instance */
public CreateTestsAction() {
    super(MENU, MENU, new Action.Shortcut(KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK, KeyEvent.VK_J));
}
 
Example 18
Source File: MenuBuilder.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
public int getModifier() {
    return (shiftDown ? KeyEvent.SHIFT_MASK : 0) + (ctrlDown ? KeyEvent.CTRL_MASK : 0) + (altDown ? KeyEvent.ALT_MASK : 0);
}
 
Example 19
Source File: OpenTestAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** creates new OpenTestAction instance */
public OpenTestAction() {
    super(MENU, MENU, new Action.Shortcut(KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK, KeyEvent.VK_K));
}
 
Example 20
Source File: Utilities.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/** Reads for modifiers and creates integer with required mask.
* @param s string with modifiers
* @return integer with mask
* @exception NoSuchElementException if some letter is not modifier
*/
private static int readModifiers(String s) throws NoSuchElementException {
    int m = 0;

    for (int i = 0; i < s.length(); i++) {
        switch (s.charAt(i)) {
        case 'C':
            m |= KeyEvent.CTRL_MASK;

            break;

        case 'A':
            m |= KeyEvent.ALT_MASK;

            break;

        case 'M':
            m |= KeyEvent.META_MASK;

            break;

        case 'S':
            m |= KeyEvent.SHIFT_MASK;

            break;

        case 'D':
            m |= CTRL_WILDCARD_MASK;

            break;

        case 'O':
            m |= ALT_WILDCARD_MASK;

            break;

        default:
            throw new NoSuchElementException(s);
        }
    }

    return m;
}