import { EventDispatcher } from '@thegraid/easeljs-module'; type KeyBits = { shift?: boolean; ctrl?: boolean; meta?: boolean; alt?: boolean; keyup?: boolean; }; /** key code, ascii value */ type CharCode = number; /** CharCode + CMAS bits added in */ type KeyCode = number; /** formatted 'C-M-A-S-c' or maybe 'Escape' ? */ type KeyStr = string; /** 'Alt' | 'Escape' | 'Return' ... */ type KeyName = string; /** use arrow function for Binding */ type kFunc = ((...args: any[]) => any); /** unless BindFunc returns true, then e.preventDefault() */ export type BindFunc = (arg?: any, key?: KeyStr) => boolean | void; export type KeyScope = { keymap?: Keymap; lastFunc?: BindFunc; onFocus?: (f: boolean) => void; }; /** * kcode is bound to Binding, invokes: func.call(scope, argVal, event) */ export type Binding = { thisArg?: object; func: BindFunc; argVal?: any; _kcode?: number; }; export type Keymap = { [key: KeyCode]: Binding; scope?: KeyScope; regexs?: { regex: RegExp; bind: Binding; }[]; }; /** EventDispatcher with keybinding (key => thisArg.func(argVal, e)) * * dispatchEvent('Focus', new target) * * Listens for keyup,keydown events from browser/window. */ export declare class KeyBinder extends EventDispatcher implements KeyScope { static keyBinder: KeyBinder; static instanceId: number; private instanceId; constructor(multi?: boolean); /** global map from e.keyCode to Binding {thisArg, func: BindFunc, argval} */ keymap: Keymap; /** last BindFunc invoked from this keymap. */ lastFunc: BindFunc; /** nothing special for us, but notify any listener */ onFocus(focus: boolean): void; /** GLOBAL FOCUS: a keymap bound to a KeyScope object */ private focus; /** * Convert shift indicators to binary-mapped number. * Extend numeric keycode with SHIFT, CTRL, META, ALT, KEYUP bits * @param kcode unicode(char) * @param bits KeyBits * @returns kcode with higher order bits possibly set */ getKeyCode(kcode: CharCode, bits?: KeyBits): KeyCode; /** return "^-C-M-A-S-keyname" for given keycode */ keyCodeToString(keycode: KeyCode): KeyStr; /** * Parse keyStr to create KeyCode (a number, before rewrite) * * setting modifier values for key binding: keyup-ctrl-meta-alt-shift char * x or X for shift. * @param keyStr "^-C-M-A-X" or "^-C-M-A-x" OR e.key/e.code(keyname) * @return keyCode of "X" with indicated shift-bits added in. * * TODO: type KeyCode = string; KeyMap: Map */ getKeyCodeFromChar(keyStr: KeyStr): KeyCode; /** event.key name -> numeric kcode. */ static keyCode(key: string): CharCode | undefined; /** event.code name -> numeric kcode. */ static codeCode(code: KeyName): CharCode | undefined; static codeKey(kcode: KeyCode): KeyStr; static key_keyCode: { [index: KeyName]: KeyCode; }; static keyEvent(key: string, code: string, keyCode: number, shiftKey: boolean, ctrlKey: boolean, metaKey: boolean, altKey: boolean, keyup: boolean): KeyboardEvent; /** get our numeric encoding of the key in the KeyboardEvent. */ getKeyCodeFromEvent(e: KeyboardEvent): number; /** * Bind (or unbind) kcode in the given keymap. * @param keymap * @param kcode * @param bind * @returns */ _bindKey(keymap: Keymap, kcode: KeyCode, bind: Binding): KeyCode; /** bindRegexp is constrained to match only on 'plain' (non-chord) alpha keys */ _bindRegex(keymap: Keymap, regex: RegExp, bind: Binding): RegExp; globalSetKey(kcode: KeyCode, bind: Binding): KeyCode; /** * Bind key to event handling function. * @param str a string describing a single keychord. * @param bind a Binding {thisArg: keymap, func: ()=>void, argVal?: arg} */ globalSetKeyFromChar(str: KeyStr, bind: Binding): KeyCode; localSetKeyFromChar(scope: KeyScope, str: KeyStr, bind: Binding): number; /** * Set key to invoke Binding in given KeyScope. * * Non-global KeyScope is used when selected by keyBinder.setFocus() * @param key a key code, string, or regexp * @param bind Binding or an [arrow] function (gets wrapped in a Binding) * @param scope a KeyScope */ setKey(key: number | string | RegExp, bind: Binding | kFunc, scope?: KeyScope): number | RegExp; getKeymap(scope: KeyScope): Keymap; /** dispatch keyup and keydown events [keypress dispatches to keydown, the default] * KeyboardEvent is HTML Event, so current/target path is: window, document, html, body * - Useless for 'local' bindings; we need a 'focus' on DisplayObject, and target to that. * - Text/EditBox can listen for 'click' and set focus. (currentTarget) * - Text/EditBox can listen for 'Enter' and release focus (blur) [also: onCollapse of menu] */ dispatchKey(e: KeyboardEvent): boolean; /** invoke keyBound function AS IF kcode(str) was received. * @param str KeyStr like "^-C-M-z" */ dispatchChar(str: KeyStr): boolean; /** * Dispatch based on KeyCode. * @param kcode extracted from KeyboardEvent, or synthesized from getKeyCodeFromChar(str) * @param e either a KeyboardEvent with .key, or the equivalent .key string */ dispatchKeyCode(kcode: KeyCode, keyStr?: KeyStr, e?: KeyboardEvent): boolean; showBindings(keymap: Keymap): string[]; /** use _keymap of the given target (or global if not supplied) */ setFocus(target?: KeyScope): void; /** if true log details of event dispatching to console.log */ details: boolean; private initListeners; showKeyEvent(str: KeyStr, e: KeyboardEvent): void; } export {};