Class GlobalHotkeys

java.lang.Object
com.codename1.desktop.GlobalHotkeys

public final class GlobalHotkeys extends Object

Keyboard shortcuts that fire while the application runs. Where the platform allows it the shortcut is system wide and fires while another application has the focus; #isGlobal() says which. The Java SE desktop build registers application wide shortcuts that fire while any of its windows has the focus. Mobile platforms answer false from #isSupported().

if (GlobalHotkeys.isSupported()) {
    int id = GlobalHotkeys.register('K', GlobalHotkeys.MOD_CTRL | GlobalHotkeys.MOD_SHIFT,
            new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    Log.p("Hotkey pressed");
                }
            });
    Log.p("Registered " + id + ", global " + GlobalHotkeys.isGlobal());
}
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The Alt (Option) key.
    static final int
    The Control key.
    static final int
    The Command (Windows, Super) key.
    static final int
    The Shift key.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    fireHotkey(int id)
    Called by the port on the EDT when a shortcut is pressed.
    static boolean
    True when shortcuts fire while another application has the focus.
    static boolean
    True when shortcuts can be registered.
    static int
    register(char key, int modifiers, ActionListener listener)
    Registers a shortcut.
    static void
    setImplementation(com.codename1.impl.CodenameOneImplementation impl)
    Invoked internally from Display, this method is for internal use only
    static void
    unregister(int id)
    Removes a shortcut.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • setImplementation

      public static void setImplementation(com.codename1.impl.CodenameOneImplementation impl)

      Invoked internally from Display, this method is for internal use only

      Parameters
      • impl: implementation instance
    • isSupported

      public static boolean isSupported()

      True when shortcuts can be registered.

      Returns

      true on a desktop platform

    • isGlobal

      public static boolean isGlobal()

      True when shortcuts fire while another application has the focus.

      Returns

      true for system wide shortcuts

    • register

      public static int register(char key, int modifiers, ActionListener listener)

      Registers a shortcut.

      Parameters
      • key: an ASCII letter or digit; letters are matched without case
      • modifiers: a mask of the MOD_ constants, at least one
      • listener: called on the EDT when the shortcut is pressed
      Returns

      an identifier for #unregister(int), or -1 when the platform did not register it

    • unregister

      public static void unregister(int id)

      Removes a shortcut.

      Parameters
      • id: the identifier #register(char, int, ActionListener) answered
    • fireHotkey

      public static void fireHotkey(int id)

      Called by the port on the EDT when a shortcut is pressed. Not meant for application code.

      Parameters
      • id: the identifier the shortcut was registered with