I want to make something like a virtual keyboard for touchscreen ultrabooks in Java. For example, I want to type in Microsoft Word with my virtual keyboard.
What I've done so far is use JButton
to represent keys, and use the Robot
class to simulate computer input.
The problem I have is when I press the JButton
, it focuses the JButton
, and therefore the Robot
class directs the input to the JButton
instead of the program I want to input to, such as Microsoft Word.
My code looks like this for the button A
int KeyValue = KeyEvent.VK_A;
Robot robot ...
JButtton.addMouseListener(new MouseListener(){
...
public void mousePressed(MouseEvent e){
robot.keyPress(KeyValue);
}
public void mouseReleased(MouseEvent e){
robot.keyRelease(KeyValue);
}
...
}
...
I have confirmed that the button is being pressed. I added a KeyListener
to the JButton
, and an "A" is being pressed whenever I click the button, but it's directed at the JButton
instead of the other program.
I have tried using JButton.setFocusable(false)
, but it didn't work...
I have searched other questions similar to mine, but they are all typing natively into another Java Window or Textbox, and not another program.
So my question is - How can I make a virtual keyboard that types into other programs? Preferably using JButtons
but I'm open to any suggestions?
Set the JFrame
property of setFocusableWindowState(false);
.