qxtglobalshortcut.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "qxtglobalshortcut.h"
  2. /****************************************************************************
  3. ** Copyright (c) 2006 - 2011, the LibQxt project.
  4. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
  5. ** All rights reserved.
  6. **
  7. ** Redistribution and use in source and binary forms, with or without
  8. ** modification, are permitted provided that the following conditions are met:
  9. ** * Redistributions of source code must retain the above copyright
  10. ** notice, this list of conditions and the following disclaimer.
  11. ** * Redistributions in binary form must reproduce the above copyright
  12. ** notice, this list of conditions and the following disclaimer in the
  13. ** documentation and/or other materials provided with the distribution.
  14. ** * Neither the name of the LibQxt project nor the
  15. ** names of its contributors may be used to endorse or promote products
  16. ** derived from this software without specific prior written permission.
  17. **
  18. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. ** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. **
  29. ** <http://libqxt.org> <foundation@libqxt.org>
  30. *****************************************************************************/
  31. #include "qxtglobalshortcut_p.h"
  32. #include <QAbstractEventDispatcher>
  33. #include <QtDebug>
  34. #ifndef Q_OS_MAC
  35. int QxtGlobalShortcutPrivate::ref = 0;
  36. # if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  37. QAbstractEventDispatcher::EventFilter QxtGlobalShortcutPrivate::prevEventFilter = 0;
  38. # endif
  39. #endif // Q_OS_MAC
  40. QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> QxtGlobalShortcutPrivate::shortcuts;
  41. QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate() : enabled(true), key(Qt::Key(0)), mods(Qt::NoModifier)
  42. {
  43. #ifndef Q_OS_MAC
  44. if (ref == 0) {
  45. # if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  46. prevEventFilter = QAbstractEventDispatcher::instance()->setEventFilter(eventFilter);
  47. # else
  48. QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
  49. #endif
  50. }
  51. ++ref;
  52. #endif // Q_OS_MAC
  53. }
  54. QxtGlobalShortcutPrivate::~QxtGlobalShortcutPrivate()
  55. {
  56. #ifndef Q_OS_MAC
  57. --ref;
  58. if (ref == 0) {
  59. QAbstractEventDispatcher *ed = QAbstractEventDispatcher::instance();
  60. if (ed != 0) {
  61. # if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  62. ed->setEventFilter(prevEventFilter);
  63. # else
  64. ed->removeNativeEventFilter(this);
  65. # endif
  66. }
  67. }
  68. #endif // Q_OS_MAC
  69. }
  70. bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut)
  71. {
  72. Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier;
  73. key = shortcut.isEmpty() ? Qt::Key(0) : Qt::Key((shortcut[0] ^ allMods) & shortcut[0]);
  74. mods = shortcut.isEmpty() ? Qt::KeyboardModifiers(0) : Qt::KeyboardModifiers(shortcut[0] & allMods);
  75. if (enabled)
  76. return registerShortcut();
  77. else
  78. return false;
  79. }
  80. bool QxtGlobalShortcutPrivate::registerShortcut()
  81. {
  82. const quint32 nativeKey = nativeKeycode(key);
  83. const quint32 nativeMods = nativeModifiers(mods);
  84. const bool res = registerShortcut(nativeKey, nativeMods);
  85. if (res)
  86. shortcuts.insert(qMakePair(nativeKey, nativeMods), &qxt_p());
  87. else
  88. qWarning() << "QxtGlobalShortcut failed to register:" << QKeySequence(key + mods).toString();
  89. return res;
  90. }
  91. bool QxtGlobalShortcutPrivate::unsetShortcut()
  92. {
  93. bool res = false;
  94. if (enabled)
  95. res = unregisterShortcut();
  96. key = Qt::Key(0);
  97. mods = Qt::KeyboardModifiers(0);
  98. return res;
  99. }
  100. bool QxtGlobalShortcutPrivate::unregisterShortcut()
  101. {
  102. bool res = false;
  103. const quint32 nativeKey = nativeKeycode(key);
  104. const quint32 nativeMods = nativeModifiers(mods);
  105. if (shortcuts.value(qMakePair(nativeKey, nativeMods)) == &qxt_p())
  106. res = unregisterShortcut(nativeKey, nativeMods);
  107. if (res)
  108. shortcuts.remove(qMakePair(nativeKey, nativeMods));
  109. else
  110. qWarning() << "QxtGlobalShortcut failed to unregister:" << QKeySequence(key + mods).toString();
  111. return res;
  112. }
  113. void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods)
  114. {
  115. QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods));
  116. if (shortcut && shortcut->isEnabled())
  117. emit shortcut->activated();
  118. }
  119. /*!
  120. \class QxtGlobalShortcut
  121. \inmodule QxtWidgets
  122. \brief The QxtGlobalShortcut class provides a global shortcut aka "hotkey".
  123. A global shortcut triggers even if the application is not active. This
  124. makes it easy to implement applications that react to certain shortcuts
  125. still if some other application is active or if the application is for
  126. example minimized to the system tray.
  127. Example usage:
  128. \code
  129. QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(window);
  130. connect(shortcut, SIGNAL(activated()), window, SLOT(toggleVisibility()));
  131. shortcut->setShortcut(QKeySequence("Ctrl+Shift+F12"));
  132. \endcode
  133. \bold {Note:} Since Qxt 0.6 QxtGlobalShortcut no more requires QxtApplication.
  134. */
  135. /*!
  136. \fn QxtGlobalShortcut::activated()
  137. This signal is emitted when the user types the shortcut's key sequence.
  138. \sa shortcut
  139. */
  140. /*!
  141. Constructs a new QxtGlobalShortcut with \a parent.
  142. */
  143. QxtGlobalShortcut::QxtGlobalShortcut(QObject* parent)
  144. : QObject(parent)
  145. {
  146. QXT_INIT_PRIVATE(QxtGlobalShortcut);
  147. }
  148. /*!
  149. Constructs a new QxtGlobalShortcut with \a shortcut and \a parent.
  150. */
  151. QxtGlobalShortcut::QxtGlobalShortcut(const QKeySequence& shortcut, QObject* parent)
  152. : QObject(parent)
  153. {
  154. QXT_INIT_PRIVATE(QxtGlobalShortcut);
  155. setShortcut(shortcut);
  156. }
  157. /*!
  158. Destructs the QxtGlobalShortcut.
  159. */
  160. QxtGlobalShortcut::~QxtGlobalShortcut()
  161. {
  162. if (qxt_d().enabled)
  163. qxt_d().unsetShortcut();
  164. }
  165. /*!
  166. \property QxtGlobalShortcut::shortcut
  167. \brief the shortcut key sequence
  168. \bold {Note:} Notice that corresponding key press and release events are not
  169. delivered for registered global shortcuts even if they are disabled.
  170. Also, comma separated key sequences are not supported.
  171. Only the first part is used:
  172. \code
  173. qxtShortcut->setShortcut(QKeySequence("Ctrl+Alt+A,Ctrl+Alt+B"));
  174. Q_ASSERT(qxtShortcut->shortcut() == QKeySequence("Ctrl+Alt+A"));
  175. \endcode
  176. */
  177. QKeySequence QxtGlobalShortcut::shortcut() const
  178. {
  179. return QKeySequence(qxt_d().key | qxt_d().mods);
  180. }
  181. bool QxtGlobalShortcut::setShortcut(const QKeySequence& shortcut)
  182. {
  183. if (qxt_d().key != 0)
  184. qxt_d().unsetShortcut();
  185. return qxt_d().setShortcut(shortcut);
  186. }
  187. /*!
  188. \property QxtGlobalShortcut::enabled
  189. \brief whether the shortcut is enabled
  190. A disabled shortcut does not get activated.
  191. The default value is \c true.
  192. \sa setDisabled()
  193. */
  194. bool QxtGlobalShortcut::isEnabled() const
  195. {
  196. return qxt_d().enabled;
  197. }
  198. void QxtGlobalShortcut::setEnabled(bool enabled)
  199. {
  200. if (qxt_d().key != 0)
  201. {
  202. if (enabled && !qxt_d().enabled)
  203. qxt_d().registerShortcut();
  204. else if (!enabled && qxt_d().enabled)
  205. qxt_d().unregisterShortcut();
  206. }
  207. qxt_d().enabled = enabled;
  208. }
  209. /*!
  210. Sets the shortcut \a disabled.
  211. \sa enabled
  212. */
  213. void QxtGlobalShortcut::setDisabled(bool disabled)
  214. {
  215. setEnabled(!disabled);
  216. }