how to handle specific keystrokes

U

user

Hi,

I want to change the output to a JTtextbox based on user input.
For example:
user types in Ctrl + Alt + Shoft + B, i want to detect this and send to
the textbox a N or user types in Ctrl + Alt + D, i want to detect this
and send to the textbox a A for example.

Pleas who can help me.

Thanks,

Anand
 
N

Niels Dybdahl

I want to change the output to a JTtextbox based on user input.
For example:
user types in Ctrl + Alt + Shoft + B, i want to detect this and send to
the textbox a N or user types in Ctrl + Alt + D, i want to detect this
and send to the textbox a A for example.

If you only want the functionality when the textbox has focus, then use
Component.addKeyListener
If you want it to work no matter where the focus is, then use
KeyboardFocusManager.addKeyEventDispatcher (from Java 1.4)

Niels Dybdahl
 
U

user

HI Niels,

I use addKeyListener.
I can intercept the keyPressed event but i dont't succeed in changing
the character send to the JTextField.
This is my coding i tried:

public void processKeyEvent (KeyEvent e)
{
if ( (e.getID () == KeyEvent.KEY_TYPED) &&
Character.isLowerCase (e.getKeyChar ()))
{
System.out.println ("keycode=" + e.getKeyCode() );

// test purpose: change lowercase to uppercase. THIS WORKS!
e.setKeyChar (Character.toUpperCase (e.getKeyChar ()));
}
else
{
if ( (e.getID () == KeyEvent.KEY_PRESSED))
{
if (e.isAltDown() && e.isControlDown() && e.isShiftDown())
{
char kar=65;
e.setKeyChar (kar); //does not work
}

if (e.isShiftDown())
// create new KeyEvent, consume old one and proces new one.
// THIS IS NOT SUCCESFULL. WHAT IS WRONG IN MY CODING?
{
char kar=67; // does not work either

KeyEvent nke = new KeyEvent(
e.getComponent() ,
e.getID(),
e.getWhen(),
e.getModifiers()
,67
,kar
,e.getKeyLocation());

// consume old event
e.consume ();
super.processKeyEvent(nke);
}
}
else
{
super.processKeyEvent (e);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top