UNPKG

4.42 kBTypeScriptView Raw
1/**
2 * An object which represents an abstract keyboard layout.
3 */
4export interface IKeyboardLayout {
5 /**
6 * The human readable name of the layout.
7 *
8 * This value is used primarily for display and debugging purposes.
9 */
10 readonly name: string;
11 /**
12 * Get an array of all key values supported by the layout.
13 *
14 * @returns A new array of the supported key values.
15 *
16 * #### Notes
17 * This can be useful for authoring tools and debugging, when it's
18 * necessary to know which keys are available for shortcut use.
19 */
20 keys(): string[];
21 /**
22 * Test whether the given key is a valid value for the layout.
23 *
24 * @param key - The user provided key to test for validity.
25 *
26 * @returns `true` if the key is valid, `false` otherwise.
27 */
28 isValidKey(key: string): boolean;
29 /**
30 * Get the key for a `'keydown'` event.
31 *
32 * @param event - The event object for a `'keydown'` event.
33 *
34 * @returns The associated key value, or an empty string if the event
35 * does not represent a valid primary key.
36 */
37 keyForKeydownEvent(event: KeyboardEvent): string;
38}
39/**
40 * Get the global application keyboard layout instance.
41 *
42 * @returns The keyboard layout for use by the application.
43 *
44 * #### Notes
45 * The default keyboard layout is US-English.
46 */
47export declare function getKeyboardLayout(): IKeyboardLayout;
48/**
49 * Set the global application keyboard layout instance.
50 *
51 * @param - The keyboard layout for use by the application.
52 *
53 * #### Notes
54 * The keyboard layout should typically be set on application startup
55 * to a layout which is appropriate for the user's system.
56 */
57export declare function setKeyboardLayout(layout: IKeyboardLayout): void;
58/**
59 * A concrete implementation of [[IKeyboardLayout]] based on keycodes.
60 *
61 * The `keyCode` property of a `'keydown'` event is a browser and OS
62 * specific representation of the physical key (not character) which
63 * was pressed on a keyboard. While not the most convenient API, it
64 * is currently the only one which works reliably on all browsers.
65 *
66 * This class accepts a user-defined mapping of keycode to key, which
67 * allows for reliable shortcuts tailored to the user's system.
68 */
69export declare class KeycodeLayout implements IKeyboardLayout {
70 /**
71 * Construct a new keycode layout.
72 *
73 * @param name - The human readable name for the layout.
74 *
75 * @param codes - A mapping of keycode to key value.
76 */
77 constructor(name: string, codes: KeycodeLayout.CodeMap);
78 /**
79 * The human readable name of the layout.
80 */
81 readonly name: string;
82 /**
83 * Get an array of the key values supported by the layout.
84 *
85 * @returns A new array of the supported key values.
86 */
87 keys(): string[];
88 /**
89 * Test whether the given key is a valid value for the layout.
90 *
91 * @param key - The user provided key to test for validity.
92 *
93 * @returns `true` if the key is valid, `false` otherwise.
94 */
95 isValidKey(key: string): boolean;
96 /**
97 * Get the key for a `'keydown'` event.
98 *
99 * @param event - The event object for a `'keydown'` event.
100 *
101 * @returns The associated key value, or an empty string if
102 * the event does not represent a valid primary key.
103 */
104 keyForKeydownEvent(event: KeyboardEvent): string;
105 private _keys;
106 private _codes;
107}
108/**
109 * The namespace for the `KeycodeLayout` class statics.
110 */
111export declare namespace KeycodeLayout {
112 /**
113 * A type alias for a keycode map.
114 */
115 type CodeMap = {
116 readonly [code: number]: string;
117 };
118 /**
119 * A type alias for a key set.
120 */
121 type KeySet = {
122 readonly [key: string]: boolean;
123 };
124 /**
125 * Extract the set of keys from a code map.
126 *
127 * @param code - The code map of interest.
128 *
129 * @returns A set of the keys in the code map.
130 */
131 function extractKeys(codes: CodeMap): KeySet;
132}
133/**
134 * A keycode-based keyboard layout for US English keyboards.
135 *
136 * This layout is valid for the following OS/Browser combinations.
137 *
138 * - Windows
139 * - Chrome
140 * - Firefox
141 * - IE
142 *
143 * - OSX
144 * - Chrome
145 * - Firefox
146 * - Safari
147 *
148 * - Linux
149 * - Chrome
150 * - Firefox
151 *
152 * Other combinations may also work, but are untested.
153 */
154export declare const EN_US: IKeyboardLayout;