Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simulating x press  RSS feed

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I click on the 'x' on an application, it shuts down as expected. Without having access to this code how can I simulate that procedure by pressing a key instead? I am able to access the frame object.
In other words, I want to press a key and make it act as though I clicked on the 'x' with my mouse.
Thansk for any help.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add a keyListener to your frame:
i have an added a bit of code down here but i didnt test it , so u may need to improve it:
addKeyListener(new KeyAdapter()
{
public void keyPressed(Keyevent e)
{
if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
System.exit(0);
}
}
note that u can change the VK_ESCAPE to any other key. check the API for all the fields, but basically its VK_ (the name of the key).
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that will only shut down my application. When I click on the 'x', it runs some other program that cleans up before shutting down. I need to be able to run this program so I want to make my key press act exactly as though I had clicked on the 'x'. I don't have access to this program and I have no idea where it's buried in the application.
Thanks for your response.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm. i undersatnd u now.
the way to do it is to simulate a close event in your keyListener.
like this:
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
WindowEvent x=new WindowEvent(YourFrame.this,WindowEvent.WINDOW_CLOSING);
YourFrame.this.dispatchEvent(x);
}
});

change the YourFrame that appears in the above code to your class name.
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works perfectly.
Much appreciated....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!