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

basic sendkeys function

Swan

When They Cry...
Joined
Jan 23, 2007
Sythe Gold
0
Messages
4,957
Age
32
Location
Australia
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.
 
basic sendkeys function
AH NICE

-Mh
 
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; 	}  }
 
basic sendkeys function
shame - google didn't turn this up now, did it?
 
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?
 
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());
 
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?
 
basic sendkeys function
Lol, thanks Swan, I feel stupid now.
 
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.
 
basic sendkeys function
i'm thinking about modifying it so it can use capitals. but not now lol.
 
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.
 
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 ;)
 
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!
 
Runescape Gamblingrunescape gambling
Back
Top