Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.4 KB
Raw Blame
import { useState, useEffect } from 'react';

/**
 * Hook to track whether the shift key is currently being held down.
 * Ignores shift when Alt is also pressed to avoid conflicts with
 * accessibility keyboard shortcuts (Shift+Alt is used for screen readers).
 * @returns boolean indicating if shift key is pressed (without Alt)
 */
export default function useShiftKey(): boolean {
  const [isShiftHeld, setIsShiftHeld] = useState(false);

  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      // Only set shift if Alt is not pressed (Alt+Shift is used for a11y)
      if (e.key === 'Shift' && !e.altKey) {
        setIsShiftHeld(true);
      }
      // If Alt is pressed while shift is held, reset shift state
      if (e.key === 'Alt' && e.shiftKey) {
        setIsShiftHeld(false);
      }
    };

    const handleKeyUp = (e: KeyboardEvent) => {
      if (e.key === 'Shift') {
        setIsShiftHeld(false);
      }
    };

    // Reset shift state when window loses focus
    const handleBlur = () => {
      setIsShiftHeld(false);
    };

    window.addEventListener('keydown', handleKeyDown);
    window.addEventListener('keyup', handleKeyUp);
    window.addEventListener('blur', handleBlur);

    return () => {
      window.removeEventListener('keydown', handleKeyDown);
      window.removeEventListener('keyup', handleKeyUp);
      window.removeEventListener('blur', handleBlur);
    };
  }, []);

  return isShiftHeld;
}