java中的KeyEvent不适用于字母或数字,但适用于control、alt、向上和向下。但不适用于字母或数字

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

我浏览了互联网,但找不到解决方案 所以,我试图集成键盘快捷键,但后来意识到按键事件不起作用 正确地说,我找不到它对其他人有效的原因。但不适合我。

这是代码:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyHandler implements KeyListener {

    NotePad note;
    public KeyHandler(NotePad note){
        this.note=note;
    }
    @Override
    public void keyTyped(KeyEvent e) {
            System.out.println("some key is pressed");
            if (e.getKeyCode() == KeyEvent.VK_S) {
                System.out.println("s is down");
                note.file.save();
            }
            else if (e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_O)
                note.file.open();
            else if (e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_Z)
                note.functionEdit.undo();
            else if (e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_Y)
                note.functionEdit.redo();

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

在按键输入方法中,我首先在按下控制键的情况下使用它,它会发生,但不会与其他字母或数字键一起使用。

我试图实现键盘快捷键,基本上触发键盘输入,如 ctrl+s 等,并让它们执行某些功能。但它不起作用。

这是我的 keyListener 实现。

public void createTextArea(){
        textArea=new JTextArea();
        textArea.addKeyListener(keyHandler);
        textArea.getDocument().addUndoableEditListener(
                new UndoableEditListener() {
                    @Override
                    public void undoableEditHappened(UndoableEditEvent e) {
                        um.addEdit(e.getEdit());
                    }
                }
        );
        scrollPane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        frame.add(scrollPane);

//frame.add(textArea); }

java swing awt keylistener
1个回答
0
投票

一般来说,您最好使用键绑定 API。这可以帮助您解耦功能并控制触发事件的焦点级别。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.UndoManager;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.add(new NotePad());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class NotePad extends JPanel {

        private UndoManager undoManager;

        public NotePad() {
            undoManager = new UndoManager();
            JTextArea textArea = new JTextArea(20, 10);
            Document doc = textArea.getDocument();
            doc.addUndoableEditListener(new UndoableEditListener() {
                @Override
                public void undoableEditHappened(UndoableEditEvent e) {
                    undoManager.addEdit(e.getEdit());

                }
            });
            JScrollPane scrollPane = new JScrollPane(textArea);
            setLayout(new BorderLayout());
            add(scrollPane);

            InputMap inputMap = textArea.getInputMap(WHEN_FOCUSED);
            ActionMap actionMap = textArea.getActionMap();

            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK), "Save");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK), "Open");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK), "Undo");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK), "Redo");

            actionMap.put("Save", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Save");
                }
            });
            actionMap.put("Open", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Open");
                }
            });
            actionMap.put("Undo", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (undoManager.canUndo()) {
                        System.out.println("Undo");
                        undoManager.undo();
                    }
                }
            });
            actionMap.put("Redo", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (undoManager.canRedo()) {
                        System.out.println("Redo");
                        undoManager.redo();
                    }
                }
            });
        }

        protected void save() {
            System.out.println("Save");
        }

        protected void open() {
            System.out.println("Open");
        }

        protected void undo() {
            System.out.println("Undo");
        }

        protected void redo() {
            System.out.println("Redo");
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.