Skip to content

Conversation

Altnbek1
Copy link

Keyboard Layout Support Fix for Undo/Redo Operations

Related Issue

Closes #928

Overview

This fix addresses the issue where undo/redo keyboard shortcuts (Ctrl+Z/Ctrl+Y) were not working correctly on non-English keyboard layouts, particularly German layouts where the Z and Y keys are swapped.

Problem

The original implementation used event.key to detect keyboard shortcuts, which depends on the keyboard layout:

  • On English layout: Ctrl+Z = undo, Ctrl+Y = redo
  • On German layout: Ctrl+Y (German Y key) = undo, Ctrl+Z (German Z key) = redo

This caused confusion and inconsistent behavior across different keyboard layouts.

Solution

Implemented a universal solution using event.code instead of event.key:

  • KeyZ code always represents the Z key position (regardless of layout)
  • KeyY code always represents the Y key position (regardless of layout)

This ensures consistent behavior across all keyboard layouts.

Changes Made

1. Updated lib/features/keyboard/KeyboardUtil.js

  • Added KEY_CODES constant for key codes:

    export var KEY_CODES = {
      Z: 'KeyZ',
      Y: 'KeyY'
    };
  • Modified isUndo() function to support both key letters and key codes:

    export function isUndo(event) {
      return isCmd(event) && !isShift(event) && (
        isKey(KEYS_UNDO, event) || 
        event.code === KEY_CODES.Z
      );
    }
  • Modified isRedo() function to support both key letters and key codes:

    export function isRedo(event) {
      return isCmd(event) && (
        isKey(KEYS_REDO, event) || 
        event.code === KEY_CODES.Y ||
        (isKey(KEYS_UNDO, event) && isShift(event)) ||
        (event.code === KEY_CODES.Z && isShift(event))
      );
    }

2. Updated lib/features/keyboard/KeyboardBindings.js

  • Added KEY_CODES to exports
  • Updated comments to reflect universal keyboard layout support
  • Removed German-specific constants

3. Updated Tests

test/spec/features/keyboard/UndoSpec.js

  • Added tests for KeyZ code support
  • Removed German-specific tests
  • Ensured backward compatibility with existing key-based tests

test/spec/features/keyboard/RedoSpec.js

  • Added tests for KeyY and KeyZ code support
  • Removed German-specific tests
  • Ensured backward compatibility with existing key-based tests

Benefits

  1. Universal Compatibility: Works on all keyboard layouts (English, German, French, etc.)
  2. Backward Compatibility: Existing key-based shortcuts continue to work
  3. Consistent Behavior: Same physical keys always trigger the same actions
  4. Future-Proof: No need to add support for each new keyboard layout

Testing

All tests pass successfully:

  • ✅ Existing undo/redo functionality preserved
  • ✅ New key code support working correctly
  • ✅ No regression in other keyboard features
  • ✅ Cross-layout compatibility verified

Technical Details

  • Uses event.code for layout-independent key detection
  • Maintains event.key support for backward compatibility
  • Leverages existing isKey() utility function
  • Follows diagram-js coding conventions and patterns

Files Modified

  • lib/features/keyboard/KeyboardUtil.js
  • lib/features/keyboard/KeyboardBindings.js
  • test/spec/features/keyboard/UndoSpec.js
  • test/spec/features/keyboard/RedoSpec.js

Commit Message

fix(keyboard): use key codes for undo/redo to support all keyboard layouts

@CLAassistant
Copy link

CLAassistant commented Jun 21, 2025

CLA assistant check
All committers have signed the CLA.

@Altnbek1
Copy link
Author

Signed the agreement

@barmac
Copy link
Member

barmac commented Jun 26, 2025

Hi, it looks that your account and the commit email are mismatching thus the contribution is not connected to the CLA. You'd need to update the email on the commit: https://stackoverflow.com/questions/750172/how-do-i-change-the-author-and-committer-name-email-for-multiple-commits

@Altnbek1
Copy link
Author

Good evening. Sorry for the confusion. I signed the agreement with the required email address, please check

@barmac
Copy link
Member

barmac commented Jul 2, 2025

Unfortunately, it is still the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hotkeys do Not work on non-English Keyboard Layout

3 participants