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");

}

}

----- Original Message -----
Sent: Saturday, August 18, 2001 8:46 AM
Subject: [JAVA3D]

Hi
 
I would like to have a key Listener like that:
Only if I press the Key "ALT" together with the Key "Numlock 7"  that it is possible to go into this "if" part but it does not work. 
Why?
 
  if (e.getKeyCode()==KeyEvent.VK_NUMPAD7 && e.getKeyCode()==KeyEvent.VK_ALT ) {
  ....
  }
 
 
 

 

Patrik

 

E-mail mailto: [EMAIL PROTECTED]

 

 

Reply via email to