如何找到按下键的键代码

问题描述 投票:0回答:1

我想有2个按钮:

1] BindBTN-单击时,keyListener /动作将监听按键,并找到该按键的KeyCode。

2)RunBTN-单击时,动作将等到用户按下相同的键,然后执行runProgram()

        RunBTN.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                @SuppressWarnings("serial")
                AbstractAction run = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        runProgram();
                    }
                };

                RunBTN.getInputMap().put(KeyStroke.getKeyStroke("**bound key**"),
                        "run");
                RunBTN.getActionMap().put("run",
                        run);
            }
        });

我要对BindBTN做什么?

java keylistener keystroke abstract-action
1个回答
0
投票

Okey,所以我知道了:

对于BindBTN:

        BindingBTN.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyPressed(KeyEvent evtBind) 
            {
                BindCmd = evtBind.getKeyCode();
                BindCmdString = KeyEvent.getKeyText(BindCmd);
            }
        });

对于RunBTN:

        RunBTN.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                @SuppressWarnings("serial")
                AbstractAction run = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        runProgram();
                    }
                };

                RunBTN.getInputMap().put(KeyStroke.getKeyStroke(BindCmdString),
                        "run");
                RunBTN.getActionMap().put("run",
                        run);
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.