java.lang.Object
java.util.EventObject
com.github.kwhat.jnativehook.NativeInputEvent
com.github.kwhat.jnativehook.keyboard.NativeKeyEvent
All Implemented Interfaces:
Serializable

public class NativeKeyEvent extends NativeInputEvent
An event which indicates that a keystroke occurred at global scope.

This low-level event is generated by the native system when a key is pressed or released independent of Java component focus. The event is passed to every NativeKeyListener object which has been registered to receive global key events using the GlobalScreen.addNativeKeyListener(NativeKeyListener) method. Each NativeKeyListener object will receive a NativeKeyEvent when the event occurs.

All NativeKeyEvent objects are dependent on the native platform and keyboard layout. NATIVE_KEY_PRESSED and NATIVE_KEY_RELEASED events are generated for every key code received by the native system. The key being pressed or released is indicated by the getKeyCode method, which returns a virtual key code or VC_UNDEFINED. Please note that getKeyChar() for events of type NATIVE_KEY_PRESSED and NATIVE_KEY_RELEASED will always return CHAR_UNDEFINED.

NATIVE_KEY_TYPED events are produced for NATIVE_KEY_PRESSED events that produce valid Unicode characters for the current keyboard layout. The getKeyChar() method always returns a valid Unicode character for this type of event. Please note that getKeyCode() will always return VC_UNDEFINED for NATIVE_KEY_TYPED events.

Virtual key codes only represent the physical key that has been pressed and should not be mistaken with the character mapped to that key by the operating system. NATIVE_KEY_PRESSED and NATIVE_KEY_RELEASED events should only be used to determin phisical key state, while NATIVE_KEY_TYPED events can be used to determine the Unicode representation of the NativeKeyEvent.

Since:
1.0
Version:
2.0
Author:
Alexander Barker (alex@1stleg.com)
See Also:
  • Field Details

  • Constructor Details

    • NativeKeyEvent

      public NativeKeyEvent(int id, int modifiers, int rawCode, int keyCode, char keyChar, int keyLocation)
      Instantiates a new native key event.

      Note that passing in an invalid ID results in unspecified behavior.

      Parameters:
      id - an integer that identifies the native event type.
      modifiers - the modifier mask for the native event. NativeInputEvent _MASK modifiers should be used as they are not compatible with AWT's InputEvent _DOWN_MASK or the older _MASK modifiers.
      rawCode - the hardware code associated with the native key in this event.
      keyCode - the virtual key code generated by this event, or VC_UNDEFINED (for a key-typed event)
      keyChar - the Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character).
      keyLocation - the location ID of the key generating this event.
      Since:
      1.1
    • NativeKeyEvent

      public NativeKeyEvent(int id, int modifiers, int rawCode, int keyCode, char keyChar)
      Instantiates a new native key event.

      Note that passing in an invalid ID results in unspecified behavior.

      Parameters:
      id - an integer that identifies the native event type.
      modifiers - the modifier mask for the native event. NativeInputEvent _MASK modifiers should be used as they are not compatible with AWT's InputEvent _DOWN_MASK or the older _MASK modifiers.
      rawCode - the hardware code associated with the native key in this event.
      keyCode - the virtual key code generated by this event, or VC_UNDEFINED (for a key-typed event)
      keyChar - the Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character).
      Since:
      1.1
  • Method Details

    • getRawCode

      public int getRawCode()
      Returns the native code associated with the native key in this event. This is an arbitrary number between 0 and 255 (inclusive) used by the operating system to represent a physical key on the keyboard. This code does not necessarily represent the native key's scan code or ASCII representation. To maintain platform independence, you should not rely on the consistency of this value from platform to platform.
      Returns:
      the native key code for this event.
    • setRawCode

      public void setRawCode(int rawCode)
      Set the rawCode value in this event. For more information on what this value represents, please see getRawCode().
      Parameters:
      rawCode - the native key code for this event.
    • getKeyCode

      public int getKeyCode()
      Returns the keyCode associated with this event. Note, this method will always return VC_UNDEFINED for the NATIVE_KEY_TYPED event types.
      Returns:
      the native virtual key code.
    • setKeyCode

      public void setKeyCode(int keyCode)
      Set the keyCode value in this event.
      Parameters:
      keyCode - the native virtual key code.
    • getKeyChar

      public char getKeyChar()
      Returns the native Unicode character associated with this event. Note, this method will always return CHAR_UNDEFINED for NATIVE_KEY_PRESSED and NATIVE_KEY_RELEASED event types.
      Returns:
      the Unicode character defined for this key event. If no valid Unicode character exists for this key event, CHAR_UNDEFINED is returned.
    • setKeyChar

      public void setKeyChar(char keyChar)
      Set the keyChar value in this event. For example, the NATIVE_KEY_TYPED event for Shift + "a" returns the Unicode value 'A'.
      Parameters:
      keyChar - the keyboard character associated with this event.
    • getKeyLocation

      public int getKeyLocation()
      Returns the location of the virtual key for this event.
      Returns:
      the location of the virtual key that was pressed or released.
    • getKeyText

      public static String getKeyText(int keyCode)
      Returns a String describing the keyCode, such as "HOME", "F1" or "A". These strings can be localized by changing the awt.properties file.
      Parameters:
      keyCode - the native virtual key code generated by this event
      Returns:
      a string containing a text description for a physical key, identified by its keyCode.
    • isActionKey

      public boolean isActionKey()
      Returns whether the key in this event is an "action" key. Typically, an action key does not fire a Unicode character and is not a modifier key.
      Returns:
      true if the key is an "action" key, false otherwise.
      Since:
      1.1
    • paramString

      public String paramString()
      Returns a parameter string identifying this event. This method is useful for event logging and debugging.
      Overrides:
      paramString in class NativeInputEvent
      Returns:
      a string identifying the event and its attributes.