how to let Excel or notepad input numbers from floating number keypad in java

59 Views Asked by At

I made a floating number keypad in java, it can key in figures in java application, but i doesn't work for other windows applications, such as Excel,notepad etc. So Anyone can help tell me how to let my number keypad use in other windows applications also.

 my floating number keypad img:

1

There are 1 best solutions below

0
Chauncey Lee On

I solved the problem by using "Robot" to simulate keyboard press, I post an example below is number button 1's acitonPerformed in my floating number keypad

 private void btn01ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    Boolean f = false;
    // for input numbers into jtextfield in  java applications
    Object source = evt.getSource();
    if (source instanceof JButton) {
        JButton btn = (JButton) source;
        try {


           
            String val = String.valueOf(btn.getText());


            Component comp = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
            if (comp instanceof JTextComponent) {
                JTextComponent tc = (JTextComponent) comp;
                tc.setText(tc.getText() + val);
                f = true;
            }
        } catch (NumberFormatException exp) {
        }
    }
    
    // below for input number 1 into other programs
    if (!f) {
        Robot r;
        try {
            r = new Robot();
            r.keyPress(KeyEvent.VK_1);
            r.keyRelease(KeyEvent.VK_1);
        } catch (AWTException ex) {
            Logger.getLogger(calculator.class.getName()).log(Level.SEVERE, null, ex);
        }


    }




} 

key in number from floating key pad to Excel