Skip to content

Commit a4ce1b9

Browse files
committed
Restrict alt-based keyboard shortcuts to the left alt key
- only left alt should be used for shortcuts ├── apps │ └── editor │ └── src │ └── views │ └── prompt │ └── frontend │ ├── Home │ │ └── hooks │ │ └── use-keyboard-shortcuts.ts │ └── Main │ └── MainView │ └── components │ └── Header │ └── hooks │ └── use-keyboard-shortcuts.ts └── packages └── ui └── src └── components └── editor └── common └── prompts └── PromptField ├── PromptField.tsx └── hooks └── use-keyboard-shortcuts.ts
1 parent fdc9970 commit a4ce1b9

3 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎apps/editor/src/views/prompt/frontend/Home/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from 'react'
1+
import { useEffect, useRef } from 'react'
22

33
type Params = {
44
is_active: boolean
@@ -8,6 +8,8 @@ type Params = {
88
}
99

1010
export const use_keyboard_shortcuts = (params: Params) => {
11+
const left_alt_pressed_ref = useRef(false)
12+
1113
useEffect(() => {
1214
const handle_mouse_up = (event: MouseEvent) => {
1315
if (params.is_active && event.button == 4) {
@@ -16,9 +18,13 @@ export const use_keyboard_shortcuts = (params: Params) => {
1618
}
1719

1820
const handle_key_down = (event: KeyboardEvent) => {
21+
if (event.code == 'AltLeft') {
22+
left_alt_pressed_ref.current = true
23+
}
24+
1925
if (!params.is_active) return
2026

21-
if (event.altKey && !event.shiftKey && !event.metaKey && !event.ctrlKey) {
27+
if (event.altKey && left_alt_pressed_ref.current && !event.shiftKey && !event.metaKey && !event.ctrlKey) {
2228
if (event.code == 'Digit1') {
2329
event.preventDefault()
2430
params.on_chatbots_click()
@@ -29,11 +35,25 @@ export const use_keyboard_shortcuts = (params: Params) => {
2935
}
3036
}
3137

38+
const handle_key_up = (event: KeyboardEvent) => {
39+
if (event.code == 'AltLeft') {
40+
left_alt_pressed_ref.current = false
41+
}
42+
}
43+
44+
const handle_blur = () => {
45+
left_alt_pressed_ref.current = false
46+
}
47+
3248
window.addEventListener('mouseup', handle_mouse_up)
3349
window.addEventListener('keydown', handle_key_down)
50+
window.addEventListener('keyup', handle_key_up)
51+
window.addEventListener('blur', handle_blur)
3452
return () => {
3553
window.removeEventListener('mouseup', handle_mouse_up)
3654
window.removeEventListener('keydown', handle_key_down)
55+
window.removeEventListener('keyup', handle_key_up)
56+
window.removeEventListener('blur', handle_blur)
3757
}
3858
}, [
3959
params.is_active,

‎apps/editor/src/views/prompt/frontend/Main/MainView/components/Header/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const use_keyboard_shortcuts = (params: {
1212
const [is_alt_pressed, set_is_alt_pressed] = useState(false)
1313
const alt_interrupted_ref = useRef(false)
1414
const is_alt_pressed_raw_ref = useRef(false)
15+
const left_alt_pressed_ref = useRef(false)
1516

1617
const update_alt_pressed = (val: boolean) => {
1718
is_alt_pressed_raw_ref.current = val
@@ -20,8 +21,12 @@ export const use_keyboard_shortcuts = (params: {
2021

2122
useEffect(() => {
2223
const handle_key_down = (event: KeyboardEvent) => {
24+
if (event.code == 'AltLeft') {
25+
left_alt_pressed_ref.current = true
26+
}
27+
2328
if (
24-
event.key == 'Alt' &&
29+
event.code == 'AltLeft' &&
2530
!event.shiftKey &&
2631
!event.ctrlKey &&
2732
!event.metaKey
@@ -51,13 +56,18 @@ export const use_keyboard_shortcuts = (params: {
5156
}
5257

5358
const handle_key_up = (event: KeyboardEvent) => {
59+
if (event.code == 'AltLeft') {
60+
left_alt_pressed_ref.current = false
61+
}
62+
5463
if (!event.altKey) {
5564
alt_interrupted_ref.current = false
56-
} else if (event.key != 'Alt') {
65+
} else if (event.code != 'AltLeft') {
5766
alt_interrupted_ref.current = true
5867
}
5968
update_alt_pressed(
6069
event.altKey &&
70+
left_alt_pressed_ref.current &&
6171
!alt_interrupted_ref.current &&
6272
!event.shiftKey &&
6373
!event.ctrlKey &&
@@ -66,6 +76,7 @@ export const use_keyboard_shortcuts = (params: {
6676
}
6777

6878
const handle_blur = () => {
79+
left_alt_pressed_ref.current = false
6980
update_alt_pressed(false)
7081
alt_interrupted_ref.current = false
7182
}
@@ -95,7 +106,7 @@ export const use_keyboard_shortcuts = (params: {
95106
const handle_key_down = (event: KeyboardEvent) => {
96107
if (params.is_disabled) return
97108

98-
if (!event.altKey || event.shiftKey || event.metaKey || event.ctrlKey) {
109+
if (!event.altKey || !left_alt_pressed_ref.current || event.shiftKey || event.metaKey || event.ctrlKey) {
99110
return
100111
}
101112

‎packages/ui/src/components/editor/common/prompts/PromptField/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const use_keyboard_shortcuts = (props: PromptFieldProps) => {
66
const [is_alt_pressed, set_is_alt_pressed] = useState(false)
77
const is_alt_pressed_raw_ref = useRef(false)
88
const pending_edit_format_ref = useRef<EditFormat | null>(null)
9+
const left_alt_pressed_ref = useRef(false)
910

1011
const update_alt_pressed = (val: boolean) => {
1112
is_alt_pressed_raw_ref.current = val
@@ -27,8 +28,12 @@ export const use_keyboard_shortcuts = (props: PromptFieldProps) => {
2728

2829
useEffect(() => {
2930
const handle_key_down = (e: KeyboardEvent) => {
31+
if (e.code == 'AltLeft') {
32+
left_alt_pressed_ref.current = true
33+
}
34+
3035
let format: EditFormat | undefined
31-
if (e.altKey && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
36+
if (e.altKey && left_alt_pressed_ref.current && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
3237
if (e.code == 'Escape') {
3338
e.preventDefault()
3439
if (props.on_target_change) {
@@ -57,7 +62,7 @@ export const use_keyboard_shortcuts = (props: PromptFieldProps) => {
5762
}
5863
}
5964

60-
if (e.key == 'Alt' && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
65+
if (e.code == 'AltLeft' && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
6166
if (!alt_interrupted_ref.current) {
6267
update_alt_pressed(true)
6368
}
@@ -81,13 +86,17 @@ export const use_keyboard_shortcuts = (props: PromptFieldProps) => {
8186
}
8287
}
8388
const handle_key_up = (e: KeyboardEvent) => {
89+
if (e.code == 'AltLeft') {
90+
left_alt_pressed_ref.current = false
91+
}
8492
if (!e.altKey) {
8593
alt_interrupted_ref.current = false
86-
} else if (e.key != 'Alt') {
94+
} else if (e.code != 'AltLeft') {
8795
alt_interrupted_ref.current = true
8896
}
8997
update_alt_pressed(
9098
e.altKey &&
99+
left_alt_pressed_ref.current &&
91100
!alt_interrupted_ref.current &&
92101
!e.shiftKey &&
93102
!e.ctrlKey &&
@@ -97,6 +106,7 @@ export const use_keyboard_shortcuts = (props: PromptFieldProps) => {
97106
const handle_blur = () => {
98107
update_alt_pressed(false)
99108
alt_interrupted_ref.current = false
109+
left_alt_pressed_ref.current = false
100110
}
101111
window.addEventListener('keydown', handle_key_down)
102112
window.addEventListener('keyup', handle_key_up)
@@ -122,7 +132,7 @@ export const use_keyboard_shortcuts = (props: PromptFieldProps) => {
122132
e.stopPropagation()
123133
return
124134
}
125-
if (e.key == 'c' && e.altKey && (e.ctrlKey || e.metaKey)) {
135+
if (e.key == 'c' && e.altKey && left_alt_pressed_ref.current && (e.ctrlKey || e.metaKey)) {
126136
if (
127137
!props.is_action_disabled &&
128138
props.on_copy &&

0 commit comments

Comments
 (0)