06-5-2012, 07:34 AM | #1 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Program works, but doesn't
I was trying to make a program that whenever I cause a KeyEvent, the program "presses" the left key, causing me to type backwards in an unorthodox way.
Code:
package start; import java.awt.Robot; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; public class Main { public static void main(String[] args) throws Exception{ @SuppressWarnings("unused") abstract class AL implements ActionListener{ public void actionPerformed(KeyEvent e) throws Exception { Robot r = new Robot(); if(e.isActionKey()){ r.keyPress(KeyEvent.VK_LEFT); r.keyRelease(KeyEvent.VK_LEFT); } } } } } I can't figure it out, my teacher can't figure it out. HALP Edit: Yes I know Username, I'm using Robot again, don't mock me :P
__________________
Quote:
Last edited by SKG_Scintill; 06-5-2012 at 07:41 AM.. |
|
06-5-2012, 08:34 AM | #2 |
the Haku
Join Date: Jul 2005
Age: 35
Posts: 4,522
|
Re: Program works, but doesn't
Question for you, how do you know the program didn't press/release left arrow ?
I see Robot and I see the r. before keyPress etc. but there's really nothing else to add? Last edited by Hakulyte; 06-5-2012 at 08:38 AM.. |
06-5-2012, 08:42 AM | #3 |
I don't get no respect
Join Date: Jun 2006
Age: 32
Posts: 1,332
|
Re: Program works, but doesn't
I'm not a java guy, but wouldn't you want a loop in there somewhere? I don't see one, but I'm not familiar with java..
|
06-5-2012, 09:49 AM | #4 | |||
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Program works, but doesn't
Quote:
Quote:
That being said, I can not tell whether it is waiting for the input or not. Given the length of the run, I can assume it doesn't, but it's not certain.
__________________
Quote:
|
|||
06-5-2012, 09:55 AM | #5 |
FFR Veteran
|
Re: Program works, but doesn't
I've not seen @SuppressWarnings before. That's new to me.
Why is your AL class abstract? Also why are you defining that class within the main function of your other class? --edit-- Also, even if this worked correctly wouldn't it cause a sort of infinite loop? Considering you register an event listener for keyboard events, yet within that listener you trigger just such an event? --edit2-- In your keypress-testing code, also check to make sure the key pressed is NOT left and, if it is, then skip the simulated left key press. That should remove such an infinite loop from there. You can't directly use abstract classes. Abstract means it doesn't fully implement everything and needs to be extended before being used. Change the abstract keyword to public, remove the suppression of warnings, and if it tells you some method or other needs to be implemented, you can always implement do-nothing methods which merely do nothing and/or call super (if applicable) and then return. And I don't know why you're making a class-within-a-class.
__________________
[SIGPIC][/SIGPIC] Last edited by UserNameGoesHere; 06-5-2012 at 10:18 AM.. |
06-5-2012, 10:10 AM | #6 | |||
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Program works, but doesn't
Quote:
Otherwise I had to make my KeyEvent a variable, which I would have to validate, which would cause a NullPointerException error. It was an issue I had before. Quote:
I was basically going for an infinite loop (as said with the ActionListener), until I terminate the program myself.
__________________
Quote:
|
|||
06-5-2012, 10:26 AM | #7 |
FFR Veteran
|
Re: Program works, but doesn't
Well what I mean is, even if the program worked, you would essentially lose control over your keyboard and get infinite left arrow presses as soon as you pressed any key. That is probably not what you intended. The reason for this is the press which is simulated within the listener would itself trigger the listener which would then resimulate another press which would trigger the same listener again, etc... You need to check to see if the left arrow key is the one which is pressed and, if so, skip over the "simulate left arrow press" instructions.
|
06-5-2012, 10:38 AM | #8 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Program works, but doesn't
True, it's good that you foresee that, quite resemblant to my previous problem with Robot, I'll continue fumbling tomorrow.
I tried using KeyTyped instead of KeyEvent, but obviously that won't work, given that KeyTyped is a subclass of KeyEvent. I'll try making another body around the "if" using KeyTyped, but I don't see much hope in that.
__________________
Quote:
|
|
06-5-2012, 08:50 PM | #9 |
FFR Veteran
|
Re: Program works, but doesn't
Here's what I think you should do.
First off, I'm not sure if your teacher is teaching you to use the class-within-a-class thing or not but that's a very non-Java way of doing things. It should be used sparingly, if ever. You just really never see this sort of thing with Java code. Get rid of the Main wrapper class because I don't see that it's doing anything useful and stick your public static void main within your AL class. Within actionPerformed, either do an additional check (in addition to e.isActionKey()) to make sure the key that is pressed is not the left key. Alternately, instead of that, you could check if a flag is set, and, if not, set the flag and simulate the left key press, then unset the flag. Or something along those lines. And in your main function you could just use an infinite loop I think. Something like while(true){}. You just need to kill your program using, for example, Windows Task Manager if you do it that way. Alternately in your main function, in a while loop, check if a flag is set and, if so, exit program. And in your actionPerformed, set that flag if a particular key is pressed (Say, the Escape key for example). |
06-5-2012, 09:32 PM | #10 |
stepmania archaeologist
Join Date: Aug 2005
Age: 34
Posts: 4,090
|
Re: Program works, but doesn't
Yeah, you definitely need some kind of infinite loop, or maybe some kind of window that will sit there and keep the program open until it's closed. Right now the program is basically creating the objects, hitting the end of the main function, and exiting immediately.
PS: Are you required to use Java for this? If not, AutoHotKey would probably be more useful.
__________________
Best AAA: Policy In The Sky [Oni] (81) Best SDG: PANTS (86) Best FC: Future Invasion (93) |
06-6-2012, 12:30 AM | #11 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Program works, but doesn't
Nah, it's not required, the main project at school is finished, we're all just trying to kill time.
@Username: I'll have to carefully read that post word by word.
__________________
Quote:
|
|
06-6-2012, 07:33 AM | #12 | |
Spun a twirly fruitcake,
Join Date: Feb 2009
Age: 31
Posts: 3,865
|
Re: Program works, but doesn't
So far it doesn't close immediately, I made a safety thread.sleep in case the robot goes looping (which it probably will eventually).
I changed the ActionEvent into a KeyEvent, that was an obvious mistake solved, seemed so simple a solution. It's probably my lack of knowledge when it comes to "abstract"-classes, but it says KL is never used locally. I understand it says that, but I don't know how to solve it :P Well this is what I have now: Code:
package start; import java.awt.Robot; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Main { public static void main(String[] args){ while(true){ abstract class KL implements KeyListener{ public void keyTyped(KeyEvent e){ if(e.isActionKey()){ try { Robot r = new Robot(); Thread.sleep(1000); r.keyPress(KeyEvent.VK_LEFT); r.keyRelease(KeyEvent.VK_LEFT); } catch (Exception e1) { e1.printStackTrace(); } } } } } } }
__________________
Quote:
|
|
06-6-2012, 06:40 PM | #13 |
stepmania archaeologist
Join Date: Aug 2005
Age: 34
Posts: 4,090
|
Re: Program works, but doesn't
The abstract class KL ...{} clause only *defines* the class. If you want to actually listen to keys, you would basically want to create an instance of a KeyListener subclass, and then attach it to something so that the key events get sent to it.
Also, note that you can't actually create an instance of an abstract class, although you can define subclasses of it. I don't think you want the class to be abstract in this case.
__________________
Best AAA: Policy In The Sky [Oni] (81) Best SDG: PANTS (86) Best FC: Future Invasion (93) |
06-7-2012, 01:46 AM | #14 |
FFR Veteran
|
Re: Program works, but doesn't
What qqwref said. The abstract keyword defines an incomplete class. You would first need to define another class that extends that class and create an object of the extending class. You could then cast it to the abstract class. It basically means you're making a class but not implementing all the methods and some extending class will finish implementing the methods.
If it makes more sense, you can think of an abstract class as kind of a cross between a class and an interface except you use the 'extends' rather than 'implements' keyword with it. Also, as qqwref said, you need to create an object, and you may or may not need to first register the listener with something. --edit-- Code:
package start; import java.awt.Robot; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class KL implements KeyListener{ private boolean inhibitKeyPress; private Robot r; public KL(){ super(); inhibitKeyPress = false; } public static void main(String[] args){ KL kl = new KL(); //Insert any needed registration code here while(true); } public void keyTyped(KeyEvent e){ if(e.isActionKey() && !inhibitKeyPress){ try { inhibitKeyPress = true; r = new Robot(); r.keyPress(KeyEvent.VK_LEFT); r.keyRelease(KeyEvent.VK_LEFT); } catch (Exception e1) { e1.printStackTrace(); } finally { inhibitKeyPress = false; } } } }
__________________
[SIGPIC][/SIGPIC] Last edited by UserNameGoesHere; 06-7-2012 at 05:27 AM.. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|