DEV Community

Gabriele B.
Gabriele B.

Posted on

Keyboard Shortcuts Implementation

Overview

Implemented a comprehensive keyboard shortcut system with instant tooltips and settings integration for the shape editor application.

Implemented Shortcuts

Shape Creation (Direct Key Press - No Modifiers)

  • Q - Add Rectangle
  • W - Add Circle
  • E - Add Text
  • R - Add Button
  • T - Add Chat Bubble
  • Y - Add Chat Frame
  • U - Add Line

Zoom Controls

  • + (Plus/Equals) - Zoom in by 5%
  • - (Minus) - Zoom out by 5%

Edit Operations (Ctrl/Cmd + Key)

  • Ctrl + Z - Undo
  • Ctrl + Shift + Z - Redo
  • Ctrl + Y - Redo (Alternative)
  • Ctrl + D - Duplicate
  • Delete/Backspace - Delete Selected

Selection (Ctrl/Cmd + Key)

  • Ctrl + A - Select All
  • Escape - Deselect All
  • Ctrl + G - Group Selected
  • Ctrl + Shift + G - Ungroup Selected

Navigation

  • Arrow Keys - Nudge selected elements by 1px
  • Shift + Arrow Keys - Nudge selected elements by 10px

View Controls

  • G - Toggle Grid

Advanced (Ctrl/Cmd + Key)

  • Ctrl + E - Export
  • Ctrl + ; - Toggle Snapping

Technical Implementation

1. Tooltip System (src/components/common/Tooltip.tsx)

  • Created a reusable Tooltip component
  • Instant show/hide on hover (no delay)
  • Non-modal, doesn't interfere with workflow
  • Displays tool name with shortcut key and description
  • Positioned dynamically below each toolbar button

2. Updated Toolbar (src/components/design-tool/Toolbar.tsx)

  • Added detailed tooltips to all shape tools
  • Integrated Tooltip component with each button
  • Added zoom in/out handlers (5% increments)
  • Created createLine function for line creation
  • Tool definitions now include:
    • Icon
    • Label
    • Shortcut key
    • Detailed description
    • Action handler

3. Enhanced Keyboard Shortcuts Hook (src/hooks/useGlobalKeyboardShortcuts.ts)

  • Updated shortcut mappings to match requirements:
    • Q → Rectangle (was R)
    • W → Circle (was O)
    • E → Text (was T)
    • R → Button (new)
    • T → Chat Bubble (new)
    • Y → Chat Frame (new)
    • U → Line (was L)
  • Added zoom controls (+ and -)
  • Created helper functions:
    • createButton() - Creates button elements
    • createChatBubble() - Creates chat bubble elements
    • createChatFrame() - Creates chat frame elements
  • Text editing detection automatically disables shape shortcuts
  • All shortcuts work seamlessly with existing animation system

4. Settings Integration (src/components/design-tool/EditorSettingsModal.tsx)

  • Replaced empty shortcuts tab with comprehensive list
  • Organized shortcuts into categories:
    • Shape Creation
    • View Controls
    • Edit Operations
    • Selection
    • Navigation
    • Advanced
  • Each shortcut displays:
    • Description
    • Key combination in styled kbd element
  • Added informative tips section
  • Visual indicator showing shape shortcuts are disabled during text editing

5. Integration Point (src/components/UIDesignTool.tsx)

  • Passed zoom and setZoom props to useGlobalKeyboardShortcuts
  • Ensures zoom shortcuts work globally across the editor

Key Features

Smart Context Detection

  • Automatically detects when user is typing in input fields
  • Disables shape creation shortcuts during text editing
  • Checks for:
    • INPUT elements
    • TEXTAREA elements
    • contentEditable elements
    • Elements with role="textbox"

Tooltip Behavior

  • Appears instantly on hover
  • Disappears instantly when hover ends
  • Shows shortcut key in title (e.g., "Rectangle (Q)")
  • Includes detailed description of each tool
  • Non-blocking and doesn't interfere with clicks

Consistent User Experience

  • Shortcuts trigger same actions as clicking toolbar buttons
  • Visual feedback through tooltips
  • Discoverable through settings panel
  • Works across all layout modes

Usage Examples

Creating Shapes

  1. Press Q to add a rectangle
  2. Press W to add a circle
  3. Press E to add text
  4. Press R to add a button
  5. Press T to add a chat bubble
  6. Press Y to add a chat frame
  7. Press U to add a line

Zoom Control

  1. Press + to zoom in by 5%
  2. Press - to zoom out by 5%

Viewing Shortcuts

  1. Click the Settings icon in the toolbar
  2. Navigate to the "Shortcuts" tab
  3. View all available shortcuts organized by category

Tooltip Discovery

  1. Hover over any tool button in the toolbar
  2. View instant tooltip with shortcut key and description

Testing Recommendations

  1. Shape Creation

    • Press each letter key (Q, W, E, R, T, Y, U)
    • Verify correct shape appears on canvas
    • Confirm shape is automatically selected
  2. Text Editing Safety

    • Click on a text element to edit
    • Press shape shortcut keys
    • Verify shortcuts don't trigger while editing
  3. Zoom Controls

    • Press + multiple times
    • Verify zoom increases by 5% each time
    • Press - multiple times
    • Verify zoom decreases by 5% each time
  4. Tooltips

    • Hover over each toolbar button
    • Verify tooltip appears instantly
    • Verify tooltip disappears when moving away
    • Check tooltip content is accurate
  5. Settings Panel

    • Open Editor Settings
    • Navigate to Shortcuts tab
    • Verify all shortcuts are listed
    • Verify organization and readability

Browser Compatibility

  • Works in all modern browsers
  • Supports both Ctrl (Windows/Linux) and Cmd (macOS) modifiers
  • Responsive to keyboard events across different layouts

Performance Considerations

  • Tooltips render on-demand (only when hovering)
  • Event listeners properly cleaned up on unmount
  • No memory leaks from tooltip instances
  • Minimal overhead from keyboard event handling

Future Enhancement Possibilities

  1. Customizable shortcut mappings
  2. Shortcut conflict detection
  3. Shortcut recording/editing UI
  4. Export/import shortcut configurations
  5. Multi-key chord shortcuts
  6. Shortcut search/filter in settings

Top comments (0)