MCPcopy Index your code
hub / github.com/processing/processing / keyTyped

Method keyTyped

app/src/processing/app/Sketch.java:370–422  ·  view source on GitHub ↗
(KeyEvent event)

Source from the content-addressed store, hash-verified

368 // zero, even if it's enter or backspace or whatever, so the keychar
369 // should be used instead. Grr.
370 public void keyTyped(KeyEvent event) {
371 //System.out.println("got event " + event);
372 char ch = event.getKeyChar();
373 if ((ch == '_') || (ch == '.') || // allow.pde and .java
374 (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z'))) {
375 // These events are allowed straight through.
376 } else if (ch == ' ') {
377 String t = field.getText();
378 int start = field.getSelectionStart();
379 int end = field.getSelectionEnd();
380 field.setText(t.substring(0, start) + "_" + t.substring(end));
381 field.setCaretPosition(start + 1);
382 event.consume();
383 } else if ((ch >= '0') && (ch <= '9')) {
384 // getCaretPosition == 0 means that it's the first char
385 // and the field is empty.
386 // getSelectionStart means that it *will be* the first
387 // char, because the selection is about to be replaced
388 // with whatever is typed.
389 if (field.getCaretPosition() == 0 ||
390 field.getSelectionStart() == 0) {
391 // number not allowed as first digit
392 event.consume();
393 }
394 } else if (ch == KeyEvent.VK_ENTER) {
395 // Slightly ugly hack that ensures OK button of the dialog consumes
396 // the Enter key event. Since the text field is the default component
397 // in the dialog, OK doesn't consume Enter key event, by default.
398 Container parent = field.getParent();
399 while (!(parent instanceof JOptionPane)) {
400 parent = parent.getParent();
401 }
402 JOptionPane pane = (JOptionPane) parent;
403 final JPanel pnlBottom = (JPanel)
404 pane.getComponent(pane.getComponentCount() - 1);
405 for (int i = 0; i < pnlBottom.getComponents().length; i++) {
406 Component component = pnlBottom.getComponents()[i];
407 if (component instanceof JButton) {
408 final JButton okButton = (JButton) component;
409 if (okButton.getText().equalsIgnoreCase("OK")) {
410 ActionListener[] actionListeners =
411 okButton.getActionListeners();
412 if (actionListeners.length > 0) {
413 actionListeners[0].actionPerformed(null);
414 event.consume();
415 }
416 }
417 }
418 }
419 } else {
420 event.consume();
421 }
422 }
423 });
424
425 int userReply = JOptionPane.showOptionDialog(editor, new Object[] {

Callers

nothing calls this directly

Calls 9

getKeyCharMethod · 0.80
setCaretPositionMethod · 0.80
getCaretPositionMethod · 0.80
getComponentMethod · 0.80
actionPerformedMethod · 0.65
getTextMethod · 0.45
getSelectionStartMethod · 0.45
setTextMethod · 0.45
getParentMethod · 0.45

Tested by

no test coverage detected