Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 06-5-2012, 07:34 AM   #1
SKG_Scintill
Spun a twirly fruitcake,
FFR Simfile AuthorFFR Veteran
 
SKG_Scintill's Avatar
 
Join Date: Feb 2009
Age: 31
Posts: 3,865
Default 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);
				}
			}
		}
	}
}
The code shows no errors, but whenever I run it, it runs for a fraction of a second, then terminates.
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:
Originally Posted by bluguerilla
So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
___
. RHYTHMS PR LAYERING
. ZOMG I HAD TO QUIT OUT TERRIBLE
.

Last edited by SKG_Scintill; 06-5-2012 at 07:41 AM..
SKG_Scintill is offline   Reply With Quote
Old 06-5-2012, 08:34 AM   #2
Hakulyte
the Haku
Retired StaffD7 Elite KeysmasherFFR Veteran
 
Hakulyte's Avatar
 
Join Date: Jul 2005
Age: 35
Posts: 4,522
Default 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..
Hakulyte is offline   Reply With Quote
Old 06-5-2012, 08:42 AM   #3
trumaestro
I don't get no respect
FFR Simfile AuthorFFR Veteran
 
trumaestro's Avatar
 
Join Date: Jun 2006
Age: 32
Posts: 1,332
Default 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..
trumaestro is offline   Reply With Quote
Old 06-5-2012, 09:49 AM   #4
SKG_Scintill
Spun a twirly fruitcake,
FFR Simfile AuthorFFR Veteran
 
SKG_Scintill's Avatar
 
Join Date: Feb 2009
Age: 31
Posts: 3,865
Default Re: Program works, but doesn't

Quote:
Originally Posted by Hakulyte View Post
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?
I know it didn't press left, since I had my cursor on the code somewhere, and it didn't go left :P

Quote:
Originally Posted by trumaestro View Post
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..
Yes, the loop is basically what I already tried to prevent my making this class to only trigger upon a key event, otherwise it waits for one.
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:
Originally Posted by bluguerilla
So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
___
. RHYTHMS PR LAYERING
. ZOMG I HAD TO QUIT OUT TERRIBLE
.
SKG_Scintill is offline   Reply With Quote
Old 06-5-2012, 09:55 AM   #5
UserNameGoesHere
FFR Veteran
FFR Veteran
 
UserNameGoesHere's Avatar
 
Join Date: May 2008
Posts: 1,114
Send a message via AIM to UserNameGoesHere
Default 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.
__________________
Quote:
Originally Posted by Crashfan3 View Post
Man, what would we do without bored rednecks?
[SIGPIC][/SIGPIC]

Last edited by UserNameGoesHere; 06-5-2012 at 10:18 AM..
UserNameGoesHere is offline   Reply With Quote
Old 06-5-2012, 10:10 AM   #6
SKG_Scintill
Spun a twirly fruitcake,
FFR Simfile AuthorFFR Veteran
 
SKG_Scintill's Avatar
 
Join Date: Feb 2009
Age: 31
Posts: 3,865
Default Re: Program works, but doesn't

Quote:
Originally Posted by UserNameGoesHere View Post
I've not seen @SuppressWarnings before. That's new to me.
It has no practical purpose, it just gets rid of yellow underscoring.
Quote:
Originally Posted by UserNameGoesHere View Post
Why is your AL class abstract?
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:
Originally Posted by UserNameGoesHere View Post
Also why are you defining that class within the main function of your other class?
ActionListener goes hand-in-hand with actionPerformed. ActionListener waits for input, actionPerformed reacts to the input. From what I've heard, it makes the Listener a loop, but the Performed an "if"-method in the loop. Something which I undoubtly NOT program myself :P
Quote:
Originally Posted by UserNameGoesHere View Post
--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?
I was basically going for an infinite loop (as said with the ActionListener), until I terminate the program myself.
__________________





Quote:
Originally Posted by bluguerilla
So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
___
. RHYTHMS PR LAYERING
. ZOMG I HAD TO QUIT OUT TERRIBLE
.
SKG_Scintill is offline   Reply With Quote
Old 06-5-2012, 10:26 AM   #7
UserNameGoesHere
FFR Veteran
FFR Veteran
 
UserNameGoesHere's Avatar
 
Join Date: May 2008
Posts: 1,114
Send a message via AIM to UserNameGoesHere
Default 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.
__________________
Quote:
Originally Posted by Crashfan3 View Post
Man, what would we do without bored rednecks?
[SIGPIC][/SIGPIC]
UserNameGoesHere is offline   Reply With Quote
Old 06-5-2012, 10:38 AM   #8
SKG_Scintill
Spun a twirly fruitcake,
FFR Simfile AuthorFFR Veteran
 
SKG_Scintill's Avatar
 
Join Date: Feb 2009
Age: 31
Posts: 3,865
Default 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:
Originally Posted by bluguerilla
So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
___
. RHYTHMS PR LAYERING
. ZOMG I HAD TO QUIT OUT TERRIBLE
.
SKG_Scintill is offline   Reply With Quote
Old 06-5-2012, 08:50 PM   #9
UserNameGoesHere
FFR Veteran
FFR Veteran
 
UserNameGoesHere's Avatar
 
Join Date: May 2008
Posts: 1,114
Send a message via AIM to UserNameGoesHere
Default 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).
__________________
Quote:
Originally Posted by Crashfan3 View Post
Man, what would we do without bored rednecks?
[SIGPIC][/SIGPIC]
UserNameGoesHere is offline   Reply With Quote
Old 06-5-2012, 09:32 PM   #10
qqwref
stepmania archaeologist
Retired StaffFFR Simfile AuthorFFR Veteran
 
qqwref's Avatar
 
Join Date: Aug 2005
Age: 34
Posts: 4,090
Default 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)
qqwref is offline   Reply With Quote
Old 06-6-2012, 12:30 AM   #11
SKG_Scintill
Spun a twirly fruitcake,
FFR Simfile AuthorFFR Veteran
 
SKG_Scintill's Avatar
 
Join Date: Feb 2009
Age: 31
Posts: 3,865
Default 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:
Originally Posted by bluguerilla
So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
___
. RHYTHMS PR LAYERING
. ZOMG I HAD TO QUIT OUT TERRIBLE
.
SKG_Scintill is offline   Reply With Quote
Old 06-6-2012, 07:33 AM   #12
SKG_Scintill
Spun a twirly fruitcake,
FFR Simfile AuthorFFR Veteran
 
SKG_Scintill's Avatar
 
Join Date: Feb 2009
Age: 31
Posts: 3,865
Default 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:
Originally Posted by bluguerilla
So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
___
. RHYTHMS PR LAYERING
. ZOMG I HAD TO QUIT OUT TERRIBLE
.
SKG_Scintill is offline   Reply With Quote
Old 06-6-2012, 06:40 PM   #13
qqwref
stepmania archaeologist
Retired StaffFFR Simfile AuthorFFR Veteran
 
qqwref's Avatar
 
Join Date: Aug 2005
Age: 34
Posts: 4,090
Default 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)
qqwref is offline   Reply With Quote
Old 06-7-2012, 01:46 AM   #14
UserNameGoesHere
FFR Veteran
FFR Veteran
 
UserNameGoesHere's Avatar
 
Join Date: May 2008
Posts: 1,114
Send a message via AIM to UserNameGoesHere
Default 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;
			}
		}
	}
}
I don't have a compiler with me right now, but your code should look more something along those lines than what you have. Start with that, and fix up anything that needs fixing from there (for example, I don't know if you need to first register that listener with something or not. If it needs, it, you'll need to enter the registration code where my comment suggests.)
__________________
Quote:
Originally Posted by Crashfan3 View Post
Man, what would we do without bored rednecks?
[SIGPIC][/SIGPIC]

Last edited by UserNameGoesHere; 06-7-2012 at 05:27 AM..
UserNameGoesHere is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution