2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_TEXTEDITORKEYMAPPER_JUCEHEADER__
27 #define __JUCE_TEXTEDITORKEYMAPPER_JUCEHEADER__
29 #include "juce_KeyPress.h"
32 //==============================================================================
33 /** This class is used to invoke a range of text-editor navigation methods on
34 an object, based upon a keypress event.
36 It's currently used internally by the TextEditor and CodeEditorComponent.
38 template <class CallbackClass
>
39 struct TextEditorKeyMapper
41 /** Checks the keypress and invokes one of a range of navigation functions that
42 the target class must implement, based on the key event.
44 static bool invokeKeyFunction (CallbackClass
& target
, const KeyPress
& key
)
46 const bool isShiftDown
= key
.getModifiers().isShiftDown();
47 const bool ctrlOrAltDown
= key
.getModifiers().isCtrlDown() || key
.getModifiers().isAltDown();
49 if (key
== KeyPress (KeyPress::downKey
, ModifierKeys::ctrlModifier
, 0)
53 if (key
== KeyPress (KeyPress::upKey
, ModifierKeys::ctrlModifier
, 0)
54 && target
.scrollDown())
58 if (key
.getModifiers().isCommandDown())
60 if (key
.isKeyCode (KeyPress::upKey
))
61 return target
.moveCaretToTop (isShiftDown
);
63 if (key
.isKeyCode (KeyPress::downKey
))
64 return target
.moveCaretToEnd (isShiftDown
);
66 if (key
.isKeyCode (KeyPress::leftKey
))
67 return target
.moveCaretToStartOfLine (isShiftDown
);
69 if (key
.isKeyCode (KeyPress::rightKey
))
70 return target
.moveCaretToEndOfLine (isShiftDown
);
74 if (key
.isKeyCode (KeyPress::upKey
))
75 return target
.moveCaretUp (isShiftDown
);
77 if (key
.isKeyCode (KeyPress::downKey
))
78 return target
.moveCaretDown (isShiftDown
);
80 if (key
.isKeyCode (KeyPress::leftKey
))
81 return target
.moveCaretLeft (ctrlOrAltDown
, isShiftDown
);
83 if (key
.isKeyCode (KeyPress::rightKey
))
84 return target
.moveCaretRight (ctrlOrAltDown
, isShiftDown
);
86 if (key
.isKeyCode (KeyPress::pageUpKey
))
87 return target
.pageUp (isShiftDown
);
89 if (key
.isKeyCode (KeyPress::pageDownKey
))
90 return target
.pageDown (isShiftDown
);
92 if (key
.isKeyCode (KeyPress::homeKey
))
93 return ctrlOrAltDown
? target
.moveCaretToTop (isShiftDown
)
94 : target
.moveCaretToStartOfLine (isShiftDown
);
96 if (key
.isKeyCode (KeyPress::endKey
))
97 return ctrlOrAltDown
? target
.moveCaretToEnd (isShiftDown
)
98 : target
.moveCaretToEndOfLine (isShiftDown
);
100 if (key
== KeyPress ('c', ModifierKeys::commandModifier
, 0)
101 || key
== KeyPress (KeyPress::insertKey
, ModifierKeys::ctrlModifier
, 0))
102 return target
.copyToClipboard();
104 if (key
== KeyPress ('x', ModifierKeys::commandModifier
, 0)
105 || key
== KeyPress (KeyPress::deleteKey
, ModifierKeys::shiftModifier
, 0))
106 return target
.cutToClipboard();
108 if (key
== KeyPress ('v', ModifierKeys::commandModifier
, 0)
109 || key
== KeyPress (KeyPress::insertKey
, ModifierKeys::shiftModifier
, 0))
110 return target
.pasteFromClipboard();
112 if (key
.isKeyCode (KeyPress::backspaceKey
))
113 return target
.deleteBackwards (ctrlOrAltDown
);
115 if (key
.isKeyCode (KeyPress::deleteKey
))
116 return target
.deleteForwards (ctrlOrAltDown
);
118 if (key
== KeyPress ('a', ModifierKeys::commandModifier
, 0))
119 return target
.selectAll();
121 if (key
== KeyPress ('z', ModifierKeys::commandModifier
, 0))
122 return target
.undo();
124 if (key
== KeyPress ('y', ModifierKeys::commandModifier
, 0)
125 || key
== KeyPress ('z', ModifierKeys::commandModifier
| ModifierKeys::shiftModifier
, 0))
126 return target
.redo();
133 #endif // __JUCE_TEXTEDITORKEYMAPPER_JUCEHEADER__