import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.Robot;
import java.awt.Event;
import java.awt.AWTException;




public   class keyboard implements ActionListener
 {
	
	int i;
    String s[] = {"esc","F1","F2","F3","F4","F5",
   		        "F6","F7","F8","F9","F10","F11","F12",
   		         "psc","slk","pau","A","B","C","D","E",
   		         "F"};           
    String g = "";
    Object b;
    
    JMenuBar m = new JMenuBar();   
    
    JMenu m1 = new JMenu("File");
    
    JMenu m2 = new JMenu("Keyboard");
    JMenu m3 = new JMenu("Setting");
    JMenu m4 = new JMenu("Help");
    
    JMenuItem m11 = new JMenuItem("Exit");
    JMenuItem m21 = new JMenuItem("StandardKeyboard");
    JMenuItem m22 = new JMenuItem("ExtendedKeyboard");
    JMenuItem m26 = new JMenuItem("RegularLayout");
    JMenuItem m23 = new JMenuItem("101");
    JMenuItem m24 = new JMenuItem("102");
    JMenuItem m25 = new JMenuItem("106");
    JMenuItem m31 = new JMenuItem("Always On Top");
    JMenuItem m41 = new JMenuItem("About On-Screen Keyboard");
    JFrame f = new JFrame();
    JButton j[] =  new JButton[(s.length)]; 
    Robot r;
   
	
    
	keyboard()
	{
	    m1.add(m11);
	     m2.add(m21);
	     m2.add(m22);
	     m2.addSeparator();
	     m2.add(m26);
	     m2.addSeparator();
	     m2.add(m23);
	     m2.add(m24);
	     m2.add(m25);
	     m3.add(m31);
	     m4.add(m41);
	     
	     m.add(m1);
	     m.add(m2);
	     m.add(m3);
	     m.add(m4);
	     
	     m41.addActionListener(this);
	     
	 
	       
	     
	     for(i=0;i<s.length;i++)
	     {  
	    	 j[i] = new JButton(s[i]);
	         j[i].setBackground(new Color(255,255,255));
	         j[i].addActionListener(this);
	         
	     }
	      
	     
	     f.setLayout(new FlowLayout(FlowLayout.LEFT));
	     f.setSize(630,200);
	     Container c =  f.getContentPane();
	     c.setBackground(new Color(33,66,119));
	     
	     f.setTitle("On-Screen Keyboard");
	     f.setJMenuBar(m);
	     
	     for(i=0;i<s.length;i++)
	    	 c.add(j[i],JFrame.LEFT_ALIGNMENT);
	     
	         
	     f.isMaximumSizeSet();
	       
	       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       f.setVisible(true); 
	       f.setFocusableWindowState(false);
	       
	       try{
		       Robot r = new Robot();
		       r.delay(500);
		       r.keyPress(KeyEvent.VK_7);
		       
		       }
		 catch (AWTException e) {
	  e.printStackTrace();
	}
	     
		}
	     
	  
	
	public static void main(String args[])
	{
	keyboard k = new keyboard();
	
   
	}      
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == m41)
		JOptionPane.showMessageDialog(null
				                      ,"Project By\nPirZada(20)\n"+
				                      	        "Salman Sayar(22)\n "+
				                      	        "Wajahat Kareem(16)","About On-Screen Keyboard",
				                      	        JOptionPane.INFORMATION_MESSAGE);
			
		if(e.getSource() == j[16])
		{
             
					
			
					
		}
				
		}

	
  
  
}

Dear all user's please help , i am making on screen keyboard like XP one but i am confused , i want it to behave it like keyboard when i press button 'A' then it write that on the notepad or else , i tried robot class but it always give me an error so please kindly see my this project and help me what can i do to solve this problem

change

try {
            // Robot
            r = new Robot();
            r.delay(500);
            r.keyPress(KeyEvent.VK_7);

        } catch (AWTException e) {
            e.printStackTrace();
        }

& try again

i tried robot class but it always give me an error

Error always is the source of information , carefully analyze it

well sorry i forgot to take that code out of these , i just when i press any button it do in my keyboard through mouse then it work and that's what all i concerned.

Implement reciver

...then it write that on the notepad or else..

for example JTextField

JTextField only work inside java i want to send the events to outside environment.

Robot will do fine for this problem, at least for part of this problem. Your main problem is this, I would imagine. Say you have three programs open: this Java program, Notepad, and a browser. At most, one of them will have the active focus. Or none of them could. So you have four possibilities for the key press:

  1. Focus is on Java program. Key press event goes to Java program.
  2. Focus is on Notepad program. Key press event goes to Notepad program.
  3. Focus is on browser program. Key press event goes to browser program.
  4. Focus is on none of the programs. Key press event goes to none of them.

You have a Java GUI with buttons, so in order for your actionPerformed to get called, the focus has to be on the Java program. You can easily use Robot to send a key stroke, but it'll go to the Java program, since that's what the focus is. Which you don't want. You want the key stroke to be sent to Notepad. So you need to somehow change that focus to Notepad. You can do that manually, by clicking Notepad after clicking the button on the Java program. So you could click the button on the Java program, then have a pause of three seconds or whatever, enough time to let you manually click Notepad active. Then the pause is over, so have Robot send that key press, which will end up on Notepad.

That's one way. or, presuming that Java knows where Notepad is on the screen, you can have Robot do the mouse press onto Notepad for you, then send the key press.

There's probably another way to set the focus that doesn't involve mouse clicking, or where you can actually specify the Notepad process number or something, but I don't know what it is. But changing focus from Java to Notepad and back is your main issue with this program, I would imagine.

All what you said is write but i want to do it automatically not manually clicking and there can any other active programe like microsoft word then how my programe send's the key .... I always post my problems here becz of great logical programer's and now no body can answer my this question....

All what you said is write but i want to do it automatically not manually clicking and there can any other active programe like microsoft word then how my programe send's the key .... I always post my problems here becz of great logical programer's and now no body can answer my this question....

As I mentioned in my last post:

That's one way. or, presuming that Java knows where Notepad is on the screen, you can have Robot do the mouse press onto Notepad for you, then send the key press.

If you know the coordinate, you can have Robot click back and forth for you. That's a poor man's focus changer where it clicks, but you don't have to do the clicking yourself. But you'd have to know the location to click and tell the Java program. You might have to do a little bit of clicking in the beginning to test/find the window(s), but after you do, no more clicking.

As also mentioned in my last post too, there may well be a way to send it directly to Notepad if you know the process number or whatever, but I've never done it.

If you're doing an on-screen keyboard, you will most likely want the keyboard to always be on top... im using it now, and it's state is 'inactive', yet it is still posting to the screen. This might be due to the fact that it can interact well with the Windows environment and has access to things Java does not. But I believe it is possible with java, but you might have to look into the JDesktop Project.

Well is there any way that java can get the handle of event which is focused?

I have source code of onscreen keyboard in MFC , is there any tool that help me to convert it in java..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.