943,648 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 706
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 20th, 2009
0

Urgent Help!

Expand Post »
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.Robot;
  8. import java.awt.Event;
  9. import java.awt.AWTException;
  10.  
  11.  
  12.  
  13.  
  14. public class keyboard implements ActionListener
  15. {
  16.  
  17. int i;
  18. String s[] = {"esc","F1","F2","F3","F4","F5",
  19. "F6","F7","F8","F9","F10","F11","F12",
  20. "psc","slk","pau","A","B","C","D","E",
  21. "F"};
  22. String g = "";
  23. Object b;
  24.  
  25. JMenuBar m = new JMenuBar();
  26.  
  27. JMenu m1 = new JMenu("File");
  28.  
  29. JMenu m2 = new JMenu("Keyboard");
  30. JMenu m3 = new JMenu("Setting");
  31. JMenu m4 = new JMenu("Help");
  32.  
  33. JMenuItem m11 = new JMenuItem("Exit");
  34. JMenuItem m21 = new JMenuItem("StandardKeyboard");
  35. JMenuItem m22 = new JMenuItem("ExtendedKeyboard");
  36. JMenuItem m26 = new JMenuItem("RegularLayout");
  37. JMenuItem m23 = new JMenuItem("101");
  38. JMenuItem m24 = new JMenuItem("102");
  39. JMenuItem m25 = new JMenuItem("106");
  40. JMenuItem m31 = new JMenuItem("Always On Top");
  41. JMenuItem m41 = new JMenuItem("About On-Screen Keyboard");
  42. JFrame f = new JFrame();
  43. JButton j[] = new JButton[(s.length)];
  44. Robot r;
  45.  
  46.  
  47.  
  48. keyboard()
  49. {
  50. m1.add(m11);
  51. m2.add(m21);
  52. m2.add(m22);
  53. m2.addSeparator();
  54. m2.add(m26);
  55. m2.addSeparator();
  56. m2.add(m23);
  57. m2.add(m24);
  58. m2.add(m25);
  59. m3.add(m31);
  60. m4.add(m41);
  61.  
  62. m.add(m1);
  63. m.add(m2);
  64. m.add(m3);
  65. m.add(m4);
  66.  
  67. m41.addActionListener(this);
  68.  
  69.  
  70.  
  71.  
  72. for(i=0;i<s.length;i++)
  73. {
  74. j[i] = new JButton(s[i]);
  75. j[i].setBackground(new Color(255,255,255));
  76. j[i].addActionListener(this);
  77.  
  78. }
  79.  
  80.  
  81. f.setLayout(new FlowLayout(FlowLayout.LEFT));
  82. f.setSize(630,200);
  83. Container c = f.getContentPane();
  84. c.setBackground(new Color(33,66,119));
  85.  
  86. f.setTitle("On-Screen Keyboard");
  87. f.setJMenuBar(m);
  88.  
  89. for(i=0;i<s.length;i++)
  90. c.add(j[i],JFrame.LEFT_ALIGNMENT);
  91.  
  92.  
  93. f.isMaximumSizeSet();
  94.  
  95. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96. f.setVisible(true);
  97. f.setFocusableWindowState(false);
  98.  
  99. try{
  100. Robot r = new Robot();
  101. r.delay(500);
  102. r.keyPress(KeyEvent.VK_7);
  103.  
  104. }
  105. catch (AWTException e) {
  106. e.printStackTrace();
  107. }
  108.  
  109. }
  110.  
  111.  
  112.  
  113. public static void main(String args[])
  114. {
  115. keyboard k = new keyboard();
  116.  
  117.  
  118. }
  119.  
  120. public void actionPerformed(ActionEvent e)
  121. {
  122. if(e.getSource() == m41)
  123. JOptionPane.showMessageDialog(null
  124. ,"Project By\nPirZada(20)\n"+
  125. "Salman Sayar(22)\n "+
  126. "Wajahat Kareem(16)","About On-Screen Keyboard",
  127. JOptionPane.INFORMATION_MESSAGE);
  128.  
  129. if(e.getSource() == j[16])
  130. {
  131.  
  132.  
  133.  
  134.  
  135. }
  136.  
  137. }
  138.  
  139.  
  140.  
  141.  
  142. }

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
Similar Threads
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Aug 20th, 2009
0

Re: Urgent Help!

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

        } catch (AWTException e) {
            e.printStackTrace();
        }
& try again
Quote ...
i tried robot class but it always give me an error
Error always is the source of information , carefully analyze it
Last edited by quuba; Aug 20th, 2009 at 5:03 pm. Reason: added
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Aug 20th, 2009
0

Re: Urgent Help!

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.
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Aug 20th, 2009
0

Re: Urgent Help!

Implement reciver
Quote ...
...then it write that on the notepad or else..
for example JTextField
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Aug 21st, 2009
0

Re: Urgent Help!

JTextField only work inside java i want to send the events to outside environment.
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Aug 21st, 2009
0

Re: Urgent Help!

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.
Last edited by VernonDozier; Aug 21st, 2009 at 12:26 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 21st, 2009
0

Re: Urgent Help!

> Urgent Help!
This is neither descriptive nor accurate
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 21st, 2009
0

Re: Urgent Help!

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....
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Aug 21st, 2009
0

Re: Urgent Help!

Click to Expand / Collapse  Quote originally posted by Majestics ...
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:

Quote ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 21st, 2009
0

Re: Urgent Help!

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.
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: acc program help
Next Thread in Java Forum Timeline: Can't install Java. Rawr.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC