|
-
Jun 26th, 2004, 06:13 PM
#1
Thread Starter
New Member
how to change keycode of KeyPressed Event and show result in JtextField
Hi,
In VB i know how to change the value of KeyAscii in KeyPress method of a textbox.
How can i do that in Java for a JTextField. I'm a newbie in Java
What i want is this:
User press Ctrl + Alt + Shift + B. After pressing this combination i want that for example an A is send to the JTextField.
or
User press Ctrl + Shift + D. After pressing this combination i want that for example an Z is send to the JTextField.
I tried to generate a New KeyEvent, but it doesn't do anything.
Here is a piece of code i tried:
Who can help me?
== class UpperCaseTextArea ==
import java.awt.AWTEvent;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
public class UpperCaseTextArea
extends TextArea
{
public UpperCaseTextArea ()
{
enableEvents (AWTEvent.KEY_EVENT_MASK);
}
public void processKeyEvent (KeyEvent e)
{
if ( (e.getID () == KeyEvent.KEY_PRESSED))
{
if (e.isAltDown() && e.isControlDown() && e.isShiftDown())
{
KeyEvent nke = new KeyEvent(
e.getComponent() ,
e.getID(),
e.getWhen(),
e.getModifiers()
,66 //show a B
,kar
,e.getKeyLocation());
// consume old event
e.consume ();
super.processKeyEvent(nke);
}
else
{
super.processKeyEvent (e);
}
}
}
}
== class UCTextAreaTest ==
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class UCTextAreaTest {
public static void main(String[] args) {
Frame f = new Frame();
f.add(new UpperCaseTextArea(), BorderLayout.CENTER);
f.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
f.setSize(200, 150);
f.setVisible(true);
}
}
Thanks,
Anand
-
Jun 26th, 2004, 09:44 PM
#2
Dazed Member
I gave this a shot but with not much luck. ActionEvent defines a bunch of constants SHIFT_MASK, CTRL_MASK ect.. but an action event is only fired off if the enter key is pressed, so say if the alt key is press the event is never fired off. The KeyEvent class defines the same kind of constants.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Example{
public static void main(String[] args){
new Example("Example");
}
private JFrame jf;
private JTextField jtf1;
private Container c;
public Example(String title){
jf = new JFrame(title);
c = jf.getContentPane();
jtf1 = new JTextField(10);
c.add(jtf1, BorderLayout.NORTH);
jf.addWindowListener(new WindowMonitor());
jtf1.addKeyListener(new KeyMonitor(jtf1));
jtf1.addActionListener(new KeyMonitor(jtf1));
jf.setSize(300,200);
jf.setVisible(true);
}
}
class WindowMonitor extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
class KeyMonitor extends KeyAdapter implements ActionListener{
JTextField jtf1;
public KeyMonitor(JTextField jtf1){
this.jtf1 = jtf1;
}
/*
public void keyTyped(KeyEvent ke){
if((ke.getID() == KeyEvent.KEY_TYPED) && (ke.getKeyCode() == KeyEvent.VK_SHIFT)){
// not working
}
}
*/
public void actionPerformed(ActionEvent ae){
if(ae.getID() == ActionEvent.SHIFT_MASK){
// not working
}
}
}
Last edited by Dilenger4; Jun 26th, 2004 at 09:50 PM.
-
Jul 15th, 2004, 02:56 AM
#3
Fanatic Member
perhaps
PHP Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Hehe extends JFrame{
public Hehe(){
super("Hehe");
InitializeComponent();
}
JTextField t=new JTextField();
void InitializeComponent(){
t.setBounds(10,10,100,20);
getContentPane().setLayout(null);
getContentPane().add(t);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
t.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int keyCode=e.getKeyCode();
if(e.isShiftDown()&&e.isAltDown()&&
e.isControlDown()&&keyCode==KeyEvent.VK_B)
t.setText("Hahaha");
}
});
setVisible(true);
}
public static void main(String[] args){
new Hehe();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|