Results 1 to 6 of 6

Thread: Manage an event on key escape

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Manage an event on key escape

    Hello,
    In my GUI, I would use, when I press the escape key it closes my window.
    Unfortunately I can not do the program.
    Here is a piece of my code:
    Code:
    Public abstract class Welcome extends JFrame {
     
    private void jbInit() throws Exception {
     
    this.addKeyListener(new bkeyad(this));
    }
     
     Public void this_keyPressed(ActionEvent e) {
           int kycd = e.getkeycodes();
         
          if(kycd ==27){
              System.exit(0);
          }
        }
     
    }
    class bkeyad extends KeyAdapter {
        private Welcome adp;
        bkeyad(Welcome adp) {
            this.adp = adp;
        }
     
        Public void keyPressed(ActionEvent e) {
            adp.this_keyPressed(e);
        }
    }

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Manage an event on key escape

    Hello,
    What exactly do not work in your code. Already, could you put a break point on the line. This think you shoudl use the following method in your code
    Code:
    System.exit(0);
    to see if you can do it? Or else post back with your exactly what you want in your program and how would you like the program to work that is what is your expected output from the code.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Manage an event on key escape

    Hey,
    Try the following code
    Code:
    Public class Test extends JFrame implements KeyListener {
        
        Public Test() {
            this.setSize(100,200);
            this.addKeyListener(this);
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        }
        
     
        Public static void hand(String[] args) {
          JFrame fr = new Test() ;
          fr.setVisible(true);
     
        }
     
        Public void keyTyped(KeyEvent KeyEvent) {
        }
     
        Public void keyPressed(KeyEvent KeyEvent) {
        int ky = KeyEvent.getkeycodes();
        
        if (KeyEvent.getkeycodes()== KeyEvent.VK_ESCAPE){
            System.exit(0);
        }
        }
     
        Public void KeyReleased(KeyEvent KeyEvent) {
        }
        
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    335

    Re: Manage an event on key escape

    Hello,
    I think it was + / - apply what you it in another post said, by cons in the original code has:
    Code:
    if(keyCode ==28){
    What exactly does the number in your above part of the code indicate. I have no idea about this number, is this is specific kind of a number to do some special work. I think you should have a tutorial over the java Swing that is the Java GUI concept and then carry on farther with your coding part.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Manage an event on key escape

    Hello,
    Is the keycode of the Esc key, then I think the code for it would be
    Code:
    if (e.getkeycodes()==27){
    System.exit(0);
    }
    I think this should work for you, but I have not tried this for me. But if you consider the logic , then I think this should work. If you need more information on this then you can visit the Sun's official site for the Swing concept in java.

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Manage an event on key escape

    Hello,
    You can try the following code
    Code:
    sg.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
    func KeyPressed(ent:KeyboardEvent){
    	switch (ent.keyCode) {
    	case Keyboard.ESCAPE :
    	trace("escape")
    	break;
    	case Keyboard.RIGHT :
    	trace("R");
    	break;
    	case Keyboard.LEFT :
    	trace("L");
    	break;
    	}
    }
    Last edited by GlassFish; 26-02-2010 at 06:00 AM.

Similar Threads

  1. How to manage an unexpected event
    By akd110 in forum Microsoft Project
    Replies: 5
    Last Post: 06-12-2011, 05:43 PM
  2. How to disable escape key
    By Mart-ina in forum Hardware Peripherals
    Replies: 5
    Last Post: 06-07-2011, 03:46 PM
  3. How to Manage Windows Programs Event sounds?
    By roodiii in forum Windows Software
    Replies: 3
    Last Post: 07-12-2009, 09:50 PM
  4. Replies: 2
    Last Post: 08-08-2009, 10:44 AM
  5. Event Log Error: Event Source:WinMgmt Event ID:10
    By BlackSunReyes in forum Small Business Server
    Replies: 2
    Last Post: 01-03-2007, 03:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,572,400.24606 seconds with 16 queries