简体   繁体   中英

Passing KeyEvent to a different component

I am trying to allow for a JTextArea to have focus but if the user enters keyboard input and control is not pressed then to forward that key event on to the command JTextField.

I tried to simply append the key char to the text in the JTextField but that results in unknown characters when pressing anything except letters, symbols, or numbers.

newField.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                if(!e.isControlDown()) {
                    Console.consoleWindow.appendTextToCMD(e.getKeyChar());
                }
            }
        });

You need to use processEvent on your other JComponent. Also, you need to create a new AWTEvent - copy all fields but with the source being the new JComponent

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