| | |
Urgent Help!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2007
Posts: 108
Reputation:
Solved Threads: 7
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
change
& try again
Error always is the source of information , carefully analyze it
try {
// Robot
r = new Robot();
r.delay(500);
r.keyPress(KeyEvent.VK_7);
} catch (AWTException e) {
e.printStackTrace();
}•
•
•
•
i tried robot class but it always give me an error
Last edited by quuba; Aug 20th, 2009 at 5:03 pm. Reason: added
•
•
Join Date: Jan 2008
Posts: 3,839
Reputation:
Solved Threads: 503
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:
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.
- Focus is on Java program. Key press event goes to Java program.
- Focus is on Notepad program. Key press event goes to Notepad program.
- Focus is on browser program. Key press event goes to browser program.
- 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.
Last edited by VernonDozier; Aug 21st, 2009 at 12:26 pm.
•
•
Join Date: Jan 2008
Posts: 3,839
Reputation:
Solved Threads: 503
•
•
•
•
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....
•
•
•
•
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.
•
•
Join Date: Aug 2008
Posts: 208
Reputation:
Solved Threads: 13
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.
![]() |
Similar Threads
- pls heeeeeeeeelp its urgent. (C)
- Urgent! (C++)
- URGENT: Need help on I/O code! (Java)
- Urgent help (Networking Hardware Configuration)
- Homepage keeps switching back to a porn site (Web Browsers)
- Importing SQL Script File - Urgent !! (Database Design)
Other Threads in the Java Forum
- Previous Thread: acc program help
- Next Thread: Can't install Java. Rawr.
Views: 480 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for Java
addball android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time tree trolltech utility windows






