Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED
  18. #define JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED
  19. //==============================================================================
  20. /** This class is used to invoke a range of text-editor navigation methods on
  21. an object, based upon a keypress event.
  22. It's currently used internally by the TextEditor and CodeEditorComponent.
  23. */
  24. template <class CallbackClass>
  25. struct TextEditorKeyMapper
  26. {
  27. /** Checks the keypress and invokes one of a range of navigation functions that
  28. the target class must implement, based on the key event.
  29. */
  30. static bool invokeKeyFunction (CallbackClass& target, const KeyPress& key)
  31. {
  32. const ModifierKeys& mods = key.getModifiers();
  33. const bool isShiftDown = mods.isShiftDown();
  34. const bool ctrlOrAltDown = mods.isCtrlDown() || mods.isAltDown();
  35. int numCtrlAltCommandKeys = 0;
  36. if (mods.isCtrlDown()) ++numCtrlAltCommandKeys;
  37. if (mods.isAltDown()) ++numCtrlAltCommandKeys;
  38. if (key == KeyPress (KeyPress::downKey, ModifierKeys::ctrlModifier, 0) && target.scrollUp()) return true;
  39. if (key == KeyPress (KeyPress::upKey, ModifierKeys::ctrlModifier, 0) && target.scrollDown()) return true;
  40. #if JUCE_MAC
  41. if (mods.isCommandDown() && ! ctrlOrAltDown)
  42. {
  43. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretToTop (isShiftDown);
  44. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretToEnd (isShiftDown);
  45. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretToStartOfLine (isShiftDown);
  46. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretToEndOfLine (isShiftDown);
  47. }
  48. if (mods.isCommandDown())
  49. ++numCtrlAltCommandKeys;
  50. #endif
  51. if (numCtrlAltCommandKeys < 2)
  52. {
  53. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretLeft (ctrlOrAltDown, isShiftDown);
  54. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretRight (ctrlOrAltDown, isShiftDown);
  55. if (key.isKeyCode (KeyPress::homeKey)) return ctrlOrAltDown ? target.moveCaretToTop (isShiftDown)
  56. : target.moveCaretToStartOfLine (isShiftDown);
  57. if (key.isKeyCode (KeyPress::endKey)) return ctrlOrAltDown ? target.moveCaretToEnd (isShiftDown)
  58. : target.moveCaretToEndOfLine (isShiftDown);
  59. }
  60. if (numCtrlAltCommandKeys == 0)
  61. {
  62. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretUp (isShiftDown);
  63. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretDown (isShiftDown);
  64. if (key.isKeyCode (KeyPress::pageUpKey)) return target.pageUp (isShiftDown);
  65. if (key.isKeyCode (KeyPress::pageDownKey)) return target.pageDown (isShiftDown);
  66. }
  67. if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
  68. || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
  69. return target.copyToClipboard();
  70. if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
  71. || key == KeyPress (KeyPress::deleteKey, ModifierKeys::shiftModifier, 0))
  72. return target.cutToClipboard();
  73. if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
  74. || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
  75. return target.pasteFromClipboard();
  76. // NB: checking for delete must happen after the earlier check for shift + delete
  77. if (numCtrlAltCommandKeys < 2)
  78. {
  79. if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
  80. if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
  81. }
  82. if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  83. return target.selectAll();
  84. if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
  85. return target.undo();
  86. if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
  87. || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
  88. return target.redo();
  89. return false;
  90. }
  91. };
  92. #endif // JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED