|
You have to do something like the following in your
implementation of
public void keyPressed(KeyEvent e){} Don't do it in keyTyped because that is for higher level key events. either:if ((keycode==KeyEvent.VK_NUMPAD7) &&
(keyevent.isAltDown())) doThis();
or if ((keycode==KeyEvent.VK_NUMPAD7) && (modifier == InputEvent.ALT_MASK)) doThat(); as in the following demo code which pretty much
covers everythiing you can do with a key event except the consume ()
methods. You can check out exactly what happens with every key event here
and test your keyboard out. I noted that if I pressed the ALT key by itself
and released it then pressed SHIFT that nothing happened. That was strange... I
am sure we could all find quirks in our keyboards.
/* WhichKeyDidYouPress.java * @author: Charles Bell * @version: Aug 18, 2001 * Copyright � 2001 QuantumHyperSpace.com * <[EMAIL PROTECTED]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. (http://www.fsf.org/copyleft/lgpl.html) * * 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 Library General Public License for more details. * * You can request a copy of the GNU Library General Public * License by writing to the Free Software Foundation at: * Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.awt.event.*; import java.util.Date; import java.awt.Toolkit; import javax.swing.*; /** WhichKeyDidYouPress */ public class WhichKeyDidYouPress extends JApplet implements KeyListener{ JTextArea display; boolean inanapplet = true; /** In an Applet the main method is not invoked, so that the boolean * inanapplet will remain true there. In an application, inanapplet * must be set to false; */ public static void main(String[] args){ WhichKeyDidYouPress whichkey = new WhichKeyDidYouPress(); whichkey.inanapplet = false; whichkey.init(); } /** */ public void init(){ JLabel label = new JLabel("Press any combination of keys."); getContentPane().add(label ,"North"); display = new JTextArea(20,70); display.setEditable(false); getContentPane().add(new JScrollPane(display),"Center"); display.addKeyListener(this); if (!inanapplet){ JFrame frame = new JFrame("Which Key Did You Press?"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(this,"Center"); frame.pack(); frame.show(); } } public void keyTyped(KeyEvent keyevent){ char c = keyevent.getKeyChar(); int keycode = keyevent.getKeyCode(); int modifiers = keyevent.getModifiers(); println("You typed Key " + KeyEvent.getKeyText(keycode)); } public void keyPressed(KeyEvent keyevent){ char c = keyevent.getKeyChar(); int keycode = keyevent.getKeyCode(); int modifier = keyevent.getModifiers(); long when = keyevent.getWhen(); Date whendate = new Date(when); println("You Pressed Key " + KeyEvent.getKeyText(keycode) + " with modifier " + KeyEvent.getKeyModifiersText(modifier) + " Character: " + String.valueOf(c) + " Key Code: " + String.valueOf(keycode) + " Modifier integer: " + String.valueOf(modifier) + " at " + whendate.toString()); if (keyevent.isAltDown()) println("The ALT key is down"); if (keyevent.isAltGraphDown()) println("The ALTGRAPH key is down"); if (keyevent.isControlDown()) println("The CONTROL key is down"); if (keyevent.isShiftDown()) println("The SHIFT key is down"); if (keyevent.isMetaDown()) println("The META key is down"); if ((keycode==KeyEvent.VK_NUMPAD7) && (keyevent.isAltDown())) beep(); if ((keycode==KeyEvent.VK_NUMPAD7) && (modifier == InputEvent.ALT_MASK)) flash(); } public void keyReleased(KeyEvent keyevent){ char c = keyevent.getKeyChar(); int keycode = keyevent.getKeyCode(); println("You Released Key " + KeyEvent.getKeyText(keycode)); } private void println(String s){ display.append(s + "\n"); display.setCaretPosition(display.getText().length()); } private void beep(){ Toolkit.getDefaultToolkit().beep(); } private void flash(){ display.setText("FLASH FLASH FLASH FLASH FLASH"); } }
|
- [JAVA3D] Lorena
- [JAVA3D] matthias sweertvaegher
- Re: [JAVA3D] Andrea Tartaro
- [JAVA3D] Lorena
- Re: [JAVA3D] Corysia Taware
- Re: [JAVA3D] Juan Miguel
- Re: [JAVA3D] Paul Pantera
- [JAVA3D] Christopher Cowen
- [JAVA3D] Patrik M�ller
- Re: [JAVA3D] Gary L. Graf
- [JAVA3D] Charles Bell at home
- [JAVA3D] Gianni Riccio
- [JAVA3D] Patrik M�ller
- [JAVA3D] What does this StackTrac... Joachim Diepstraten
- [JAVA3D] VAHID HAJALI
- Re: [JAVA3D] DigitalSedition Developer Relations
- [JAVA3D] Carlos Augusto Gurgel de Sousa
- [JAVA3D] Carlos Augusto Gurgel de Sousa
- Re: [JAVA3D] Thomas Gilbert Giusepe
- [JAVA3D] NullPointerException usi... Daniel Selman
- [JAVA3D] Fredrik Andersson
