Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Implement Option/Alt + number key shortcuts for variant switching

Abi Raja24336571c0b6
diff --git a/VARIANT_EXPANSION_PLAN.md b/VARIANT_EXPANSION_PLAN.md
index ad3f76d4..278e47c0 100644
--- a/VARIANT_EXPANSION_PLAN.md
+++ b/VARIANT_EXPANSION_PLAN.md
@@ -231,4 +231,4 @@ const baseCommitObject = {
- [DONE] Let's set up a number of models that we want. And it will just cycle through those and repeat them. If models are [A, B], then if num = 5, it will be [A, B, A, B, A] and so on.
1. [DONE] With 3 options, it looks too squished on the front-end.
-2. We need to change hotkey to Option + 1, 2, 3, etc because when focused on textbox, the 1, 2, 3 keys are captured by the textbox.
+2. [DONE] We need to change hotkey to Option + 1, 2, 3, etc because when focused on textbox, the 1, 2, 3 keys are captured by the textbox. Fixed using event.code for cross-platform compatibility.
diff --git a/frontend/src/components/variants/Variants.tsx b/frontend/src/components/variants/Variants.tsx
index ad0e9b9f..37a1c5ae 100644
--- a/frontend/src/components/variants/Variants.tsx
+++ b/frontend/src/components/variants/Variants.tsx
@@ -31,28 +31,22 @@ function Variants() {
// Add keyboard shortcuts for variant switching
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
- // Only handle number keys when not in an input field
- if (
- event.target instanceof HTMLInputElement ||
- event.target instanceof HTMLTextAreaElement ||
- event.target instanceof HTMLSelectElement
- ) {
- return;
- }
-
- // Handle number keys 1-9
- const key = event.key;
- if (key >= "1" && key <= "9") {
- const variantIndex = parseInt(key) - 1;
-
- // Only switch if the variant exists and component is visible
- if (
- variantIndex < variants.length &&
- variants.length > 1 &&
- !commit.isCommitted
- ) {
- event.preventDefault();
- handleVariantClick(variantIndex);
+ // Handle Option + number keys 1-9 using key codes (works even when in input fields)
+ if (event.altKey && !event.ctrlKey && !event.shiftKey && !event.metaKey) {
+ // Use event.code to get the physical key, not the character it produces
+ const code = event.code;
+ if (code >= "Digit1" && code <= "Digit9") {
+ const variantIndex = parseInt(code.replace("Digit", "")) - 1;
+
+ // Only switch if the variant exists and component is visible
+ if (
+ variantIndex < variants.length &&
+ variants.length > 1 &&
+ !commit.isCommitted
+ ) {
+ event.preventDefault();
+ handleVariantClick(variantIndex);
+ }
}
}
};
@@ -126,7 +120,7 @@ function Variants() {
{statusIndicator}
</h3>
<span className="text-xs text-gray-400 dark:text-gray-500 font-mono bg-gray-100 dark:bg-gray-800 px-1.5 py-0.5 rounded">
- {index + 1}
+ ⌥{index + 1}
</span>
</div>
<div className="text-xs mt-1 flex items-center">