Skip to content

Commit

Permalink
fix: handle keyboard shortcuts with Ctrl key (#1690)
Browse files Browse the repository at this point in the history
fix: prevent default and stop propagation on certain key combos

Prevent default behavior on `ctrl + non-allowlisted keys`.. 
Also check for alt + vscode cody shortcuts and prevent default without
stopping propagation.

This fixes issues with certain key combinations triggering unintended
characters in input box.

- Add ctrlKeysAllowList Set to check pressed keys.
- Check ctrlKey or AltGraph modifier and that key is not in allow list.
- If so, prevent default browser shortcut and stop event propagation.

## Test plan

<!-- Required. See
https://docs.sourcegraph.com/dev/background-information/testing_principles.
-->

Pressing CtrlKey with any of the keys in the allowed list should not add
character to the chat inbox.

#### Before

Press `alt`+`/` when chat box is focused. This shortcut will open a new
Cody panel, but it will also add a symbol to your chat input box:


![image](https://github.com/sourcegraph/cody/assets/68532117/225581ff-0080-4853-9a0e-5a356cf6430c)


#### After

Press `alt`+`/` when chat box is focused will not change your current
input in the input box.
  • Loading branch information
abeatrix committed Nov 9, 2023
1 parent 26dd6a0 commit ac62708
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/ui/src/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,21 @@ export const Chat: React.FunctionComponent<ChatProps> = ({
event.stopPropagation()
setMessageBeingEdited(false)
onChatSubmit()
return
}

// Handles keyboard shortcuts with Ctrl key.
// Checks if the Ctrl key is pressed with a key not in the allow list
// to avoid triggering default browser shortcuts and bubbling the event.
const ctrlKeysAllowList = new Set(['a', 'c', 'v', 'x', 'y', 'z'])
if ((event.ctrlKey || event.getModifierState('AltGraph')) && !ctrlKeysAllowList.has(event.key)) {
event.preventDefault()
}

// Ignore alt + c key combination for editor to avoid conflict with cody shortcut
if (event.altKey && event.key === 'c') {
const vscodeCodyShortcuts = new Set(['Slash', 'KeyC'])
if (event.altKey && vscodeCodyShortcuts.has(event.code)) {
event.preventDefault()
event.stopPropagation()
}

// Handles cycling through chat command suggestions using the up and down arrow keys
Expand Down
1 change: 1 addition & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Starting from `0.2.0`, Cody is using `major.EVEN_NUMBER.patch` for release versi

- Fixup: Updated the fixup create task to just use the previous command text. [pull/1615](https://github.com/sourcegraph/cody/pull/1615)
- Chat: Opening files from the new chat panel will now show up beside the chat panel instead of on top of the chat panel. [pull/1677](https://github.com/sourcegraph/cody/pull/1677)
- Chat: Prevented default events on certain key combos when chat box is focused. [pull/1690](https://github.com/sourcegraph/cody/pull/1690)
- Command: Fixed an issue that opened a new chat window when running `/doc` and `/edit` commands from the command palette. [pull/1678](https://github.com/sourcegraph/cody/pull/1678)
- Chat: Prevent sidebar from opening when switching editor chat panels. [pull/1691](https://github.com/sourcegraph/cody/pull/1691)
- Chat: Prevent `"command 'cody.chat'panel.new' not found"` error when the new chat panel UI is disabled. [pull/1696](https://github.com/sourcegraph/cody/pull/1696)
Expand Down

0 comments on commit ac62708

Please sign in to comment.