Link to home
Start Free TrialLog in
Avatar of amakalski
amakalski

asked on

how to handle CTRL+C

in my java program I want to handle CTRL+C gracefully (close sockets at least). Anybody knows how and where I can catch CTRL+C ?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can try it like :

YourComponent.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
     
        if(key == KeyEvent.VK_C){
                  if(e.getModifiers() == KeyEvent.CTRL_MASK){
                         ShutDown file = new ShutDown();
                         Thread printThread = new Thread(file);
                         printThread.start();        
                    }
         }
      }
});

class ShutDown extends Thread {
        public void run() {
        Runtime.getRuntime().addShutdownHook(new MyShutdown());
    }
}

Hope it helps . . .
Javatm


Oh sorry I'm sick it should be like :

 YourComponent.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
     
        if(key == KeyEvent.VK_C){
             if(e.getModifiers() == KeyEvent.CTRL_MASK){
                   MyShutdown1  file = new MyShutdown1();
                   Thread printThread = new Thread(file);
                   printThread.start();        
            }
         }
      }
 });

  class MyShutdown1 {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new MyShutdown2());
    }
}

 class MyShutdown2 {
    public void run() {
        // clean up here
    }
}
Sorry Again try this :

YourComponent.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
     
        if(key == KeyEvent.VK_C){
             if(e.getModifiers() == KeyEvent.CTRL_MASK){
                   MyShutdown1  file = new MyShutdown1();
                   Thread printThread = new Thread(file);
                   printThread.start();        
            }
         }
      }
 });

  class MyShutdown1 {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new MyShutdown2());
    }
}

 class MyShutdown extends Thread {
    public void run() {
        // clean up here
    }
}
Javatm,

I don't think that'll actually work as I don't think the component will recieve the KeyEvent for the Ctrl-C. The shutdown hook just needs to be added once when the application starts up as in the code i posted above.
amakalski only talked about closing sockets. What if its not an AWT application at all, but what if its just a simple server application that runs as a console application and somebody hits Ctrl-C at that end? I guess shutdown hook should work for that.
Yeh its correct, I'm visualizing it as advance.
thats why there are 2 options :)

Friend : Javatm
>> I don't think that'll actually work as I don't think the component will recieve the KeyEvent for the Ctrl-C.
     Sorry if I tried adding some answers, I jus feel very sick today and I'm not in my real self, sorry.