Reads for modifiers and creates integer with required mask : Key Event « Event « Java

Reads for modifiers and creates integer with required mask


import java.awt.event.KeyEvent;
import java.util.NoSuchElementException;

 * $Id:,v 1.11 2008/10/14 22:31:46 rah003 Exp $
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 * Contribution from NetBeans: Issue #319-swingx.
 * <p>
 * PENDING: need to reconcile with OS, JVM... added as-is because needed the
 * shortcut handling to fix #
 * @author apple
public class Utils {

  private static final int CTRL_WILDCARD_MASK = 32768;
  private static final int ALT_WILDCARD_MASK = CTRL_WILDCARD_MASK * 2;
  /** 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;

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

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

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

          case 'D':
              m |= CTRL_WILDCARD_MASK;

          case 'O':
              m |= ALT_WILDCARD_MASK;

              throw new NoSuchElementException(s);

      return m;


Related examples in the same category

1.KeyListener, ActionListener Demo 1KeyListener, ActionListener Demo 1
2.KeyListener, ActionListener Demo 2KeyListener, ActionListener Demo 2
3.Key event on frameKey event on frame
4.Responding to KeystrokesResponding to Keystrokes
5.Key Event DemoKey Event Demo
6.Set java.awt.Container.getFocusTraversalKeys(int id)
8.Activating a Keystroke When Any Component in the Window Has Focus
9.InputMap javax.swing.JComponent.getInputMap(int condition)
10.Activating a Keystroke When Any Child Component Has Focus
11.Listening to All Key Events Before Delivery to Focused Component
12.List keystrokes in the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map of the component
13.List keystrokes in the WHEN_IN_FOCUSED_WINDOW input map of the component
14.Converting a KeyStroke to a String
15.Creating a KeyStroke and Binding It to an Action
16.void InputMap.put(KeyStroke keyStroke, Object actionMapKey)
17.Map actions with keystrokes
18.Handling Key Presses
19.Get key pressed as a key character (which is a Unicode character)
20.Get key pressed as a key code
21.Get Key Text
22.KeyStroke to String
23.Construct a new key description from a given universal string descriptionConstruct a new key description from a given universal string description