| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {atom} from 'jotai'; |
| b69ab31 | | | 9 | import {writeAtom} from '../jotaiUtils'; |
| b69ab31 | | | 10 | import {registerCleanup} from '../utils'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | /** Subset of KeyboardEvent. */ |
| b69ab31 | | | 13 | export type KeyPress = { |
| b69ab31 | | | 14 | altKey?: boolean; |
| b69ab31 | | | 15 | ctrlKey?: boolean; |
| b69ab31 | | | 16 | shiftKey?: boolean; |
| b69ab31 | | | 17 | metaKey?: boolean; |
| b69ab31 | | | 18 | isComposing?: boolean; |
| b69ab31 | | | 19 | }; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /** State of if modified keys (alt, ctrl, etc) are currently pressed. */ |
| b69ab31 | | | 22 | export const keyPressAtom = atom<KeyPress>({}); |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | const keyChange = (e: KeyboardEvent) => { |
| b69ab31 | | | 25 | const {altKey, ctrlKey, shiftKey, metaKey, isComposing} = e; |
| b69ab31 | | | 26 | writeAtom(keyPressAtom, {altKey, ctrlKey, shiftKey, metaKey, isComposing}); |
| b69ab31 | | | 27 | }; |
| b69ab31 | | | 28 | document.addEventListener('keydown', keyChange); |
| b69ab31 | | | 29 | document.addEventListener('keyup', keyChange); |
| b69ab31 | | | 30 | const onBlur = () => { |
| b69ab31 | | | 31 | // reset all pressed keys when ISL is blurred, to prevent stuck key state from vscode shortcuts |
| b69ab31 | | | 32 | writeAtom(keyPressAtom, {}); |
| b69ab31 | | | 33 | }; |
| b69ab31 | | | 34 | window.addEventListener('blur', onBlur); |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | registerCleanup( |
| b69ab31 | | | 37 | keyPressAtom, |
| b69ab31 | | | 38 | () => { |
| b69ab31 | | | 39 | document.removeEventListener('keydown', keyChange); |
| b69ab31 | | | 40 | document.removeEventListener('keyup', keyChange); |
| b69ab31 | | | 41 | window.removeEventListener('blur', onBlur); |
| b69ab31 | | | 42 | }, |
| b69ab31 | | | 43 | import.meta.hot, |
| b69ab31 | | | 44 | ); |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | /** Is the alt key currently held down. */ |
| b69ab31 | | | 47 | export const holdingAltAtom = atom<boolean>(get => get(keyPressAtom).altKey ?? false); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | /** Is the ctrl key currently held down. */ |
| b69ab31 | | | 50 | export const holdingCtrlAtom = atom<boolean>(get => get(keyPressAtom).ctrlKey ?? false); |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | /** Is the meta ("Command" on macOS, or "Windows" on Windows) key currently held down. */ |
| b69ab31 | | | 53 | export const holdingMetaKey = atom<boolean>(get => get(keyPressAtom).metaKey ?? false); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | /** Is the shift key currently held down. */ |
| b69ab31 | | | 56 | export const holdingShiftAtom = atom<boolean>(get => get(keyPressAtom).shiftKey ?? false); |