juce_TextEditorKeyMapper.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 (numCtrlAltCommandKeys < 2)
  68. {
  69. if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
  70. if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
  71. }
  72. if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
  73. || key == KeyPress (KeyPress::insertKey, ModifierKeys::ctrlModifier, 0))
  74. return target.copyToClipboard();
  75. if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
  76. || key == KeyPress (KeyPress::deleteKey, ModifierKeys::shiftModifier, 0))
  77. return target.cutToClipboard();
  78. if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
  79. || key == KeyPress (KeyPress::insertKey, ModifierKeys::shiftModifier, 0))
  80. return target.pasteFromClipboard();
  81. if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
  82. return target.selectAll();
  83. if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
  84. return target.undo();
  85. if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
  86. || key == KeyPress ('z', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0))
  87. return target.redo();
  88. return false;
  89. }
  90. };
  91. #endif // JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED