@witchcraft/spellcraft
    Preparing search index...

    Class Emulator

    A simple key emulator for testing purposes. Should be hooked up to a manager class as if it was an element:

    const emulator = new Emulator()
    manager.attach(emulator)
    // press and release a
    emulator.fire("KeyA")
    // in case the pressed keys need to be cleared you can use the manager to do so:
    manager.clear()
    Index

    Constructors

    Properties

    initiated: boolean = false
    listeners: {
        keydown: AnyFunction[];
        keyup: AnyFunction[];
        mousedown: AnyFunction[];
        mouseenter: AnyFunction[];
        mouseup: AnyFunction[];
        wheel: AnyFunction[];
    } = ...
    validKeys?: Keys<Record<string, Key<string>>>

    Methods

    • Parameters

      • type: string
      • func: AnyFunction

      Returns void

    • Emulate pressing/releasing keys.

      // press and release a
      emulator.fire("KeyA")
      // hold A down
      emulator.fire("KeyA+")
      // release A
      emulator.fire("KeyA-")
      // press Ctrl+A
      emulator.fire("Ctrl+ KeyA Ctrl-")

      // to truly simulate pressing native modifiers, pass all modifiers pressed as an array
      emulator.fire("Ctrl+ KeyA Ctrl-", ["ControlLeft"])

      // emulate a modifier state change out of focus
      // e.g. user focuses out, holds control+A, and focuses back
      // only KeyA would fire
      emulator.fire("KeyA+", ["ControlLeft"])
      // they release both in focus
      emulater.fire("KeyA- Ctrl-")

      // to simulate them releasing out of focus again you will have to add a delay since no events would fire
      delay(1000)

      Keys should be a KeyboardEvent.code (though this is not validated) seperated by one or more whitespace characters*. For buttons 0-5 can be used. For wheel events, you can pass wheelUp/Down to set the deltaY respectively which is how the manager gets the direction.

      + and - are used to indicate keydown and keyup respectively (except for wheel events*). This can seem confusing but think of the signs as adding/removing from the set of currently held keys. If no +/- is given, both are fired.

      There is no need to handle toggle keys in any special way. You should fire the root code normally (e.g. emulator.fire("Capslock") or emulator.fire("Capslock+ Capslock-"))

      * Multiple whitespace characters have no real meaning, but in tests usually I use them to more easily delimite chords. ** Wheel events do not have keyup/keydown so passing wheelUp+/- will incorrectly create a keyboard event.

      Note: While the emulator is aware of correct mouse/wheel names, it does not check key names are valid.

      Parameters

      • str: string
      • modifiers: string[] = []
      • OptionalvalidKeys: Keys<Record<string, Key<string>>>

      Returns void

    • Parameters

      • modifiers: string[] = []

      Returns void

    • Parameters

      • type: "wheel" | "key" | "mouse"
      • key: string
      • modifiers: string[] = []
      • OptionalvalidKeys: Keys<Record<string, Key<string>>>

      Returns void

    • Parameters

      • type: "key" | "mouse"
      • key: string
      • modifiers: string[] = []
      • OptionalvalidKeys: Keys<Record<string, Key<string>>>

      Returns void

    • Parameters

      • type: string
      • func: AnyFunction

      Returns void