forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdeInputHandler.java
More file actions
285 lines (231 loc) · 10.6 KB
/
Copy pathPdeInputHandler.java
File metadata and controls
285 lines (231 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
PdeInputHandler - PDE-specific handling of keys
Part of the Processing project - http://processing.org
Copyright (c) 2012-14 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-03 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.app.syntax;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import processing.app.Platform;
import processing.app.Preferences;
import processing.app.ui.Editor;
/**
* Sets key bindings used by the PDE, except for those that are Mode-specific.
* Not part of the original jeditsyntax DefaultInputHandler because it makes
* use of Preferences and other PDE classes.
*/
public class PdeInputHandler extends DefaultInputHandler {
/**
* Need the Editor object for Input Method changes, plus most subclasses
* will want a local copy anyway. Changed after Processing 3.1.2, need to
* see if this breaks any other Modes before releasing.
*/
protected Editor editor;
/**
* Recommended constructor.
* @since 3.2
*/
public PdeInputHandler(Editor editor) {
// Make sure the default constructor is called to set up the basics
this();
this.editor = editor;
}
/**
* Not recommended, but included for API compatibility.
*/
public PdeInputHandler() {
// Use option on mac for text edit controls that are ctrl on Windows/Linux.
// (i.e. ctrl-left/right is option-left/right on OS X)
String mod = Platform.isMacOS() ? "A" : "C";
// right now, ctrl-up/down is select up/down, but mod should be
// used instead, because the mac expects it to be option(alt)
addKeyBinding("BACK_SPACE", InputHandler.BACKSPACE);
// for 0122, shift-backspace is delete, for 0176, it's now a preference,
// to prevent holy warriors from attacking me for it.
if (Preferences.getBoolean("editor.keys.shift_backspace_is_delete")) {
addKeyBinding("S+BACK_SPACE", InputHandler.DELETE);
} else {
// Made the default for 0215, deemed better for our audience.
addKeyBinding("S+BACK_SPACE", InputHandler.BACKSPACE);
}
addKeyBinding("DELETE", InputHandler.DELETE);
addKeyBinding("S+DELETE", InputHandler.DELETE);
// the following two were changed for 0122 for better mac/pc compatability
addKeyBinding(mod + "+BACK_SPACE", InputHandler.BACKSPACE_WORD); // 0122
addKeyBinding(mod + "S+BACK_SPACE", InputHandler.BACKSPACE_WORD); // 0215
addKeyBinding(mod + "+DELETE", InputHandler.DELETE_WORD); // 0122
addKeyBinding(mod + "S+DELETE", InputHandler.DELETE_WORD); // 0215
// handled by listener, don't bother here
//addKeyBinding("ENTER", InputHandler.INSERT_BREAK);
//addKeyBinding("TAB", InputHandler.INSERT_TAB);
addKeyBinding("INSERT", InputHandler.OVERWRITE);
// http://dev.processing.org/bugs/show_bug.cgi?id=162
// added for 0176, though the bindings do not appear relevant for osx
if (Preferences.getBoolean("editor.keys.alternative_cut_copy_paste")) {
addKeyBinding("C+INSERT", InputHandler.CLIPBOARD_COPY);
addKeyBinding("S+INSERT", InputHandler.CLIPBOARD_PASTE);
addKeyBinding("S+DELETE", InputHandler.CLIPBOARD_CUT);
}
// disabling for 0122, not sure what this does
//addKeyBinding("C+\\", InputHandler.TOGGLE_RECT);
// for 0122, these have been changed for better compatibility
// HOME and END now mean the beginning/end of the document
// for 0176 changed this to a preference so that the Mac OS X people
// can get the "normal" behavior as well if they prefer.
if (Preferences.getBoolean("editor.keys.home_and_end_travel_far")) {
addKeyBinding("HOME", InputHandler.DOCUMENT_HOME);
addKeyBinding("END", InputHandler.DOCUMENT_END);
addKeyBinding("S+HOME", InputHandler.SELECT_DOC_HOME);
addKeyBinding("S+END", InputHandler.SELECT_DOC_END);
} else {
// for 0123 added the proper windows defaults
addKeyBinding("HOME", InputHandler.HOME);
addKeyBinding("END", InputHandler.END);
addKeyBinding("S+HOME", InputHandler.SELECT_HOME);
addKeyBinding("S+END", InputHandler.SELECT_END);
addKeyBinding("C+HOME", InputHandler.DOCUMENT_HOME);
addKeyBinding("C+END", InputHandler.DOCUMENT_END);
addKeyBinding("CS+HOME", InputHandler.SELECT_DOC_HOME);
addKeyBinding("CS+END", InputHandler.SELECT_DOC_END);
}
if (Platform.isMacOS()) {
// Additional OS X key bindings added for 0215.
// Also note that two more are added above and marked 0215.
// http://code.google.com/p/processing/issues/detail?id=1354
// Could not find a proper Apple guide, but a partial reference is here:
// http://guides.macrumors.com/Keyboard_shortcuts§ion=10#Text_Shortcuts
// control-A move to start of current paragraph
addKeyBinding("C+A", InputHandler.HOME);
addKeyBinding("CS+A", InputHandler.SELECT_HOME);
// control-E move to end of current paragraph
addKeyBinding("C+E", InputHandler.END);
addKeyBinding("CS+E", InputHandler.SELECT_END);
// control-D forward delete
addKeyBinding("C+D", InputHandler.DELETE);
// control-B move left one character
addKeyBinding("C+B", InputHandler.PREV_CHAR);
addKeyBinding("CS+B", InputHandler.SELECT_PREV_CHAR);
// control-F move right one character
addKeyBinding("C+F", InputHandler.NEXT_CHAR);
addKeyBinding("CS+F", InputHandler.SELECT_NEXT_CHAR);
// control-H delete (just ASCII for backspace)
addKeyBinding("C+H", InputHandler.BACKSPACE);
// control-N move down one line
addKeyBinding("C+N", InputHandler.NEXT_LINE);
addKeyBinding("CS+N", InputHandler.SELECT_NEXT_LINE);
// control-P move up one line
addKeyBinding("C+P", InputHandler.PREV_LINE);
addKeyBinding("CS+P", InputHandler.SELECT_PREV_LINE);
// might be nice, but no handlers currently available
// control-O insert new line after cursor
// control-T transpose (swap) two surrounding character
// control-V move to end, then left one character
// control-K delete remainder of current paragraph
// control-Y paste text previously deleted with control-K
}
if (Platform.isMacOS()) {
addKeyBinding("M+LEFT", InputHandler.HOME);
addKeyBinding("M+RIGHT", InputHandler.END);
addKeyBinding("MS+LEFT", InputHandler.SELECT_HOME); // 0122
addKeyBinding("MS+RIGHT", InputHandler.SELECT_END); // 0122
} else {
addKeyBinding("C+LEFT", InputHandler.HOME); // 0122
addKeyBinding("C+RIGHT", InputHandler.END); // 0122
addKeyBinding("CS+HOME", InputHandler.SELECT_HOME); // 0122
addKeyBinding("CS+END", InputHandler.SELECT_END); // 0122
}
addKeyBinding("PAGE_UP", InputHandler.PREV_PAGE);
addKeyBinding("PAGE_DOWN", InputHandler.NEXT_PAGE);
addKeyBinding("S+PAGE_UP", InputHandler.SELECT_PREV_PAGE);
addKeyBinding("S+PAGE_DOWN", InputHandler.SELECT_NEXT_PAGE);
addKeyBinding("LEFT", InputHandler.PREV_CHAR);
addKeyBinding("S+LEFT", InputHandler.SELECT_PREV_CHAR);
addKeyBinding(mod + "+LEFT", InputHandler.PREV_WORD);
addKeyBinding(mod + "S+LEFT", InputHandler.SELECT_PREV_WORD);
addKeyBinding("RIGHT", InputHandler.NEXT_CHAR);
addKeyBinding("S+RIGHT", InputHandler.SELECT_NEXT_CHAR);
addKeyBinding(mod + "+RIGHT", InputHandler.NEXT_WORD);
addKeyBinding(mod + "S+RIGHT", InputHandler.SELECT_NEXT_WORD);
addKeyBinding("UP", InputHandler.PREV_LINE);
addKeyBinding(mod + "+UP", InputHandler.PREV_LINE); // p5
addKeyBinding("S+UP", InputHandler.SELECT_PREV_LINE);
addKeyBinding("DOWN", InputHandler.NEXT_LINE);
addKeyBinding(mod + "+DOWN", InputHandler.NEXT_LINE); // p5
addKeyBinding("S+DOWN", InputHandler.SELECT_NEXT_LINE);
addKeyBinding("MS+UP", InputHandler.SELECT_DOC_HOME);
addKeyBinding("CS+UP", InputHandler.SELECT_DOC_HOME);
addKeyBinding("MS+DOWN", InputHandler.SELECT_DOC_END);
addKeyBinding("CS+DOWN", InputHandler.SELECT_DOC_END);
addKeyBinding(mod + "+ENTER", InputHandler.REPEAT);
}
protected boolean isMnemonic(KeyEvent event) {
// Don't do this on OS X, because alt (the option key) is used for
// non-ASCII chars, and there are no menu mnemonics to speak of
if (!Platform.isMacOS()) {
if ((event.getModifiers() & InputEvent.ALT_MASK) != 0 &&
(event.getModifiers() & InputEvent.CTRL_MASK) == 0 &&
event.getKeyChar() != KeyEvent.VK_UNDEFINED) {
// This is probably a menu mnemonic, don't pass it through.
// If it's an alt-NNNN sequence, those only work on the keypad
// and pass through UNDEFINED as the keyChar.
return true;
}
}
return false;
}
public void keyPressed(KeyEvent event) {
// don't pass the ctrl-, through to the editor
// https://github.com/processing/processing/issues/3074
if ((event.getModifiers() & InputEvent.CTRL_MASK) != 0 &&
event.getKeyChar() == ',') {
return;
}
// don't pass menu mnemonics (alt-f for file, etc) to the editor
if (isMnemonic(event)) {
return;
}
if (!handlePressed(event)) {
super.keyPressed(event);
}
}
public void keyTyped(KeyEvent event) {
if (isMnemonic(event)) {
return;
}
if (!handleTyped(event)) {
super.keyTyped(event);
}
}
// we don't need keyReleased(), so that's passed through automatically
/**
* Override this function in your InputHandler to do any gymnastics.
* @return true if key has been handled (no further handling should be done)
*/
public boolean handlePressed(KeyEvent event) {
return false;
}
/**
* Override this instead of keyPressed/keyTyped
* @return true if key has been handled (no further handling should be done)
*/
public boolean handleTyped(KeyEvent event) {
return false;
}
public void handleInputMethodCommit() {
editor.getSketch().setModified(true);
}
}