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.

117 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /** This class is used to invoke a range of text-editor navigation methods on
  17. an object, based upon a keypress event.
  18. It's currently used internally by the TextEditor and CodeEditorComponent.
  19. @tags{GUI}
  20. */
  21. template <class CallbackClass>
  22. struct TextEditorKeyMapper
  23. {
  24. /** Checks the keypress and invokes one of a range of navigation functions that
  25. the target class must implement, based on the key event.
  26. */
  27. static bool invokeKeyFunction (CallbackClass& target, const KeyPress& key)
  28. {
  29. auto mods = key.getModifiers();
  30. const bool isShiftDown = mods.isShiftDown();
  31. const bool ctrlOrAltDown = mods.isCtrlDown() || mods.isAltDown();
  32. int numCtrlAltCommandKeys = 0;
  33. if (mods.isCtrlDown()) ++numCtrlAltCommandKeys;
  34. if (mods.isAltDown()) ++numCtrlAltCommandKeys;
  35. if (key == KeyPress (KeyPress::downKey, ModifierKeys::ctrlModifier, 0) && target.scrollUp()) return true;
  36. if (key == KeyPress (KeyPress::upKey, ModifierKeys::ctrlModifier, 0) && target.scrollDown()) return true;
  37. #if JUCE_MAC
  38. if (mods.isCommandDown() && ! ctrlOrAltDown)
  39. {
  40. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretToTop (isShiftDown);
  41. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretToEnd (isShiftDown);
  42. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretToStartOfLine (isShiftDown);
  43. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretToEndOfLine (isShiftDown);
  44. }
  45. if (mods.isCommandDown())
  46. ++numCtrlAltCommandKeys;
  47. #endif
  48. if (numCtrlAltCommandKeys < 2)
  49. {
  50. if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretLeft (ctrlOrAltDown, isShiftDown);
  51. if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretRight (ctrlOrAltDown, isShiftDown);
  52. if (key.isKeyCode (KeyPress::homeKey)) return ctrlOrAltDown ? target.moveCaretToTop (isShiftDown)
  53. : target.moveCaretToStartOfLine (isShiftDown);
  54. if (key.isKeyCode (KeyPress::endKey)) return ctrlOrAltDown ? target.moveCaretToEnd (isShiftDown)
  55. : target.moveCaretToEndOfLine (isShiftDown);
  56. }
  57. if (numCtrlAltCommandKeys == 0)
  58. {
  59. if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretUp (isShiftDown);
  60. if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretDown (isShiftDown);
  61. if (key.isKeyCode (KeyPress::pageUpKey)) return target.pageUp (isShiftDown);
  62. if (key.isKeyCode (KeyPress::pageDownKey)) return target.pageDown (isShiftDown);
  63. }
  64. if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
  65. || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
  66. return target.copyToClipboard();
  67. if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
  68. || key == KeyPress (KeyPress::deleteKey, ModifierKeys::shiftModifier, 0))
  69. return target.cutToClipboard();
  70. if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
  71. || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
  72. return target.pasteFromClipboard();
  73. // NB: checking for delete must happen after the earlier check for shift + delete
  74. if (numCtrlAltCommandKeys < 2)
  75. {
  76. if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
  77. if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
  78. }
  79. if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  80. return target.selectAll();
  81. if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
  82. return target.undo();
  83. if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
  84. || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
  85. return target.redo();
  86. return false;
  87. }
  88. };
  89. } // namespace juce