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 20export class NotSupportedError extends Error {} 21 22// Fetch the user's keyboard layout map. 23// This function is merely a wrapper around the keyboard API, which throws a 24// specific error when used in browsers that don't support it. 25export async function nativeKeyboardLayoutMap(): Promise<KeyboardLayoutMap> { 26 // Browser's that don't support the Keyboard API won't have a keyboard 27 // property in their window.navigator object. 28 // Note: it seems this is also what Chrome does when the website is accessed 29 // through an insecure connection. 30 if ('keyboard' in window.navigator) { 31 // Typescript's dom library doesn't know about this feature, so we must 32 // take some liberties when it comes to relaxing types 33 const keyboard = (window.navigator as any).keyboard; 34 return await keyboard.getLayoutMap(); 35 } else { 36 throw new NotSupportedError('Keyboard API is not supported'); 37 } 38} 39