Adblock breaks this site

basic sendkeys function

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

  1. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    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.
     
  2. Monster Hack

    Monster Hack Member

    Joined:
    Nov 19, 2005
    Posts:
    59
    Referrals:
    2
    Sythe Gold:
    0
    basic sendkeys function

    AH NICE

    -Mh
     
  3. Olan14

    Olan14 Forum Addict

    Joined:
    Jan 26, 2007
    Posts:
    581
    Referrals:
    0
    Sythe Gold:
    0
    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; 	}  }
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    basic sendkeys function

    shame - google didn't turn this up now, did it?
     
  5. rscheater13

    rscheater13 Guest

    Referrals:
    0
    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?
     
  6. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    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());
     
  7. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    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
    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?
     
  8. rscheater13

    rscheater13 Guest

    Referrals:
    0
    basic sendkeys function

    Lol, thanks Swan, I feel stupid now.
     
  9. speedster239

    speedster239 Forum Addict
    Java Programmers

    Joined:
    Jan 21, 2007
    Posts:
    313
    Referrals:
    0
    Sythe Gold:
    0
    basic sendkeys function

    Good job on this Swan!
     
  10. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    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.
     
  11. slashshot007

    slashshot007 Active Member

    Joined:
    May 6, 2006
    Posts:
    164
    Referrals:
    0
    Sythe Gold:
    0
    basic sendkeys function

    i'm thinking about modifying it so it can use capitals. but not now lol.
     
  12. Michael3455

    Michael3455 Active Member

    Joined:
    Sep 11, 2005
    Posts:
    134
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  13. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    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 ;)
     
  14. Michael3455

    Michael3455 Active Member

    Joined:
    Sep 11, 2005
    Posts:
    134
    Referrals:
    0
    Sythe Gold:
    0
    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 ? >


 
 
Adblock breaks this site