openshot-audio  0.1.2
juce_TextEditorKeyMapper.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED
26 #define JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED
27 
28 
29 //==============================================================================
35 template <class CallbackClass>
37 {
41  static bool invokeKeyFunction (CallbackClass& target, const KeyPress& key)
42  {
43  const ModifierKeys& mods = key.getModifiers();
44 
45  const bool isShiftDown = mods.isShiftDown();
46  const bool ctrlOrAltDown = mods.isCtrlDown() || mods.isAltDown();
47 
48  int numCtrlAltCommandKeys = 0;
49  if (mods.isCtrlDown()) ++numCtrlAltCommandKeys;
50  if (mods.isAltDown()) ++numCtrlAltCommandKeys;
51 
52  if (key == KeyPress (KeyPress::downKey, ModifierKeys::ctrlModifier, 0) && target.scrollUp()) return true;
53  if (key == KeyPress (KeyPress::upKey, ModifierKeys::ctrlModifier, 0) && target.scrollDown()) return true;
54 
55  #if JUCE_MAC
56  if (mods.isCommandDown() && ! ctrlOrAltDown)
57  {
58  if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretToTop (isShiftDown);
59  if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretToEnd (isShiftDown);
60  if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretToStartOfLine (isShiftDown);
61  if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretToEndOfLine (isShiftDown);
62  }
63 
64  if (mods.isCommandDown())
65  ++numCtrlAltCommandKeys;
66  #endif
67 
68  if (numCtrlAltCommandKeys < 2)
69  {
70  if (key.isKeyCode (KeyPress::leftKey)) return target.moveCaretLeft (ctrlOrAltDown, isShiftDown);
71  if (key.isKeyCode (KeyPress::rightKey)) return target.moveCaretRight (ctrlOrAltDown, isShiftDown);
72 
73  if (key.isKeyCode (KeyPress::homeKey)) return ctrlOrAltDown ? target.moveCaretToTop (isShiftDown)
74  : target.moveCaretToStartOfLine (isShiftDown);
75  if (key.isKeyCode (KeyPress::endKey)) return ctrlOrAltDown ? target.moveCaretToEnd (isShiftDown)
76  : target.moveCaretToEndOfLine (isShiftDown);
77  }
78 
79  if (numCtrlAltCommandKeys == 0)
80  {
81  if (key.isKeyCode (KeyPress::upKey)) return target.moveCaretUp (isShiftDown);
82  if (key.isKeyCode (KeyPress::downKey)) return target.moveCaretDown (isShiftDown);
83 
84  if (key.isKeyCode (KeyPress::pageUpKey)) return target.pageUp (isShiftDown);
85  if (key.isKeyCode (KeyPress::pageDownKey)) return target.pageDown (isShiftDown);
86  }
87 
88  if (key == KeyPress ('c', ModifierKeys::commandModifier, 0)
90  return target.copyToClipboard();
91 
92  if (key == KeyPress ('x', ModifierKeys::commandModifier, 0)
94  return target.cutToClipboard();
95 
96  if (key == KeyPress ('v', ModifierKeys::commandModifier, 0)
98  return target.pasteFromClipboard();
99 
100  // NB: checking for delete must happen after the earlier check for shift + delete
101  if (numCtrlAltCommandKeys < 2)
102  {
103  if (key.isKeyCode (KeyPress::backspaceKey)) return target.deleteBackwards (ctrlOrAltDown);
104  if (key.isKeyCode (KeyPress::deleteKey)) return target.deleteForwards (ctrlOrAltDown);
105  }
106 
107  if (key == KeyPress ('a', ModifierKeys::commandModifier, 0))
108  return target.selectAll();
109 
110  if (key == KeyPress ('z', ModifierKeys::commandModifier, 0))
111  return target.undo();
112 
113  if (key == KeyPress ('y', ModifierKeys::commandModifier, 0)
115  return target.redo();
116 
117  return false;
118  }
119 };
120 
121 
122 #endif // JUCE_TEXTEDITORKEYMAPPER_H_INCLUDED
static const int pageUpKey
Definition: juce_KeyPress.h:197
static const int endKey
Definition: juce_KeyPress.h:200
bool isCtrlDown() const noexcept
Definition: juce_ModifierKeys.h:106
static const int deleteKey
Definition: juce_KeyPress.h:189
Definition: juce_KeyPress.h:37
Definition: juce_TextEditorKeyMapper.h:36
static const int insertKey
Definition: juce_KeyPress.h:191
static const int leftKey
Definition: juce_KeyPress.h:195
static const int downKey
Definition: juce_KeyPress.h:194
Definition: juce_ModifierKeys.h:38
bool isCommandDown() const noexcept
Definition: juce_ModifierKeys.h:66
static const int backspaceKey
Definition: juce_KeyPress.h:190
bool isAltDown() const noexcept
Definition: juce_ModifierKeys.h:109
Definition: juce_ModifierKeys.h:119
static const int homeKey
Definition: juce_KeyPress.h:199
static const int pageDownKey
Definition: juce_KeyPress.h:198
Definition: juce_ModifierKeys.h:122
bool isShiftDown() const noexcept
Definition: juce_ModifierKeys.h:97
static const int rightKey
Definition: juce_KeyPress.h:196
static const int upKey
Definition: juce_KeyPress.h:193
ModifierKeys getModifiers() const noexcept
Definition: juce_KeyPress.h:110
static bool invokeKeyFunction(CallbackClass &target, const KeyPress &key)
Definition: juce_TextEditorKeyMapper.h:41
bool isKeyCode(int keyCodeToCompare) const noexcept
Definition: juce_KeyPress.h:127
Definition: juce_ModifierKeys.h:145