/** Controls the movements and actions of character and tetris pieces */ public void keyPressed(KeyEvent key) { String keyText = KeyEvent.getKeyText(key.getKeyCode()); if (!pause) { if (!shapeAI.getAI()) { if (keyText.equalsIgnoreCase("a")) { shapes.get(0).moveLeft(map); shapeTrack.add(1); } if (keyText.equalsIgnoreCase("d")) { shapes.get(0).moveRight(map); shapeTrack.add(2); } if (keyText.equalsIgnoreCase("w")) { shapes.get(0).turn(map, shapeTrack); } } if (keyText.equalsIgnoreCase("left")) { character.moveLeft(); } if (keyText.equalsIgnoreCase("right")) { character.moveRight(); } if (keyText.equalsIgnoreCase("up")) { character.jump(); } if (keyText.equalsIgnoreCase("Slash")) { guntrack += 1; if (guntrack > weapons.length - 1) { guntrack = 0; } } } repaint(); }
// Special case for OS X, where Ctrl-Space is not detected as KEY_TYPED // https://github.com/processing/processing/issues/2699 private void processControlSpace(final KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_SPACE && event.isControlDown()) { // Provide completions only if it's enabled if (JavaMode.codeCompletionsEnabled) { Messages.log( "[KeyEvent]" + KeyEvent.getKeyText(event.getKeyCode()) + " |Prediction started"); fetchPhrase(); } } }
// keypressed public void keyPressed(KeyEvent e) { int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_ESCAPE) { // VK_ what ever stop(); } else { mess = "You pressed: " + KeyEvent.getKeyText(KeyCode); e.consume(); // not allowing key combinations } }
public void keyReleased(KeyEvent key) { String keyText = KeyEvent.getKeyText(key.getKeyCode()); if (!pause) { if (keyText.equalsIgnoreCase("left")) { character.stop(); } if (keyText.equalsIgnoreCase("right")) { character.stop(); } } repaint(); }
private void displayInfo(KeyEvent e, String keyStatus) { // Method copied from http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html // You should only rely on the key char if the event // is a key typed event. int id = e.getID(); String keyString; if (id == KeyEvent.KEY_TYPED) { char c = e.getKeyChar(); keyString = "key character = '" + c + "'"; } else { int keyCode = e.getKeyCode(); keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")"; } int modifiersEx = e.getModifiersEx(); String modString = "extended modifiers = " + modifiersEx; String tmpString = KeyEvent.getModifiersExText(modifiersEx); if (tmpString.length() > 0) { modString += " (" + tmpString + ")"; } else { modString += " (no extended modifiers)"; } String actionString = "action key? "; if (e.isActionKey()) { actionString += "YES"; } else { actionString += "NO"; } String locationString = "key location: "; int location = e.getKeyLocation(); if (location == KeyEvent.KEY_LOCATION_STANDARD) { locationString += "standard"; } else if (location == KeyEvent.KEY_LOCATION_LEFT) { locationString += "left"; } else if (location == KeyEvent.KEY_LOCATION_RIGHT) { locationString += "right"; } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) { locationString += "numpad"; } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN) locationString += "unknown"; } // Added: System.out.println("Keypress: " + keyString); }
private void processCompletionKeys(final KeyEvent event) { char keyChar = event.getKeyChar(); int keyCode = event.getKeyCode(); if (keyChar == KeyEvent.VK_ENTER || keyChar == KeyEvent.VK_ESCAPE || keyChar == KeyEvent.VK_TAB || (event.getID() == KeyEvent.KEY_RELEASED && keyCode != KeyEvent.VK_LEFT && keyCode != KeyEvent.VK_RIGHT)) { // ignore } else if (keyChar == ')') { // https://github.com/processing/processing/issues/2741 hideSuggestion(); } else if (keyChar == '.') { if (JavaMode.codeCompletionsEnabled) { Messages.log( "[KeyEvent]" + KeyEvent.getKeyText(event.getKeyCode()) + " |Prediction started"); fetchPhrase(); } } else if (keyChar == ' ') { // Trigger on Ctrl-Space if (!Platform.isMacOS() && JavaMode.codeCompletionsEnabled && (event.isControlDown() || event.isMetaDown())) { // Provide completions only if it's enabled if (JavaMode.codeCompletionsEnabled) { // Removed for https://github.com/processing/processing/issues/3847 // try { // getDocument().remove(getCaretPosition() - 1, 1); // Remove the typed space Messages.log("[KeyEvent]" + event.getKeyChar() + " |Prediction started"); fetchPhrase(); // } catch (BadLocationException e) { // e.printStackTrace(); // } } } else { hideSuggestion(); // hide on spacebar } } else { if (JavaMode.codeCompletionsEnabled) { prepareSuggestions(event); } } }
public void keyPressed(KeyEvent e) { // if (e.isConsumed()) return; int keyCode = e.getKeyCode(); IJ.setKeyDown(keyCode); hotkey = false; if (keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_SHIFT) return; char keyChar = e.getKeyChar(); int flags = e.getModifiers(); if (IJ.debugMode) IJ.log( "keyPressed: code=" + keyCode + " (" + KeyEvent.getKeyText(keyCode) + "), char=\"" + keyChar + "\" (" + (int) keyChar + "), flags=" + KeyEvent.getKeyModifiersText(flags)); boolean shift = (flags & KeyEvent.SHIFT_MASK) != 0; boolean control = (flags & KeyEvent.CTRL_MASK) != 0; boolean alt = (flags & KeyEvent.ALT_MASK) != 0; boolean meta = (flags & KeyEvent.META_MASK) != 0; String cmd = null; ImagePlus imp = WindowManager.getCurrentImage(); boolean isStack = (imp != null) && (imp.getStackSize() > 1); if (imp != null && !control && ((keyChar >= 32 && keyChar <= 255) || keyChar == '\b' || keyChar == '\n')) { Roi roi = imp.getRoi(); if (roi instanceof TextRoi) { if ((flags & KeyEvent.META_MASK) != 0 && IJ.isMacOSX()) return; if (alt) { switch (keyChar) { case 'u': case 'm': keyChar = IJ.micronSymbol; break; case 'A': keyChar = IJ.angstromSymbol; break; default: } } ((TextRoi) roi).addChar(keyChar); return; } } // Handle one character macro shortcuts if (!control && !meta) { Hashtable macroShortcuts = Menus.getMacroShortcuts(); if (macroShortcuts.size() > 0) { if (shift) cmd = (String) macroShortcuts.get(new Integer(keyCode + 200)); else cmd = (String) macroShortcuts.get(new Integer(keyCode)); if (cmd != null) { // MacroInstaller.runMacroCommand(cmd); commandName = cmd; MacroInstaller.runMacroShortcut(cmd); return; } } } if ((!Prefs.requireControlKey || control || meta) && keyChar != '+') { Hashtable shortcuts = Menus.getShortcuts(); if (shift) cmd = (String) shortcuts.get(new Integer(keyCode + 200)); else cmd = (String) shortcuts.get(new Integer(keyCode)); } if (cmd == null) { switch (keyChar) { case '<': case ',': if (isStack) cmd = "Previous Slice [<]"; break; case '>': case '.': case ';': if (isStack) cmd = "Next Slice [>]"; break; case '+': case '=': cmd = "In [+]"; break; case '-': cmd = "Out [-]"; break; case '/': cmd = "Reslice [/]..."; break; default: } } if (cmd == null) { switch (keyCode) { case KeyEvent.VK_TAB: WindowManager.putBehind(); return; case KeyEvent.VK_BACK_SPACE: // delete if (deleteOverlayRoi(imp)) return; cmd = "Clear"; hotkey = true; break; // case KeyEvent.VK_BACK_SLASH: cmd=IJ.altKeyDown()?"Animation Options...":"Start // Animation"; break; case KeyEvent.VK_EQUALS: cmd = "In [+]"; break; case KeyEvent.VK_MINUS: cmd = "Out [-]"; break; case KeyEvent.VK_SLASH: case 0xbf: cmd = "Reslice [/]..."; break; case KeyEvent.VK_COMMA: case 0xbc: if (isStack) cmd = "Previous Slice [<]"; break; case KeyEvent.VK_PERIOD: case 0xbe: if (isStack) cmd = "Next Slice [>]"; break; case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: // arrow keys if (imp == null) return; Roi roi = imp.getRoi(); if (IJ.shiftKeyDown() && imp == Orthogonal_Views.getImage()) return; boolean stackKey = imp.getStackSize() > 1 && (roi == null || IJ.shiftKeyDown()); boolean zoomKey = roi == null || IJ.shiftKeyDown() || IJ.controlKeyDown(); if (stackKey && keyCode == KeyEvent.VK_RIGHT) cmd = "Next Slice [>]"; else if (stackKey && keyCode == KeyEvent.VK_LEFT) cmd = "Previous Slice [<]"; else if (zoomKey && keyCode == KeyEvent.VK_DOWN && !ignoreArrowKeys(imp) && Toolbar.getToolId() < Toolbar.SPARE6) cmd = "Out [-]"; else if (zoomKey && keyCode == KeyEvent.VK_UP && !ignoreArrowKeys(imp) && Toolbar.getToolId() < Toolbar.SPARE6) cmd = "In [+]"; else if (roi != null) { if ((flags & KeyEvent.ALT_MASK) != 0) roi.nudgeCorner(keyCode); else roi.nudge(keyCode); return; } break; case KeyEvent.VK_ESCAPE: abortPluginOrMacro(imp); return; case KeyEvent.VK_ENTER: WindowManager.toFront(this); return; default: break; } } if (cmd != null && !cmd.equals("")) { commandName = cmd; if (cmd.equals("Fill") || cmd.equals("Draw")) hotkey = true; if (cmd.charAt(0) == MacroInstaller.commandPrefix) MacroInstaller.runMacroShortcut(cmd); else { doCommand(cmd); keyPressedTime = System.currentTimeMillis(); lastKeyCommand = cmd; } } }
public void keyTyped(KeyEvent e) { if (debug) { System.out.println( "--- RecordingModule: key typed = " + e + "\n > Key char->int = " + (int) e.getKeyChar()); System.out.println(" -- isActionKey() = " + e.isActionKey()); System.out.println(" -- isISOControl() = " + Character.isISOControl(e.getKeyChar())); System.out.println(" -- isWhitespace() = " + Character.isWhitespace(e.getKeyChar())); } if (isKeyReserved(e)) { return; } if (enabled && !readOnly && lastKeyPressEvent != null) { if (enableKeyboard) { boolean replace = false; String text = ""; if (isControl(e)) { if (lastKeyPressEvent.getKeyCode() == KeyEvent.VK_ENTER) { // Change the Type cmd prior to Typeline if the delay from the last type key is less // than 1 sec if (useTypeline && e.getModifiers() == 0 && lastElement != null) { String s = DocumentUtils.getElementText(lastElement); if (s.startsWith("Type ") && (System.currentTimeMillis() - lastInsertTime) < typelineDelay) { replace = true; text = s.replaceFirst("Type", "Typeline"); } } } if ("".equals(text)) { int count = 1; KeyEvent e2; long lastEventTime = e.getWhen(); // We go through the vector of events and check whether there are events corresponding // to a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (e.getID() == e2.getID() && e.getKeyChar() == e2.getKeyChar() && e.getKeyCode() == e2.getKeyCode() && e.getModifiers() == e2.getModifiers() && (lastEventTime - e2.getWhen() < keyMutiDelay)) { count++; replace = true; lastEventTime = e2.getWhen(); } else { break; } } text = "Press "; // String modifiers = KeyEvent.getKeyModifiersText(e.getModifiers()); String modifiers = parser.modifiersToString(e.getModifiers()); if (!"".equals(modifiers)) { text += modifiers + "+"; } String charText = KeyEvent.getKeyText(lastKeyPressEvent.getKeyCode()); if (charText == null) { charText = "<unknown>"; } text += charText; if (count > 1) { text += " " + PressCommand.PARAM_COUNT + "=" + count; } if (debug) { System.out.println("--- RecordingModule: Inserting '" + text + "'"); } } } else { text = "" + e.getKeyChar(); KeyEvent e2; // We go through the vector of events and check whether there are events corresponding to // a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (!isControl(e2) && !e2.isActionKey()) { text = e2.getKeyChar() + text; replace = true; } else { break; } } text = "Type \"" + Utils.escapeUnescapedDoubleQuotes(text) + "\""; } // Insert the command to the current editor insertLine(text, replace, true, false); } insertEvent(e); } }
private void jbInit() throws Exception { confirm = new JButton(ls.getString(16)); cancel = new JButton(ls.getString(18)); pauseRB = new JRadioButton(ls.getString(22)); leftRB = new JRadioButton(ls.getString(13)); rightRB = new JRadioButton(ls.getString(14)); Container container = this.getContentPane(); titledBorder1 = new TitledBorder(ls.getString(11)); container.setLayout(null); leftL.setEditable(false); leftL.setBounds(new Rectangle(116, 24, 84, 20)); leftL.setText( gameconfigure.getLeftKeyCode() + " " + KeyEvent.getKeyText(gameconfigure.getLeftKeyCode())); leftRB.setBounds(new Rectangle(5, 23, 97, 20)); leftRB.addActionListener(new KeyOptionDialog_leftRB_actionAdapter(this)); rightRB.setBounds(new Rectangle(5, 48, 97, 20)); rightRB.addActionListener(new KeyOptionDialog_rightRB_actionAdapter(this)); rightL.setText( gameconfigure.getRightKeyCode() + " " + KeyEvent.getKeyText(gameconfigure.getRightKeyCode())); rightL.setEditable(false); rightL.setBounds(new Rectangle(116, 49, 84, 20)); confirm.setBounds(new Rectangle(110, 114, 85, 20)); confirm.addActionListener(new KeyOptionDialog_confirm_actionAdapter(this)); cancel.setBounds(new Rectangle(18, 114, 85, 20)); cancel.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cancel_actionPerformed(e); } }); pauseRB.setBounds(new Rectangle(6, 74, 97, 20)); pauseRB.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { pauseRB_actionPerformed(e); } }); pauseL.setText( gameconfigure.getPausekeyCode() + " " + KeyEvent.getKeyText(gameconfigure.getPausekeyCode())); pauseL.setBounds(new Rectangle(117, 75, 84, 20)); pauseL.setEditable(false); container.add(leftL, null); container.add(rightL, null); container.add(rightRB, null); container.add(leftRB, null); container.add(pauseL, null); container.add(confirm, null); container.add(cancel, null); container.add(pauseRB, null); keylistener = new KeyAdapter() { public void keyPressed(KeyEvent e) { switch (KeyOptionDialog.this.selecti) { case 1: leftL.setText(e.getKeyCode() + " " + e.getKeyText(e.getKeyCode())); gameconfigure.setLeftKeyCode(e.getKeyCode()); break; case 2: rightL.setText(e.getKeyCode() + " " + e.getKeyText(e.getKeyCode())); gameconfigure.setRightKeyCode(e.getKeyCode()); break; case 6: pauseL.setText(e.getKeyCode() + " " + e.getKeyText(e.getKeyCode())); gameconfigure.setPausekeyCode(e.getKeyCode()); break; } } }; Component[] com = container.getComponents(); for (int i = 0; i < com.length; i++) { com[i].addKeyListener(keylistener); } buttonGroup.add(leftRB); buttonGroup.add(rightRB); buttonGroup.add(pauseRB); }
// key released public void keyReleased(KeyEvent e) { int KeyCode = e.getKeyCode(); mess = "You released: " + KeyEvent.getKeyText(KeyCode); e.consume(); }