2

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);
2
  • 1
    Why are you trying to dispatch "2" to the frame. A KeyEvent is normally dispatched to a component that has focus. A frame will not have focus, a component on the frame will have focus. So I don't think your attempted solution will solve the problem. What problem are you trying to solve. Post your minimal reproducible example demonstrating the problem.
    – camickr
    Commented Jan 13, 2021 at 15:43
  • Yeah i meant a component in the JFrame sorry. So my question was that how do u send an KeyEvent to the component? The component.dispatchEvent() doesn't work for KeyEvents but works for MouseEvents atleast for me.
    – Coderman69
    Commented Jan 13, 2021 at 16:00

3 Answers 3

1

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(); 
} 
1
  • 1
    Yes ive tried to use the Robot. But the robot takes over your real keyboard and mouse which is not what i wanted. But i figured out how to send the KeyEvents now.
    – Coderman69
    Commented Jan 14, 2021 at 8:59
0

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.

2
  • Thanks but I'm looking for a way to send key events not listen to them.
    – Coderman69
    Commented Jan 13, 2021 at 16:59
  • Oh, as in doing something in the component that presses a button? Or something like a robot?
    – CreaM
    Commented Jan 13, 2021 at 17:32
-1

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);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.