How to Get Native Modifiers in QMouseEvent to Differentiate Ctrl and Command?
-
Hi everyone,
I am working on a program that allows users to customize shortcuts, including modifiers for both keyboard and mouse events. However, I ran into an issue on macOS where QMouseEvent::modifiers() does not provide enough detail to correctly distinguish between different modifier keys.My Problem
- In QKeyEvent, I can use nativeModifiers() to get system-specific key modifiers. However, QMouseEvent only provides modifiers(), which lack native information.
- On macOS, Qt::ControlModifier is mapped to the Command (⌘) key, making it difficult to differentiate between Command and Control (⌃) when handling shortcuts.
- Additionally, I need to distinguish between left and right Command keys in mouse events, but QMouseEvent does not seem to provide this distinction.
- Since QMouseEvent does not detect the Control key at all on macOS, users cannot assign mouse-based shortcuts that involve Control.
Is there a way to get native modifier information (like nativeModifiers() in QKeyEvent) for QMouseEvent? If not, what alternative approaches can I use to achieve this?
I appreciate any help you can provide.
-
@Kevin-Hoang said in How to Get Native Modifiers in QMouseEvent to Differentiate Ctrl and Command?:
- On macOS, Qt::ControlModifier is mapped to the Command (⌘) key, making it difficult to differentiate between Command and Control (⌃) when handling shortcuts.
https://doc.qt.io/qt-6/qt.html#KeyboardModifier-enum:
Note: On macOS, the ControlModifier value corresponds to the Command keys on the keyboard, and the MetaModifier value corresponds to the Control keys.
- Additionally, I need to distinguish between left and right Command keys in mouse events, but QMouseEvent does not seem to provide this distinction.
This can be accomplished by caching the pressed/released keys. Unfortunate but not insurmountable.
-
Thanks for your response! However, the situation is a bit more complex than just swapping ControlModifier and MetaModifier on macOS.
- Modifier keys on macOS can be remapped in the Keyboard Modifier Settings. This means ControlModifier does not always correspond to the Command key, and MetaModifier does not always correspond to the Control key. If a user changes these settings, Qt's default mapping no longer reflects the actual physical key being pressed.
- Command + Left Click is interpreted as a Right Click on macOS by default. This behavior is designed for trackpads and Magic Mouse, but it creates inconsistencies when handling shortcuts. If a user assigns a shortcut using Command, it might get detected as Control, leading to confusion.
- Users may perceive this as a bug because when setting up a shortcut, they might press Command, but the program registers it as Control (or vice versa, depending on their settings). If they modify their keyboard settings, all existing shortcuts could become misaligned.
I am looking for a way to access native modifier keys directly, so that when setting up shortcuts, the program accurately detects the actual key pressed, regardless of macOS system settings. Is there a way to achieve this in Qt?