简体   繁体   中英

Double Tap Key Bindings Java

public void fullscreenKey(final JFrame frame)
{
    k = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
    getInputMap(WHEN_IN_FOCUSED_WINDOW).put(k, k.toString());
    getActionMap().put(k.toString(), new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            fullscreen(!f1, frame);
            f1 = !f1;
        }
    });  
}

Whenever I call this method into other classes, nothing happens. I can't make this method static because of getInputMap and getActionMap. I call with Main main = new Main(); main.fullscreenKey(frame); This method is called in the Main constructor. How can I call this method in other classes and have it work?

Not sure if this is what you're after or not

public void fullscreenKey(JComponent comp, final JFrame frame)
{
    k = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
    comp.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(k, k.toString());
    comp.getActionMap().put(k.toString(), new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            fullscreen(!f1, frame);
            f1 = !f1;
        }
    });  
}

If you're looking for a "global" key handler instead (ie you want to handle the key stroke at anytime in your application), you could look at Toolkit.addAWTEventListener , this is very low level so it might take a little effort to it to work properly.

You could also look at the KeyboardFocusManager ( Java Global JFrame KeyListener )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM