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.

juce_TextEditorKeyMapper.h 5.3KB

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