Results 1 to 3 of 3

Thread: how to change keycode of KeyPressed Event and show result in JtextField

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    8

    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

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
         } 
        }
       }

  3. #3
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    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
  •  



Click Here to Expand Forum to Full Width