< prev index next >

src/java.desktop/share/classes/java/awt/MenuShortcut.java

Print this page




   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 

  27 import java.awt.event.KeyEvent;
  28 
  29 /**
  30  * The {@code MenuShortcut} class represents a keyboard accelerator
  31  * for a MenuItem.
  32  * <p>
  33  * Menu shortcuts are created using virtual keycodes, not characters.
  34  * For example, a menu shortcut for Ctrl-a (assuming that Control is
  35  * the accelerator key) would be created with code like the following:
  36  * <p>
  37  * {@code MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);}
  38  * <p> or alternatively
  39  * <p>
  40  * {@code MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('A'), false);}
  41  * <p>
  42  * Menu shortcuts may also be constructed for a wider set of keycodes
  43  * using the {@code java.awt.event.KeyEvent.getExtendedKeyCodeForChar} call.
  44  * For example, a menu shortcut for "Ctrl+cyrillic ef" is created by
  45  * <p>
  46  * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('\u0444'), false);</code>
  47  * <p>
  48  * Note that shortcuts created with a keycode or an extended keycode defined as a constant in {@code KeyEvent}
  49  * work regardless of the current keyboard layout. However, a shortcut made of
  50  * an extended keycode not listed in {@code KeyEvent}
  51  * only work if the current keyboard layout produces a corresponding letter.
  52  * <p>
  53  * The accelerator key is platform-dependent and may be obtained
  54  * via {@link Toolkit#getMenuShortcutKeyMask}.
  55  *
  56  * @author Thomas Ball
  57  * @since 1.1
  58  */
  59 public class MenuShortcut implements java.io.Serializable
  60 {
  61     /**
  62      * The virtual keycode for the menu shortcut.
  63      * This is the keycode with which the menu shortcut will be created.
  64      * Note that it is a virtual keycode, not a character,
  65      * e.g. KeyEvent.VK_A, not 'a'.
  66      * Note: in 1.1.x you must use setActionCommand() on a menu item
  67      * in order for its shortcut to work, otherwise it will fire a null
  68      * action command.
  69      *
  70      * @serial
  71      * @see #getKey()
  72      * @see #usesShiftModifier()
  73      * @see java.awt.event.KeyEvent
  74      * @since 1.1


 163         if (obj instanceof MenuShortcut) {
 164             return equals( (MenuShortcut) obj );
 165         }
 166         return false;
 167     }
 168 
 169     /**
 170      * Returns the hashcode for this MenuShortcut.
 171      * @return the hashcode for this MenuShortcut.
 172      * @since 1.2
 173      */
 174     public int hashCode() {
 175         return (usesShift) ? (~key) : key;
 176     }
 177 
 178     /**
 179      * Returns an internationalized description of the MenuShortcut.
 180      * @return a string representation of this MenuShortcut.
 181      * @since 1.1
 182      */
 183     @SuppressWarnings("deprecation")
 184     public String toString() {
 185         int modifiers = 0;
 186         if (!GraphicsEnvironment.isHeadless()) {
 187             modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
 188         }
 189         if (usesShiftModifier()) {
 190             modifiers |= Event.SHIFT_MASK;
 191         }
 192         return KeyEvent.getKeyModifiersText(modifiers) + "+" +
 193                KeyEvent.getKeyText(key);
 194     }
 195 
 196     /**
 197      * Returns the parameter string representing the state of this
 198      * MenuShortcut. This string is useful for debugging.
 199      * @return    the parameter string of this MenuShortcut.
 200      * @since 1.1
 201      */
 202     protected String paramString() {
 203         String str = "key=" + key;
 204         if (usesShiftModifier()) {
 205             str += ",usesShiftModifier";
 206         }
 207         return str;
 208     }
 209 }


   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.event.InputEvent;
  28 import java.awt.event.KeyEvent;
  29 
  30 /**
  31  * The {@code MenuShortcut} class represents a keyboard accelerator
  32  * for a MenuItem.
  33  * <p>
  34  * Menu shortcuts are created using virtual keycodes, not characters.
  35  * For example, a menu shortcut for Ctrl-a (assuming that Control is
  36  * the accelerator key) would be created with code like the following:
  37  * <p>
  38  * {@code MenuShortcut ms = new MenuShortcut(KeyEvent.VK_A, false);}
  39  * <p> or alternatively
  40  * <p>
  41  * {@code MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('A'), false);}
  42  * <p>
  43  * Menu shortcuts may also be constructed for a wider set of keycodes
  44  * using the {@code java.awt.event.KeyEvent.getExtendedKeyCodeForChar} call.
  45  * For example, a menu shortcut for "Ctrl+cyrillic ef" is created by
  46  * <p>
  47  * <code>MenuShortcut ms = new MenuShortcut(KeyEvent.getExtendedKeyCodeForChar('\u0444'), false);</code>
  48  * <p>
  49  * Note that shortcuts created with a keycode or an extended keycode defined as a constant in {@code KeyEvent}
  50  * work regardless of the current keyboard layout. However, a shortcut made of
  51  * an extended keycode not listed in {@code KeyEvent}
  52  * only work if the current keyboard layout produces a corresponding letter.
  53  * <p>
  54  * The accelerator key is platform-dependent and may be obtained
  55  * via {@link Toolkit#getMenuShortcutKeyMaskEx()}.
  56  *
  57  * @author Thomas Ball
  58  * @since 1.1
  59  */
  60 public class MenuShortcut implements java.io.Serializable
  61 {
  62     /**
  63      * The virtual keycode for the menu shortcut.
  64      * This is the keycode with which the menu shortcut will be created.
  65      * Note that it is a virtual keycode, not a character,
  66      * e.g. KeyEvent.VK_A, not 'a'.
  67      * Note: in 1.1.x you must use setActionCommand() on a menu item
  68      * in order for its shortcut to work, otherwise it will fire a null
  69      * action command.
  70      *
  71      * @serial
  72      * @see #getKey()
  73      * @see #usesShiftModifier()
  74      * @see java.awt.event.KeyEvent
  75      * @since 1.1


 164         if (obj instanceof MenuShortcut) {
 165             return equals( (MenuShortcut) obj );
 166         }
 167         return false;
 168     }
 169 
 170     /**
 171      * Returns the hashcode for this MenuShortcut.
 172      * @return the hashcode for this MenuShortcut.
 173      * @since 1.2
 174      */
 175     public int hashCode() {
 176         return (usesShift) ? (~key) : key;
 177     }
 178 
 179     /**
 180      * Returns an internationalized description of the MenuShortcut.
 181      * @return a string representation of this MenuShortcut.
 182      * @since 1.1
 183      */

 184     public String toString() {
 185         int modifiers = 0;
 186         if (!GraphicsEnvironment.isHeadless()) {
 187             modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
 188         }
 189         if (usesShiftModifier()) {
 190             modifiers |= InputEvent.SHIFT_DOWN_MASK;
 191         }
 192         return InputEvent.getModifiersExText(modifiers) + "+" +
 193                KeyEvent.getKeyText(key);
 194     }
 195 
 196     /**
 197      * Returns the parameter string representing the state of this
 198      * MenuShortcut. This string is useful for debugging.
 199      * @return    the parameter string of this MenuShortcut.
 200      * @since 1.1
 201      */
 202     protected String paramString() {
 203         String str = "key=" + key;
 204         if (usesShiftModifier()) {
 205             str += ",usesShiftModifier";
 206         }
 207         return str;
 208     }
 209 }
< prev index next >