basic sendkeys function

Discussion in 'Programming General' started by Swan, Sep 24, 2007.

basic sendkeys function
  1. Unread #1 - Sep 24, 2007 at 8:49 PM
  2. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    basic sendkeys function

    Well, this only sends lowercase text, but for what you guys are probably going to use it for (runescape) you probably won't care, because RS automatically makes things uppercase in the first place.

    Code:
    import java.awt.Robot;
    
    public void sendkeys(String text) {
       try {
           Robot robot = new Robot();
           String lol = text.toUpperCase();
           for(int i=0;i<lol.length();i++) {
               robot.keyPress(lol.charAt(i));
           }
       } catch(java.awt.AWTException exc) {
           System.out.println("error");
       }
    }
    heres a basic application that uses it (its declared as static here because main() is a static method):
    Code:
    import java.awt.Robot;
     
    public class Sendkeys {
        
        public static void sendkeys(String text) {
           try {
               Robot robot = new Robot();
               String lol = text.toUpperCase();
               for(int i=0;i<lol.length();i++) {
                   robot.keyPress(lol.charAt(i));
               }
           } catch(java.awt.AWTException exc) {
               System.out.println("error");
           }
        }
        
        public static void main(String[] args) {
        	try {
        		Runtime.getRuntime().exec("notepad");
        		Robot r = new Robot();
        		r.delay(1000);
        		sendkeys("Hello world!");
        	} catch(java.io.IOException exc) {
        		System.out.println("Error!");
        	} catch(java.awt.AWTException exc) {
        		System.out.println("Error!");
        	}
        }
    }
    Hope I helped.
     
  3. Unread #2 - Sep 24, 2007 at 9:00 PM
  4. Monster Hack
    Joined:
    Nov 19, 2005
    Posts:
    59
    Referrals:
    2
    Sythe Gold:
    0

    Monster Hack Member

    basic sendkeys function

    AH NICE

    -Mh
     
  5. Unread #3 - Oct 1, 2007 at 1:15 PM
  6. Olan14
    Joined:
    Jan 26, 2007
    Posts:
    581
    Referrals:
    0
    Sythe Gold:
    0

    Olan14 Forum Addict

    basic sendkeys function

    Epic failure to use this for making a RS2 Bot. Better:
    Code:
    class Test extends java.awt.Robot {  	Test(String s) 	{ 		try 		{ 			Runtime.getRuntime().exec(System.getenv("WINDIR") + "\\system32\\notepad.exe"); 			super(); 			this.delay(1000); 			sendKeys("Hallo wurld"); 		} 		catch(Throwable t0) 		{ 			t0.printStackTrace(); 		} 	}  	boolean sendKeys(String txt) 	{ 		for(char c : txt.toCharArray()) 		{ 			this.keyPress(c); 			this.keyRelease((int) c); 		} 		return true; 	}  }
     
  7. Unread #4 - Oct 1, 2007 at 9:53 PM
  8. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    basic sendkeys function

    shame - google didn't turn this up now, did it?
     
  9. Unread #5 - Oct 29, 2007 at 7:44 PM
  10. rscheater13
    Referrals:
    0

    rscheater13 Guest

    basic sendkeys function

    I'm curious as to where you get the string you want to print. I see where it is declared, but I don't see where it is set to anything? Or did you just leave that for us to do?
     
  11. Unread #6 - Oct 30, 2007 at 2:02 AM
  12. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    basic sendkeys function

    Code:
    public static void sendkeys(String text)
    Look at the signature. 'String text' is where you put the text you want to type.

    i.e.
    Code:
    sendkeys("Hello world!");
    sendkeys(JTextField1.getText());
     
  13. Unread #7 - Oct 30, 2007 at 8:42 PM
  14. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    basic sendkeys function

    Isn't calling one of your variables "lol" a bad naming convention?

    Also, out of curiosity, since I don't know Java but I know C++, does every function have to have a try/catch for exceptions?
     
  15. Unread #8 - Oct 30, 2007 at 9:31 PM
  16. rscheater13
    Referrals:
    0

    rscheater13 Guest

    basic sendkeys function

    Lol, thanks Swan, I feel stupid now.
     
  17. Unread #9 - Nov 1, 2007 at 10:52 PM
  18. speedster239
    Joined:
    Jan 21, 2007
    Posts:
    313
    Referrals:
    0
    Sythe Gold:
    0

    speedster239 Forum Addict
    Java Programmers

    basic sendkeys function

    Good job on this Swan!
     
  19. Unread #10 - Nov 2, 2007 at 2:59 AM
  20. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    basic sendkeys function

    SMR - No, however some things require an exception handling statement.

    For example, handling threads without an InteruptedException would just be dumb. Same thing with Robots, you're best off catching a java.awt.AWTException rather than crashing your program.

    Bad naming conventions my ass - I understand it. You should see my draft work. Variables such as gg, kk, roflmao, nubz, etc.

    Olan - I find it inefficient to extend a class when you don't edit or override anything in that class. I find it better to simply declare a new object.
     
  21. Unread #11 - Nov 6, 2007 at 7:37 PM
  22. slashshot007
    Joined:
    May 6, 2006
    Posts:
    164
    Referrals:
    0
    Sythe Gold:
    0

    slashshot007 Active Member

    basic sendkeys function

    i'm thinking about modifying it so it can use capitals. but not now lol.
     
  23. Unread #12 - Nov 8, 2007 at 3:35 AM
  24. Michael3455
    Joined:
    Sep 11, 2005
    Posts:
    134
    Referrals:
    0
    Sythe Gold:
    0

    Michael3455 Active Member

    basic sendkeys function

    Hmm, I might just be dumb here, but doesn't Robot.keyPress expect a key code (like KeyEvent.VK_L)? Also, why don't you need to do a KeyRelease?

    I haven't tried your code, and I'm assuming it works perfectly, but when I did both of those things on my computer tonight, it didn't work as expected. The only thing I could see is maybe the text.ToUpper changes it. o.0

    It's probably my brain just being stupid, but I'd be very happy if you could clear those things up for me.
     
  25. Unread #13 - Nov 8, 2007 at 3:46 AM
  26. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    basic sendkeys function

    I wrote this half asleep, so ja it might be a bit buggy, lol.

    Use Olan14's example, assuming you know what classes are and how to extract code from them ;)
     
  27. Unread #14 - Nov 8, 2007 at 3:50 AM
  28. Michael3455
    Joined:
    Sep 11, 2005
    Posts:
    134
    Referrals:
    0
    Sythe Gold:
    0

    Michael3455 Active Member

    basic sendkeys function

    Meh, I as having troube figuring out my own SendKey fuction, so I looked at yours and it was pretty different. But anyway, I was playing around, and the message.ToUpperCase(); seems to make it work as predicted, except that after you break from your loop (in the keypress function) you should have something like

    Code:
    int g = message.length() - 1;
    robot.keyRelease(message.charAt(g));
    
    So that it doesn't hold down the last character.

    Anyway, thanks. Your sendkeys showed me a few things!
     
< SMRDll - Autoing Functions - BETA | Green error ? double you tee eff ? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site