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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh sorry I'm sick it should be like :
YourComponent.addKeyListen er(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().addSh utdownHook (new MyShutdown2());
}
}
class MyShutdown2 {
public void run() {
// clean up here
}
}
YourComponent.addKeyListen
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().addSh
}
}
class MyShutdown2 {
public void run() {
// clean up here
}
}
Sorry Again try this :
YourComponent.addKeyListen er(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().addSh utdownHook (new MyShutdown2());
}
}
class MyShutdown extends Thread {
public void run() {
// clean up here
}
}
YourComponent.addKeyListen
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().addSh
}
}
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.
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
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.
Sorry if I tried adding some answers, I jus feel very sick today and I'm not in my real self, sorry.
YourComponent.addKeyListen
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().addSh
}
}
Hope it helps . . .
Javatm