xref: /aosp_15_r20/external/perfetto/ui/src/frontend/keyboard_layout_map.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5//
6//      http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// A keyboard layout map that converts key codes to their equivalent glyphs for
15// a given keyboard layout (e.g. 'KeyX' -> 'x').
16export interface KeyboardLayoutMap {
17  get(code: string): string | undefined;
18}
19
20interface Keyboard {
21  getLayoutMap(): KeyboardLayoutMap;
22}
23
24export class NotSupportedError extends Error {}
25
26// Fetch the user's keyboard layout map.
27// This function is merely a wrapper around the keyboard API, which throws a
28// specific error when used in browsers that don't support it.
29export async function nativeKeyboardLayoutMap(): Promise<KeyboardLayoutMap> {
30  // Browser's that don't support the Keyboard API won't have a keyboard
31  // property in their window.navigator object.
32  // Note: it seems this is also what Chrome does when the website is accessed
33  // through an insecure connection.
34  if ('keyboard' in window.navigator) {
35    // Typescript's dom library doesn't know about this feature, so we must
36    // take some liberties when it comes to relaxing types
37    const keyboard = window.navigator.keyboard as Keyboard;
38    return await keyboard.getLayoutMap();
39  } else {
40    throw new NotSupportedError('Keyboard API is not supported');
41  }
42}
43