• Java: Emulating/Simulating Key Press
    3 replies, posted
Hello their at facepunch, I have heard you are good for forums and help. Recently I have been working on a way to emulate key press in Java. I have found some code that I would like to work on and I though, as I am new it would be a good idea to ask for a little bit of help, so here I go. This is the section of code that I would be confused with (I have a good understanding of variables and programming logic): try { //What does "try" mean? Would it mean it has to try and possible fail or something? Robot robot = new Robot(); // Simulate a mouse click robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); // Simulate a key press robot.keyPress(KeyEvent.VK_A); //Is this Key "A"? or is it some code for what key? robot.keyRelease(KeyEvent.VK_A); } catch (AWTException e) { //What is this catch for? I am guessing it is something to do with clearing what keys (being pressed down or up) are doing? e.printStackTrace(); } For example, would this script actually emulate keys in notepad for example? So I could make it type in Notepad or another program, it would need to emulate through windows so any program will pick it up as a normal key input. Thanks ~Joe
[QUOTE=ducsuus;36027082] try { //[B]What does "try" mean? Would it mean it has to try and possible fail or something?[/B] Robot robot = new Robot(); [/quote] Yes. try{ is the beginning of a try/catch statement which has to do with error handling. It will attempt to execute the code block within the brackets. If anything fails it will generate an exception which is handled by catch() down below. [quote] // Simulate a mouse click robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); // Simulate a key press robot.keyPress(KeyEvent.VK_A); //[B]Is this Key "A"? or is it some code for what key?[/B] robot.keyRelease(KeyEvent.VK_A); [/quote] Short answer to your question is yes, that's the 'a' key. Long answer, learn to read documentation, it's an essential part of programming. [url]http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html[/url] [quote] } catch (AWTException e) { //[B]What is this catch for? I am guessing it is something to do with clearing what keys (being pressed down or up) are doing?[/B] e.printStackTrace(); } [/quote] No, this is where errors are handled by the try/catch statement. The AWTException will be thrown if some error occurs within the AWT (simply put Java's primary way of dealing with user interfaces, and KeyEvent is a subclass of AWT). [quote] For example, would this script actually emulate keys in notepad for example? So I could make it type in Notepad or another program, it would need to emulate through windows so any program will pick it up as a normal key input. [/QUOTE] Again, read the KeyEvent documentation.
[QUOTE=ducsuus;36027082]try { //What does "try" mean? Would it mean it has to try and possible fail or something? } catch (AWTException e) { //What is this catch for? I am guessing it is something to do with clearing what keys (being pressed down or up) are doing? [/QUOTE] Why guess something when you could just... [url]http://bit.ly/J6zbiN[/url] and/or [url]http://bit.ly/J6zhqE[/url] Sorry if I'm a bit harsh, but you'll never get anywhere with programming if you run to other people for answers before doing your own research.
Well the thing is I have tried many times to find out about the keys, via google, and this is what made me ask in the first place, I have to understand why something is doing it's function before I can understand the rest of the script properly, it's just me. And thanks demoguy, very helpful, I hope that I will be able to make my mini program with this XD
Sorry, you need to Log In to post a reply to this thread.