简体   繁体   中英

Adding TAB key java swing keybindings — What is the correct name?

Action tab = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {    
            System.err.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        }
    };

Game.panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("Tab"), "tab");
Game.panel.getActionMap().put("tab", tab);

However its not printing. This is the same set up that I use for all my other keys, but I can't get the tab key to work. I've tried "TAB" , "Tab" , and "tab" .

What's the correct name for it? Or is there something special?

You probably want:

KeyStroke.getKeyStroke('\t');

\\t is the escape sequence for the TAB character (0x09) , which is output by pressing tab on your keyboard .

There is also KeyEvent.VK_TAB which might be preferable depending on your use case. See the documentation in KeyEvent for more on why you should prefer one or the other.

I'm surprised that KeyStroke works for you since the Tab character is generally handled by the focus subsystem.

It doesn't work for me. I'm using JDK8 on Windows 7:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TabSSCCE extends JPanel
{
    public TabSSCCE()
    {
        Action tab = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("key typed");
            }
        };

        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('1'), "typed");
        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('a'), "typed");
        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('\t'), "typed");
        getActionMap().put("typed", tab);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("TabSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TabSSCCE());
        frame.setLocationByPlatform( true );
        frame.setSize(100, 100);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

Does the above code work for you?

Only the "1" and "a" work for me. The Tab key does not work for me.

If not, then can you post a simple SSCCE showing how you are using this?

Edit:

The above code will work with either of the following:

frame.setVisible( true );
frame.setFocusTraversalKeysEnabled(false);

or you can disable the focus traversal keys for the panel:

public TabSSCCE()
{
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

    ...
}

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