
-----------------------------------
Vish
Thu Jul 07, 2011 3:32 pm

Java Keyboard Event
-----------------------------------
I'm new to java so i don't really know what I'm doing. 

Here's a little code that i can't work out.

The point is for me to be able to check for keyboard events while running the main loop, without any JThis or JThat.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class HelloWorld extends Applet{
	int a=50;
	int b=8;
	int c=a;
	int d=99;
	int e=d;
	String where;
	public int keyPressed(KeyEvent gg) {
		int buttondown;
	    if (gg.getKeyCode() == KeyEvent.VK_RIGHT ) {
			buttondown=1;
	    } else if (gg.getKeyCode() == KeyEvent.VK_LEFT ) {
	            buttondown=2;
	    } else if (gg.getKeyCode() == KeyEvent.VK_UP ) {
	            buttondown=3;
	    } else if (gg.getKeyCode() == KeyEvent.VK_DOWN ) {
	            buttondown=4;
	    }
		return buttondown;

	}
	public void paint(Graphics g){
		//g.drawString( "Hello World", 50,25);
  		g.drawRect (a,b,d,e);
  		g.drawLine(a,b,c,b);
		g.drawLine(a,b+d,c,b+e);
		g.drawLine(a,b,c,b+d);
		int www=keyPressed();
		if (www==1){
			a+=1;
			c-=1;
			d-=1;
			e-=1;
		}
	}
}

This is the only error that comes up:

keyPressed(java.awt.event.KeyEvent) in HelloWorld cannot be applied to ()
		int www=keyPressed();
		        ^
1 error

Any help would be duly appreciated.

-----------------------------------
jrcdude
Wed Jul 13, 2011 11:19 pm

Re: Java Keyboard Event
-----------------------------------
The first example on this page should help:

http://profs.etsmtl.ca/mmcguffin/learn/java/05-keyboardInput/


And for your code, you can modify it to be like this:

[CODE]
public int keyPressed(KeyEvent gg) { 
	switch(gg.getKeyCode())
	{
		case: KeyEvent.VK_RIGHT
		return 1;
		break;

		case: KeyEvent.VK_LEFT
		return 2;
		break;

		case: KeyEvent.VK_UP
		return 3;
		break;

		case: KeyEvent.VK_DOWN
		return 4;
		break;
	}
}
[/CODE]

-----------------------------------
Vish
Sat Jul 16, 2011 7:51 am

Re: Java Keyboard Event
-----------------------------------
Thanks.

Just one question though.

What if I want to utilize KeyListener without the use of an Applet or JFrame or anything else.

If I just want to run a normal "main" loop without the use of graphics and only printing things out, will I be able to use KeyListener?

-----------------------------------
Vish
Sat Jul 16, 2011 9:09 am

Re: Java Keyboard Event
-----------------------------------
This is some code that I've been trying so that I can use KeyListener without extending the Applet or anything else, just the main loop.

import java.awt.*;
import java.awt.event.*;

public class InitArray
	implements KeyListener{

	final String ship="";
	int sx=5;

	public void keyPressed( KeyEvent e ) {
		int j=e.getKeyCode();
		if (j==KeyEvent.VK_RIGHT && sx2){
			sx-=1;
		}
   	}
   public void keyReleased( KeyEvent e ) { }
   public void keyTyped( KeyEvent e ) { }

	public static void main(String[] args){
	while( sx!=5 ){
		String[][] used=	{{"|"," "," "," "," "," "," "," "," "," ","|"},
						 	{"|"," "," "," "," "," "," "," "," "," ","|"},
						 	{"|"," "," "," "," "," "," "," "," "," ","|"},
						 	{"|"," "," "," "," "," "," "," "," "," ","|"},
						 	{"|"," "," "," "," "," "," "," "," "," ","|"},
						 	{"|"," "," "," "," "," "," "," "," "," ","|"},
						 	{"|"," "," "," "," "," "," "," "," "," ","|"}};
		used[sx][10]=ship;
		for(int i=0; i