Fix macOS keyboard layout issues with Unicode typing#947
Open
jmpnop wants to merge 1 commit intoasweigart:masterfrom
Open
Fix macOS keyboard layout issues with Unicode typing#947jmpnop wants to merge 1 commit intoasweigart:masterfrom
jmpnop wants to merge 1 commit intoasweigart:masterfrom
Conversation
This fix resolves issues where PyAutoGUI would type incorrect characters when using non-US keyboard layouts on macOS (Russian, German, Dvorak, etc.). Changes: - Add _isPrintableChar() to distinguish printable chars from special keys - Add _typeUnicodeChar() using CGEventKeyboardSetUnicodeString for layout-independent character input - Modify _normalKeyEvent() to route printable characters through Unicode typing while maintaining key code approach for special keys This approach: - Is backward compatible (no API changes) - Only affects macOS (_pyautogui_osx.py) - Preserves existing behavior for special keys - Works with any keyboard layout Fixes asweigart#46, asweigart#122 Related to asweigart#137
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PyAutoGUI currently types incorrect characters on macOS when using non-US keyboard layouts (Russian, German, Dvorak, etc.) because it sends virtual key codes that get mapped by the active keyboard layout.
Example:
pyautogui.write("test")This has been reported in:
Solution
This PR fixes the issue by using
CGEventKeyboardSetUnicodeStringfrom macOS's Quartz framework for printable characters, while maintaining the existing key code approach for special keys.Changes
_isPrintableChar(key): Distinguishes printable characters from special keys (arrows, modifiers, function keys, etc.)_typeUnicodeChar(char): Types characters using layout-independent Unicode method viaCGEventKeyboardSetUnicodeString_normalKeyEvent(): Routes printable characters through Unicode typing for 'down' events, skips 'up' events for printable chars (Unicode method handles both)Why This Approach?
_pyautogui_osx.py, ~50 lines of code)Testing
Tested On
Test Results
Test Code
Impact
This fix will benefit users who:
Based on the issue tracker, this affects many users across different languages and layouts.
Backward Compatibility
✅ All existing PyAutoGUI code continues to work
Technical Details
The fix uses macOS's
CGEventKeyboardSetUnicodeStringAPI which sets the Unicode representation of characters directly on keyboard events, bypassing the keyboard layout mapping that was causing the issue.Before:
After:
Special keys still use key codes (arrows need physical key position, not Unicode).
Additional Notes
I'm happy to:
This is a real-world fix that I've been using successfully. I wanted to contribute it back to help others facing the same issue.
Thank you for maintaining PyAutoGUI! It's an incredibly useful library.