This site requires JavaScript, please enable it in your browser!
Greenfoot back
JetLennit
JetLennit wrote ...

2013/6/5

Java keys

JetLennit JetLennit

2013/6/5

#
While I know this is not technically "Greenfoot"...... how do I detect keys with normal java? So, I want to know how to do this in strait java
if(Greenfoot.isKeyDown("somekey"))
{
     do stuff
}
I have tried to look at the source but I am yet to find it.... any help would be greatly appreciated! (need this because I am working in jMonkey)
HaX101 HaX101

2013/6/5

#
if (key=="somekey") { do stuff }
JetLennit JetLennit

2013/6/5

#
It says "can not find symbol symbol: variable key location: class Main"
Gevater_Tod4711 Gevater_Tod4711

2013/6/5

#
In JMonkey there probably are methods you can use like Greenfoot.isKeyDown() but in java there are ActionEvents that tell you whether a key / a button is pressed or anything else happens. If a key is pressed that throws a KeyEvent. E.g. in a GUI you can add KeyListeners to a JTextField or whatever you want to use:
textField.addKeyListener(new KeyAdapter() {
	@Override
	public void keyPressed(KeyEvent arg0) {
		if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {//the enter key in this example;
			//do something;
		}
	}
});
If a key has pressed the KeyListener will listen to this event and the code in the method keyPressed(KeyEvent arg0) will be executed. Also it's possible to declare separate classes for the KeyListeners. The only condition for this class is it has to implement the interface KeyListener.
JetLennit JetLennit

2013/6/5

#
Yeah, I also wanted to know what it was for java.. thanks!
You need to login to post a reply.