>>keyPressed method is not calling when hitting alt
Not sure what you mean. You mean when pressing <Alt>+<the mnemonic for the button>
>>or spacebar key
when the butotn has the focus I hope
Main Topics
Browse All TopicsIn my application when porting from jdk1.3 to jdk 1.4 then keyPressed method is not calling when hitting alt or spacebar key.
can anybody suggest me why this is not calling.I have implemented keyListener on eventhandler class
thanks in adv.
Anirudh Kumar
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
It would be a lot more easy to help you if you would react on our comments.
(See also your previous open Q: http://www.experts-exchang
i know it may sound silly, but have you added a listener to the component?
[component].addKeyListener
or if your subclassing the KeyListener class use:
[component].addKeyListener
public void keyPressed(KeyEvent key)
{
if ( key.getKeyCode() == KeyEvent.VK_ALT )
{
doSomething();
}
}
if you are trying to use an ALT + [key] combination, just include an additional if statement such
public void keyPressed(KeyEvent key)
{
if ( key.getKeyCode() == KeyEvent.VK_ALT )
{
if ( key.getKeyCode() == KeyEvent.VK_P)
doSomething();
}
}
hope it helps,
cheers affa
Hi johnny,
Thanks for ur suggestion but i tried this way earlier also and now also but not working.Actually I have a class like CityName.java.In this i am having one IvjEventHandler class where i have implementing keyListener. method keyPressed and keyreleased & key Typed all are written in IvjHandler class. In main class i have call a button bean class in which five buttons are there.when i am pressing Alt+I (Mnemonic) then keyPress are not working. I have written this.addKeyListener(object
But still it's not working.
I think I have the solution (if you're still interested):
1) Add the following function to your CityName class:
private void addKeyListenerRecursively(
{
//To be on the safe side, try to remove KeyListener first
//just in case it has been added before.
//If not, it won't do any harm
c.removeKeyListener(listen
//Add KeyListener to the Component passed as an argument
c.addKeyListener(listener)
if (c instanceof Container) {
//Component c is a Container. The following cast is safe.
Container cont = (Container)c;
//Get the Container's array of children Components.
Component[] children = cont.getComponents();
//For every child repeat the above operation.
for (int i = 0; i < children.length; i++) {
addKeyListenerRecursively(
}
}
}
2)
and instead of writing
this.addKeyListener(object
write
addKeyListenerRecursively(
Let me know
Here's a demo app if you're interested:
/*
* KeyPressDemo.java
*
*/
import java.awt.event.*;
import java.awt.*;
/**
*
*/
public class KeyPressDemo extends javax.swing.JFrame {
/** Creates new form KeyPressDemo */
public KeyPressDemo() {
initComponents();
// Don't write this // <<<<<<<<<<<<<<<<<<<< !!!!
//addKeyListener(new MyKeyListener());
// but this:
addKeyListenerRecursively(
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
getContentPane().setLayout
addWindowListener(new java.awt.event.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
jButton1.setText("Button 1");
getContentPane().add(jButt
pack();
}
private void exitForm(java.awt.event.Wi
System.exit(0);
}
public static void main(String args[]) {
new KeyPressDemo().show();
}
private javax.swing.JButton jButton1;
private void addKeyListenerRecursively(
{
//To be on the safe side, try to remove KeyListener first
//just in case it has been added before.
//If not, it won't do any harm
c.removeKeyListener(listen
//Add KeyListener to the Component passed as an argument
c.addKeyListener(listener)
if (c instanceof Container) {
//Component c is a Container. The following cast is safe.
Container cont = (Container)c;
//Get the Container's array of children Components.
Component[] children = cont.getComponents();
//For every child repeat the above operation.
for (int i = 0; i < children.length; i++) {
addKeyListenerRecursively(
}
}
}
public class MyKeyListener extends KeyAdapter {
public MyKeyListener() { }
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("Key released");
}
public void keyTyped(KeyEvent e) {
System.out.println("Key typed");
}
}
}
anirudh4uonly, here try this. it works test for ALT and then the number 1, when alt is pressed the background color is changed to blue and if 1 is pressed the color changes to red. i think this is kinda what your after;
import java.awt.*;
import java.awt.event.*;
class Key extends Frame
{
public Key()
{
this.setInterface();
}
private void setInterface()
{
this.setLayout(null);
this.setBounds(100,100,400
this.addKeyListener(new MyKeyListener(this));
this.setVisible(true);
}
}
class MyKeyListener extends KeyAdapter
{
Key reference;
boolean isALT;
public MyKeyListener(Key reference)
{
this.reference = reference;
this.isALT = false;
}
public void keyPressed(KeyEvent key)
{
if ( key.getKeyCode() == KeyEvent.VK_ALT )
{
this.isALT = true;
this.reference.setBackgrou
}
if ( this.isALT == true )
{
if ( key.getKeyCode() == KeyEvent.VK_1 )
{
this.reference.setBackgrou
this.isALT = !this.isALT;
}
}
}
}
class test
{
public static void main(String args[])
{
new Key();
}
}
Business Accounts
Answer for Membership
by: omry_yPosted on 2004-07-01 at 06:14:13ID: 11446291
1.4 is known to be buggy.
did you try with 1.4.2?