Results 1 to 8 of 8
  1. #1
    tom_k is offline Member
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    3
    Rep Power
    0

    Default Need help with on-screen keyboard (first post)

    hello, this is my first post. i just finished a java course in university and i've found that i can probably use java for making small apps i need.

    the keyboard is made of JButton components inside a JPanel inserted to a JFrame. I've set up ButtonListener for each button and used the Robot class to fire key events according the source of the event like so:

    Robot robot = new Robot();
    robot.delay(5000);
    robot.keyPress(KeyEvent.VK_0);

    you might notice the .delay(5000); and here is my problem, since the JFrame is .setAlwaysOnTop(true); clicking a button will make the window with the carret inactive so a 5sec delay is enough to make it active again so i can see the output is working. does anyone know how i can make this process automatic?

    if this is too complicated maybe i can create a JTextArea component with the correct character and insert that somehow?

    i'll really appreciate any help with this!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Not sure how your program is supposed to work.
    Is it like this: Your program displays a number of buttons that are to simulate a keyboard. When the mouse clicks on one of those buttons, then you want Robot to press the key for the hardware keyboard attached to the PC. That key press then will be sent to an input area in your program (JTextArea).
    Your problem is keeping the focus somewhere.
    carret inactive
    is this related to focus?
    You want to serialize/synch the passing of focus from the button to the JTextArea and back to the buttons.

  3. #3
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    Could explain a little more?

    What is the "window with the carret"?
    Is the keyboard in a different window from the main app?
    What exactly is the delay doing? How is the delay making things active?
    Last edited by Jeremy; 07-19-2008 at 06:37 PM.

  4. #4
    tom_k is offline Member
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    3
    Rep Power
    0

    Default

    i'll try to explain a little better...

    the app is supposed to be a virtual-keyboard, just like the one that comes with windows (but will contain numeric buttons). i want it to be set always on top and keep in the corner while i use cad software and when i need to enter numbers i'll use the virtual keyboard instead of having the real keyboard in reach.

    i used the Robot class to fire key events, since it will output the same way using a keyboard would do. let's say i have notepad open, so when i click a button on my virtual keyboard (like "0") it will run a method with robot.keyPress(KeyEvent.VK_0); that will output 0 into the document, like i would hit the 0 key on the keyboard. the problem with the program is that when i click the button the notepad window becomes inactive and the carret disappears (i hope that's the correct name for the flashing line marking where the next charecter will be inserted). when i use the delay i have enough time to select the notepad window and the see the carret flashing and the output will be entered to the document correctly.

    i'm not sure that this way is the best way to do what i want to do, this is why i asked about JTextArea, maybe i can create a component with the text i want to output and sent it to the notepad window somehow? it's just an idea about a different solution without firing key events.

  5. #5
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    Cursor would be the correct term ;)

    The best way to fix your problem is to look into how you can make your program (i.e. Notepad, CAD) stay focused when you click on your virtual keyboard. Maybe there's a way for your keyboard program to know what program previously had focus and then regain its focus?

    Try this: java.net Forums : Application window focus ...

    I typed "java swing window focus" into Google to find it.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Here's a simple program that does some of it. Open notepad it top right of screen and execute this program. Put the caret into notepad before starting this program.
    The program will enter a character into notepad when a button is pressed and then return focus to the button.
    Java Code:
    // RobotTests.java
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Robot;
    
    public class RobotTests extends Frame {
    	Robot r; // = new Robot();
    
       // Define locations of window's title bar
       Point OtherWindow = new Point(600, 20);
       Point OurWindow = new Point(200, 20);
    
       Point lastMouseLoc = new Point(0,0); // set this on mouse press
    
    
    	public RobotTests() {
            try {
            	  r = new Robot();
            }catch(Exception ex) {
                System.err.println("ex " + ex);
                return;                     // EXIT??
            }
            setTitle("Robot tests");
            setLayout(new BorderLayout());
    
            TextField t1 = new  TextField(10);
            t1.setBackground(Color.red);
            add(t1, BorderLayout.NORTH);
    
            // Put some buttons in center to send chars to another app
            Panel p = new Panel();
            Button b1 = new Button("A");
            b1.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                   setMouseLoc(ae);
                   writeChar(KeyEvent.VK_A);
               }
            });
            p.add(b1);
            Button b2 = new Button("B");
            b2.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                   setMouseLoc(ae);
                   writeChar(KeyEvent.VK_B);
               }
            });
            p.add(b2);
            Button b3 = new Button("C");
            b3.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                   setMouseLoc(ae);
                   writeChar(KeyEvent.VK_C);
               }
            });
            p.add(b3);
            Button b4 = new Button("D");
            b4.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                   setMouseLoc(ae);
                   writeChar(KeyEvent.VK_D);
               }
            });
            p.add(b4);
            add(p, BorderLayout.CENTER);
    
            TextField t2 = new TextField(10);
            add(t2, BorderLayout.SOUTH);
            t2.requestFocus();
    
            addFocusListener(new FocusListener() {
                public void 	focusGained(FocusEvent e) {
                   System.out.println("focusGained " + e);
                }
                public void 	focusLost(FocusEvent e) {
                   System.out.println("focusLost " + e);
                }
             });
    
    		  setSize(300, 200);
    //        setLocation(200, 100);
    //        pack();
    		  setVisible(true);
    
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });
    	} // end constructor
    
       //---------------------------------------------------
       // Get location of component and save as mouse loc
       void setMouseLoc(ActionEvent ae) {
          Component comp = (Component)ae.getSource();
          lastMouseLoc = comp.getLocationOnScreen();   // top left of component
          // Need mouse listener to get true loc, we'll fudge it to the middle of component
          Dimension dim = comp.getSize();
          lastMouseLoc.x += dim.width/2;
          lastMouseLoc.y += dim.height/2;
    //      System.out.println("lastMouseLoc=" + lastMouseLoc);
       }
    
       //-----------------------------------------------
       // Send char to another window
       // Set focus, type letter and then come back
       void writeChar(int let) {
          r.mouseMove(OtherWindow.x, OtherWindow.y); // another window's title bar
          r.mousePress(InputEvent.BUTTON1_MASK);     // press to get focus
          r.mouseRelease(InputEvent.BUTTON1_MASK);
          // Problem here if called second time too soon -> sends double click
          r.keyPress(let);                 // type the letter
          r.keyRelease(let); 
          r.mouseMove(OurWindow.x, OurWindow.y);  // back to our window's title bar
          r.mousePress(InputEvent.BUTTON1_MASK);  // press to get focus
          r.mouseRelease(InputEvent.BUTTON1_MASK);
          r.mouseMove(lastMouseLoc.x, lastMouseLoc.y);  // put mouse back on button
       }
    
    
        //------------------------------------------------
        // Other tests
        int[] chars =  {KeyEvent.VK_N, 
                        KeyEvent.VK_O,
                        KeyEvent.VK_R,
                        KeyEvent.VK_M,
                        KeyEvent.VK_SPACE
                        };
    
        //---------------------------------------------------------------------
        // Move cursor around and enter text
        void start() {
            for(int i=0; i < chars.length; i++) {
    //            r.mouseMove(10, 40);  // Our window
    //            r.delay(100);
    //            r.mouseMove(400, 300); // another window
    //            r.delay(100);
                r.mouseMove(200, 20);  // back to our window's title bar
    //            r.mouseMove(500, 100);      // This will write into this program!!!
                r.delay(20);
                r.mousePress(InputEvent.BUTTON1_MASK);
                r.mouseRelease(InputEvent.BUTTON1_MASK);
                r.mousePress(InputEvent.BUTTON1_MASK);     // double click - resizes window
                r.mouseRelease(InputEvent.BUTTON1_MASK);
                r.delay(50);
    
                // Type in some stuff into top text field
                r.mouseMove(15 + (i * 20), 40);
                r.delay(50);
                r.keyPress(chars[i]);       // Input seems to go to first field???
                r.keyRelease(chars[i]);     // vs the one that requestFocus
                r.delay(50);
            }
        }
    
        //--------------------------------------------------------------------------
     	public static void main(String[] args) {
    		RobotTests rt = new RobotTests();
    //        rt.start();
    	} // end main()
    } // end class
    Last edited by Norm; 07-19-2008 at 10:57 PM.

  7. #7
    tom_k is offline Member
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    3
    Rep Power
    0

    Default

    i'm not sure why but this app didn't work... it ran but i couldn't make it output anything to notepad or any other program

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Did the cursor jump around? Where was the Notepad window on the screen. Look at the values for OurWindow and OtherWindow to see if the two apps windows were correctly positioned.

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 01:08 PM
  2. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 07:12 PM
  3. post info to a webpage
    By kmp in forum Networking
    Replies: 1
    Last Post: 03-22-2008, 02:20 AM
  4. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 10:42 PM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •