UNPKG

6.06 kBTypeScriptView Raw
1import { Disposable } from "../index";
2
3/**
4 * Allows commands to be associated with keystrokes in a context-sensitive way.
5 * In Atom, you can access a global instance of this object via `atom.keymaps`.
6 */
7export interface KeymapManager {
8 /** Clear all registered key bindings and enqueued keystrokes. For use in tests. */
9 clear(): void;
10
11 /** Unwatch all watched paths. */
12 destroy(): void;
13
14 // Event Subscription
15 /** Invoke the given callback when one or more keystrokes completely match a key binding. */
16 onDidMatchBinding(callback: (event: FullKeybindingMatchEvent) => void): Disposable;
17
18 /** Invoke the given callback when one or more keystrokes partially match a binding. */
19 onDidPartiallyMatchBindings(callback: (event: PartialKeybindingMatchEvent) => void): Disposable;
20
21 /** Invoke the given callback when one or more keystrokes fail to match any bindings. */
22 onDidFailToMatchBinding(callback: (event: FailedKeybindingMatchEvent) => void): Disposable;
23
24 /** Invoke the given callback when a keymap file is reloaded. */
25 onDidReloadKeymap(callback: (event: KeymapLoadedEvent) => void): Disposable;
26
27 /** Invoke the given callback when a keymap file is unloaded. */
28 onDidUnloadKeymap(callback: (event: KeymapLoadedEvent) => void): Disposable;
29
30 /** Invoke the given callback when a keymap file not able to be loaded. */
31 onDidFailToReadFile(callback: (error: FailedKeymapFileReadEvent) => void): Disposable;
32
33 // Adding and Removing Bindings
34 /** Construct KeyBindings from an object grouping them by CSS selector. */
35 build(source: string, bindings: { [key: string]: { [key: string]: string } }, priority?: number): KeyBinding[];
36
37 /** Add sets of key bindings grouped by CSS selector. */
38 add(source: string, bindings: { [key: string]: { [key: string]: string } }, priority?: number): Disposable;
39
40 // Accessing Bindings
41 /** Get all current key bindings. */
42 getKeyBindings(): KeyBinding[];
43
44 /** Get the key bindings for a given command and optional target. */
45 findKeyBindings(params?: {
46 keystrokes?: string | undefined; // e.g. 'ctrl-x ctrl-s'
47 command?: string | undefined; // e.g. 'editor:backspace'
48 target?: Element | undefined;
49 }): KeyBinding[];
50
51 // Managing Keymap Files
52 /** Load the key bindings from the given path. */
53 loadKeymap(bindingsPath: string, options?: { watch?: boolean | undefined; priority?: number | undefined }): void;
54
55 /**
56 * Cause the keymap to reload the key bindings file at the given path whenever
57 * it changes.
58 */
59 watchKeymap(filePath: string, options?: { priority: number }): void;
60
61 // Managing Keyboard Events
62 /**
63 * Dispatch a custom event associated with the matching key binding for the
64 * given `KeyboardEvent` if one can be found.
65 */
66 handleKeyboardEvent(event: KeyboardEvent): void;
67
68 /** Translates a keydown event to a keystroke string. */
69 keystrokeForKeyboardEvent(event: KeyboardEvent): string;
70
71 /** Customize translation of raw keyboard events to keystroke strings. */
72 addKeystrokeResolver(resolver: (event: AddedKeystrokeResolverEvent) => string): Disposable;
73
74 /**
75 * Get the number of milliseconds allowed before pending states caused by
76 * partial matches of multi-keystroke bindings are terminated.
77 */
78 getPartialMatchTimeout(): number;
79}
80
81export interface FullKeybindingMatchEvent {
82 /** The string of keystrokes that matched the binding. */
83 keystrokes: string;
84
85 /** The KeyBinding that the keystrokes matched. */
86 binding: KeyBinding;
87
88 /** The DOM element that was the target of the most recent keyboard event. */
89 keyboardEventTarget: Element;
90}
91
92export interface KeyBinding {
93 // Properties
94 enabled: boolean;
95 source: string;
96 command: string;
97 keystrokes: string;
98 keystrokeArray: string[];
99 keystrokeCount: number;
100 selector: string;
101 specificity: number;
102
103 // Comparison
104 /** Determines whether the given keystroke matches any contained within this binding. */
105 matches(keystroke: string): boolean;
106
107 /**
108 * Compare another KeyBinding to this instance.
109 * Returns <= -1 if the argument is considered lesser or of lower priority.
110 * Returns 0 if this binding is equivalent to the argument.
111 * Returns >= 1 if the argument is considered greater or of higher priority.
112 */
113 compare(other: KeyBinding): number;
114}
115
116export interface PartialKeybindingMatchEvent {
117 /** The string of keystrokes that matched the binding. */
118 keystrokes: string;
119
120 /** The KeyBindings that the keystrokes partially matched. */
121 partiallyMatchedBindings: KeyBinding[];
122
123 /** DOM element that was the target of the most recent keyboard event. */
124 keyboardEventTarget: Element;
125}
126
127export interface AddedKeystrokeResolverEvent {
128 /**
129 * The currently resolved keystroke string. If your function returns a falsy
130 * value, this is how Atom will resolve your keystroke.
131 */
132 keystroke: string;
133
134 /**
135 * The raw DOM 3 `KeyboardEvent` being resolved. See the DOM API documentation
136 * for more details.
137 */
138 event: KeyboardEvent;
139
140 /** The OS-specific name of the current keyboard layout. */
141 layoutName: string;
142
143 /**
144 * An object mapping DOM 3 `KeyboardEvent.code` values to objects with the
145 * typed character for that key in each modifier state, based on the current
146 * operating system layout.
147 */
148 keymap: object;
149}
150
151export interface FailedKeybindingMatchEvent {
152 /** The string of keystrokes that failed to match the binding. */
153 keystrokes: string;
154
155 /** The DOM element that was the target of the most recent keyboard event. */
156 keyboardEventTarget: Element;
157}
158
159export interface FailedKeymapFileReadEvent {
160 /** The error message. */
161 message: string;
162
163 /** The error stack trace. */
164 stack: string;
165}
166
167export interface KeymapLoadedEvent {
168 /** The path of the keymap file. */
169 path: string;
170}