qhotkey.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #ifndef QHOTKEY_H
  2. #define QHOTKEY_H
  3. #include <QKeySequence>
  4. #include <QLoggingCategory>
  5. #include <QObject>
  6. #include <QPair>
  7. #ifdef QHOTKEY_LIB
  8. #ifdef QHOTKEY_LIB_BUILD
  9. #define QHOTKEY_SHARED_EXPORT Q_DECL_EXPORT
  10. #else
  11. #define QHOTKEY_SHARED_EXPORT Q_DECL_IMPORT
  12. #endif
  13. #else
  14. #define QHOTKEY_SHARED_EXPORT
  15. #endif
  16. //! A class to define global, systemwide Hotkeys
  17. class QHOTKEY_SHARED_EXPORT QHotkey : public QObject
  18. {
  19. Q_OBJECT
  20. //! @private
  21. friend class QHotkeyPrivate;
  22. //! Specifies whether this hotkey is currently registered or not
  23. Q_PROPERTY(bool registered READ isRegistered WRITE setRegistered NOTIFY
  24. registeredChanged)
  25. //! Holds the shortcut this hotkey will be triggered on
  26. Q_PROPERTY(
  27. QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut)
  28. public:
  29. //! Defines shortcut with native keycodes
  30. class QHOTKEY_SHARED_EXPORT NativeShortcut
  31. {
  32. public:
  33. //! The native keycode
  34. quint32 key;
  35. //! The native modifiers
  36. quint32 modifier;
  37. //! Creates an invalid native shortcut
  38. NativeShortcut();
  39. //! Creates a valid native shortcut, with the given key and modifiers
  40. NativeShortcut(quint32 key, quint32 modifier = 0);
  41. //! Checks, whether this shortcut is valid or not
  42. bool isValid() const;
  43. //! Equality operator
  44. bool operator==(NativeShortcut other) const;
  45. //! Inequality operator
  46. bool operator!=(NativeShortcut other) const;
  47. private:
  48. bool valid;
  49. };
  50. //! Adds a global mapping of a key sequence to a replacement native shortcut
  51. static void addGlobalMapping(const QKeySequence& shortcut,
  52. NativeShortcut nativeShortcut);
  53. //! Checks if global shortcuts are supported by the current platform
  54. static bool isPlatformSupported();
  55. //! Default Constructor
  56. explicit QHotkey(QObject* parent = nullptr);
  57. //! Constructs a hotkey with a shortcut and optionally registers it
  58. explicit QHotkey(const QKeySequence& shortcut,
  59. bool autoRegister = false,
  60. QObject* parent = nullptr);
  61. //! Constructs a hotkey with a key and modifiers and optionally registers it
  62. explicit QHotkey(Qt::Key keyCode,
  63. Qt::KeyboardModifiers modifiers,
  64. bool autoRegister = false,
  65. QObject* parent = nullptr);
  66. //! Constructs a hotkey from a native shortcut and optionally registers it
  67. explicit QHotkey(NativeShortcut shortcut,
  68. bool autoRegister = false,
  69. QObject* parent = nullptr);
  70. ~QHotkey() override;
  71. //! @readAcFn{QHotkey::registered}
  72. bool isRegistered() const;
  73. //! @readAcFn{QHotkey::shortcut}
  74. QKeySequence shortcut() const;
  75. //! @readAcFn{QHotkey::shortcut} - the key only
  76. Qt::Key keyCode() const;
  77. //! @readAcFn{QHotkey::shortcut} - the modifiers only
  78. Qt::KeyboardModifiers modifiers() const;
  79. //! Get the current native shortcut
  80. NativeShortcut currentNativeShortcut() const;
  81. public slots:
  82. //! @writeAcFn{QHotkey::registered}
  83. bool setRegistered(bool registered);
  84. //! @writeAcFn{QHotkey::shortcut}
  85. bool setShortcut(const QKeySequence& shortcut, bool autoRegister = false);
  86. //! @writeAcFn{QHotkey::shortcut}
  87. bool setShortcut(Qt::Key keyCode,
  88. Qt::KeyboardModifiers modifiers,
  89. bool autoRegister = false);
  90. //! @resetAcFn{QHotkey::shortcut}
  91. bool resetShortcut();
  92. //! Set this hotkey to a native shortcut
  93. bool setNativeShortcut(QHotkey::NativeShortcut nativeShortcut,
  94. bool autoRegister = false);
  95. signals:
  96. //! Will be emitted if the shortcut is pressed
  97. void activated(QPrivateSignal);
  98. //! Will be emitted if the shortcut press is released
  99. void released(QPrivateSignal);
  100. //! @notifyAcFn{QHotkey::registered}
  101. void registeredChanged(bool registered);
  102. private:
  103. Qt::Key _keyCode;
  104. Qt::KeyboardModifiers _modifiers;
  105. NativeShortcut _nativeShortcut;
  106. bool _registered;
  107. };
  108. uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key);
  109. uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key, uint seed);
  110. QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
  111. Q_DECLARE_METATYPE(QHotkey::NativeShortcut)
  112. #endif // QHOTKEY_H