Home arrow static arrow Java Programming [Archive] - Key event without focus
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Key event without focus
This topic has 5 replies on 1 page.

Posts:20
Registered: 7/15/04
Key event without focus  
Aug 2, 2004 9:36 AM



 
Hi,

Could you tell me how to get F1 key event which is
nothing to do with any control's focuses?

For example, suppose there's a button saying "F1 exit",
and the focus is on some other button.
In such case, if user pushes F1, program should exits.
That's what I want to do.

Thank you.

 

Posts:19,725
Registered: 9/26/01
Re: Key event without focus  
Aug 2, 2004 10:30 AM (reply 1 of 5)



 
Have you tried adding a keyListener to the dialog/window which is containing those other controls?
 

Posts:14,142
Registered: 99-04-02
Re: Key event without focus  
Aug 2, 2004 10:52 AM (reply 2 of 5)



 
com.sun.java.accessibility.util package?
http://java.sun.com/products/jfc/jaccess-1.3/doc/api/com/sun/java/accessibility/util/AWTEventMonitor.html
 

Posts:20
Registered: 7/15/04
Re: Key event without focus  
Aug 3, 2004 6:45 AM (reply 3 of 5)



 
Thank you all. I'll check those and try.
 

Posts:10,972
Registered: 10/23/03
Re: Key event without focus  
Aug 3, 2004 6:54 AM (reply 4 of 5)



 
http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html
 

Posts:10,972
Registered: 10/23/03
Re: Key event without focus  
Aug 3, 2004 7:12 AM (reply 5 of 5)



 
For example:
import java.awt.*;import java.awt.event.*;import javax.swing.*; public class Example {    public static void main(String[] args) {        JPanel p = new JPanel();        p.add(new JButton("sample button"));        Action action = new AbstractAction("homer") {            public void actionPerformed(ActionEvent evt) {                System.out.println("DOH!");            }        };        KeyStroke stroke = KeyStroke.getKeyStroke("F1");        Object key = action.getValue(Action.NAME); //homer        p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, key);        p.getActionMap().put(key, action);         JFrame f = new JFrame("Example");        f.setContentPane(p);        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        f.pack();        f.setLocationRelativeTo(null);        f.setVisible(true);    }}
 
This topic has 5 replies on 1 page.