简体   繁体   中英

How to send an KeyEvent to JFrame Component?

So im trying to send an KeyEvent to the JFrame component. Like when u press a key while the window is focused normally it sends an KeyEvent to the JFrame and triggers something. But how can i simulate this? Without the Robot class, like directly inputting a KeyEvent into a JFrame component?

I found out how to send MouseEvents to it which you can do by creating an MouseEvent object and calling component.dispatchEvent(MouseEvent). Which then sends the mouse event to the JFrame and it works perfectly. But doing the same thing with KeyEvents doesn't seem to work. I have tried pretty much everything like sending an FocusEvent before the KeyEvent ect. Nothing just seems to work.

This is my KeyEvent objects that im trying to send to it. It uses the same target and stuff as the MouseEvent which works. So this shouldn't be the problem, the problem is that sending it to the component doesn't work. The "key" argument is the key id like KeyEvent.VK_2

KeyEvent ke = new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char)key);
KeyEvent ke = new KeyEvent(target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char)key);

Have you tried using the Robot class as per the docs:

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed

An example is:

someComponent.requestFocusInWindow(); // need to focus the component we want the key events to be sent too

try { 
    Robot robot = new Robot(); 
    robot.keyPress(KeyEvent.VK_ENTER); 
} catch (AWTException e) { 
    e.printStackTrace(); 
} 

You can add a KeyListener, assuming that's what you want. Every Character has a char code, which means KeyEvent.VK_2 can be simplified to the code equivalent of that. Just to make it simpler.

public class Foo {
    public static void main(String[] args) {
       JFrame frame = new JFrame();
       frame.setSize(300,300);
       frame.setVisible(true);
       frame.addKeyListener(new KeyListener() {

           @Override
           public void keyPressed(KeyEvent e) {
                if(e.getKeyChar() == 'a'){
                    doSomething();
                }
           }
           @Override
           public void keyTyped(KeyEvent e) {
           }

           @Override
           public void keyReleased(KeyEvent e) {

           }
       });
    }
}

I replaced the doSomething(); with console output, so whenever I'm on the frame, it listens to my key events, and when I press a , it executes some code for me.

The KeyEvents were actually being sent correctly. But nothing was happening because i was only sending the press and release events and i found out that after sending the press event you need to send a typed event and after that the release event. After that the KeyEvents actually work.

Heres an example:

    int key = KeyEvent.VK_2;
    Component target = null;
    
    KeyEvent pressed = new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char)key, 1);
    KeyEvent typed = new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, 0, (char)key);
    KeyEvent released = new KeyEvent(target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char)key, 1);
    
    EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
    queue.postEvent(pressed);
    queue.postEvent(typed);
    queue.postEvent(released);

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