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