1
0

OBSHotkeyEdit.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /******************************************************************************
  2. Copyright (C) 2014-2015 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "OBSHotkeyEdit.hpp"
  15. #include "OBSBasicSettings.hpp"
  16. #include <OBSApp.hpp>
  17. #include <qt-wrappers.hpp>
  18. #include <util/dstr.hpp>
  19. #include <QKeyEvent>
  20. #include "moc_OBSHotkeyEdit.cpp"
  21. void OBSHotkeyEdit::keyPressEvent(QKeyEvent *event)
  22. {
  23. if (event->isAutoRepeat())
  24. return;
  25. obs_key_combination_t new_key;
  26. switch (event->key()) {
  27. case Qt::Key_Shift:
  28. case Qt::Key_Control:
  29. case Qt::Key_Alt:
  30. case Qt::Key_Meta:
  31. new_key.key = OBS_KEY_NONE;
  32. break;
  33. #ifdef __APPLE__
  34. case Qt::Key_CapsLock:
  35. // kVK_CapsLock == 57
  36. new_key.key = obs_key_from_virtual_key(57);
  37. break;
  38. #endif
  39. default:
  40. new_key.key = obs_key_from_virtual_key(event->nativeVirtualKey());
  41. }
  42. new_key.modifiers = TranslateQtKeyboardEventModifiers(event->modifiers());
  43. HandleNewKey(new_key);
  44. }
  45. QVariant OBSHotkeyEdit::inputMethodQuery(Qt::InputMethodQuery query) const
  46. {
  47. if (query == Qt::ImEnabled) {
  48. return false;
  49. } else {
  50. return QLineEdit::inputMethodQuery(query);
  51. }
  52. }
  53. #ifdef __APPLE__
  54. void OBSHotkeyEdit::keyReleaseEvent(QKeyEvent *event)
  55. {
  56. if (event->isAutoRepeat())
  57. return;
  58. if (event->key() != Qt::Key_CapsLock)
  59. return;
  60. obs_key_combination_t new_key;
  61. // kVK_CapsLock == 57
  62. new_key.key = obs_key_from_virtual_key(57);
  63. new_key.modifiers = TranslateQtKeyboardEventModifiers(event->modifiers());
  64. HandleNewKey(new_key);
  65. }
  66. #endif
  67. void OBSHotkeyEdit::mousePressEvent(QMouseEvent *event)
  68. {
  69. obs_key_combination_t new_key;
  70. switch (event->button()) {
  71. case Qt::NoButton:
  72. case Qt::LeftButton:
  73. case Qt::RightButton:
  74. case Qt::AllButtons:
  75. case Qt::MouseButtonMask:
  76. return;
  77. case Qt::MiddleButton:
  78. new_key.key = OBS_KEY_MOUSE3;
  79. break;
  80. #define MAP_BUTTON(i, j) \
  81. case Qt::ExtraButton##i: \
  82. new_key.key = OBS_KEY_MOUSE##j; \
  83. break;
  84. MAP_BUTTON(1, 4)
  85. MAP_BUTTON(2, 5)
  86. MAP_BUTTON(3, 6)
  87. MAP_BUTTON(4, 7)
  88. MAP_BUTTON(5, 8)
  89. MAP_BUTTON(6, 9)
  90. MAP_BUTTON(7, 10)
  91. MAP_BUTTON(8, 11)
  92. MAP_BUTTON(9, 12)
  93. MAP_BUTTON(10, 13)
  94. MAP_BUTTON(11, 14)
  95. MAP_BUTTON(12, 15)
  96. MAP_BUTTON(13, 16)
  97. MAP_BUTTON(14, 17)
  98. MAP_BUTTON(15, 18)
  99. MAP_BUTTON(16, 19)
  100. MAP_BUTTON(17, 20)
  101. MAP_BUTTON(18, 21)
  102. MAP_BUTTON(19, 22)
  103. MAP_BUTTON(20, 23)
  104. MAP_BUTTON(21, 24)
  105. MAP_BUTTON(22, 25)
  106. MAP_BUTTON(23, 26)
  107. MAP_BUTTON(24, 27)
  108. #undef MAP_BUTTON
  109. }
  110. new_key.modifiers = TranslateQtKeyboardEventModifiers(event->modifiers());
  111. HandleNewKey(new_key);
  112. }
  113. void OBSHotkeyEdit::HandleNewKey(obs_key_combination_t new_key)
  114. {
  115. if (new_key == key || obs_key_combination_is_empty(new_key))
  116. return;
  117. key = new_key;
  118. changed = true;
  119. emit KeyChanged(key);
  120. RenderKey();
  121. }
  122. void OBSHotkeyEdit::RenderKey()
  123. {
  124. DStr str;
  125. obs_key_combination_to_str(key, str);
  126. setText(QT_UTF8(str));
  127. }
  128. void OBSHotkeyEdit::ResetKey()
  129. {
  130. key = original;
  131. changed = false;
  132. emit KeyChanged(key);
  133. RenderKey();
  134. }
  135. void OBSHotkeyEdit::ClearKey()
  136. {
  137. key = {0, OBS_KEY_NONE};
  138. changed = true;
  139. emit KeyChanged(key);
  140. RenderKey();
  141. }
  142. void OBSHotkeyEdit::UpdateDuplicationState()
  143. {
  144. if (!dupeIcon && !hasDuplicate)
  145. return;
  146. if (!dupeIcon)
  147. CreateDupeIcon();
  148. if (dupeIcon->isVisible() != hasDuplicate) {
  149. dupeIcon->setVisible(hasDuplicate);
  150. update();
  151. }
  152. }
  153. void OBSHotkeyEdit::InitSignalHandler()
  154. {
  155. layoutChanged = {obs_get_signal_handler(), "hotkey_layout_change",
  156. [](void *this_, calldata_t *) {
  157. auto edit = static_cast<OBSHotkeyEdit *>(this_);
  158. QMetaObject::invokeMethod(edit, "ReloadKeyLayout");
  159. },
  160. this};
  161. }
  162. void OBSHotkeyEdit::CreateDupeIcon()
  163. {
  164. dupeIcon = addAction(settings->GetHotkeyConflictIcon(), ActionPosition::TrailingPosition);
  165. dupeIcon->setToolTip(QTStr("Basic.Settings.Hotkeys.DuplicateWarning"));
  166. QObject::connect(dupeIcon, &QAction::triggered, [=] { emit SearchKey(key); });
  167. dupeIcon->setVisible(false);
  168. }
  169. void OBSHotkeyEdit::ReloadKeyLayout()
  170. {
  171. RenderKey();
  172. }