Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Key

One and only one instance of a Key object exists per set of parameters used for its construction. So if a = new Key(1, 2, 3) and b = new Key(1, 2, 3) then a === b is true. The last three parameters are normalized to boolean values, so new Key(1, 2, 3) is the same as new Key(1, 2, 3, false, false, false).

Key objects should be considered immutable. Modifying them after their creation is likely to cause code to execute erratically.

A note on the handling of the shift key. For key presses, we do not care whether shift was held or not when the key was pressed. It does not matter to us whether the user types the letter A because "Shift-a" was pressed or because the user was in caps lock mode and pressed "a". Conversely, keydown and keyup events concern themselves with Shift. We do want to distinguish Ctrl-A and Ctrl-Shift-A. (Yes, we use the capital A for both: browsers report that the key "A" was pressed whether Shift was held or not.)

Hierarchy

  • Key

Index

Constructors

Private constructor

  • new Key(hashKey: string, which: number, keypress?: boolean, keyCode: number, charCode?: number, ctrlKey?: boolean, altKey?: boolean, metaKey?: boolean, shiftKey?: TriValued): Key
  • Parameters

    • hashKey: string

      The unique hash which represents this key.

    • which: number

      The character code of the key.

    • Default value keypress: boolean = true

      Whether this key is meant to be used for keypress events rather than keyup and keydown.

    • keyCode: number

      The key code of the key.

    • Default value charCode: number = 0

      The character code of the key.

    • Default value ctrlKey: boolean = false

      Whether this key requires the Ctrl key held.

    • Default value altKey: boolean = false

      Whether this key requires the Alt key held.

    • Default value metaKey: boolean = false

      Whether this key requires the meta key held.

    • Default value shiftKey: TriValued = EITHER

      Whether this key requires the shift key held. It is invalid to use this parameter if keypress is true. When keypress is false, an unspecified value here means false.

    Returns Key

Properties

altKey

altKey: boolean

charCode

charCode: number

ctrlKey

ctrlKey: boolean

hashKey

hashKey: string

Private id

id: number

keyCode

keyCode: number

keypress

keypress: boolean

metaKey

metaKey: boolean

shiftKey

shiftKey: TriValued

which

which: number

Static Private __cache

__cache: Record<string, Key> = Object.create(null)

Methods

anyModifier

  • anyModifier(): boolean
  • Returns boolean

    True if any modifiers are turned on for this key. False otherwise. Shift is not considered a modifier for our purposes.

hash

  • hash(): number
  • The uniqueness of the return value this method returns is guaranteed only per module instance, which generally translates to "per JavaScript execution context". For instance, if this code is loaded in two different browser pages, the module will be instantiated once per page and the return values for Key objects that were created with the same parameters might differ. So if these two pages communicate with one another they cannot use the return value of this method to identify objects.

    Returns number

    A hash value that uniquely identifies the object. The value should be considered to be opaque.

matchesEvent

  • matchesEvent(ev: KeyboardEvent | JQueryKeyEventObject): boolean
  • This method compares the key object to an event object. The event object should have been generated for a keyboard event. This method does not check the type of object.

    Parameters

    • ev: KeyboardEvent | JQueryKeyEventObject

      A jQuery or DOM event object.

    Returns boolean

    True if the key object matches the event, false otherwise.

setEventToMatch

  • setEventToMatch(ev: KeyboardEvent | JQueryKeyEventObject): void
  • Sets an event object so that it matches this key. If this is not a keypress event, the event type will be set to keydown. The caller can set it to keyup as needed.

    Parameters

    • ev: KeyboardEvent | JQueryKeyEventObject

      A jQuery or DOM event object. This object is modified by the method.

    Returns void

Static make

  • make(which: number, keypress?: boolean, keyCode: number, charCode?: number, ctrlKey?: boolean, altKey?: boolean, metaKey?: boolean, shiftKey?: TriValued): Key
  • Client code should use the convenience functions provided by this module to create keys rather than use this function directly.

    Parameters

    • which: number

      The character code of the key.

    • Default value keypress: boolean = true

      Whether this key is meant to be used for keypress events rather than keyup and keydown.

    • keyCode: number

      The key code of the key.

    • Default value charCode: number = 0

      The character code of the key.

    • Default value ctrlKey: boolean = false

      Whether this key requires the Ctrl key held.

    • Default value altKey: boolean = false

      Whether this key requires the Alt key held.

    • Default value metaKey: boolean = false

      Whether this key requires the meta key held.

    • Default value shiftKey: TriValued = EITHER

      Whether this key requires the shift key held. It is invalid to use this parameter if keypress is true. When keypress is false, an unspecified value here means false.

    Returns Key

    The key corresponding to the parameters.

Generated using TypeDoc