• Log in or sign up for an account here: https://www.sythe.org/login

basic sendkeys function

Swan

When They Cry...

Retired Global Moderator
Joined
Jan 23, 2007
Sythe Gold
0
Messages
4,957
Age
32
Location
Australia
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
AH NICE

-Mh
 
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; 	}  }
 
Member of the Month Winner
Sythe's 10th Anniversary
shame - google didn't turn this up now, did it?
 
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?
 
Member of the Month Winner
Sythe's 10th Anniversary
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());
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
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?
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
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.
 
Member of the Month Winner
Sythe's 10th Anniversary
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 ;)
 
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!
 
Runescape Gamblingrunescape gambling
Back
Top