Computer Science Canada

Java Keyboard Event

Author:  Vish [ Thu Jul 07, 2011 3:32 pm ]
Post subject:  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.

Author:  jrcdude [ Wed Jul 13, 2011 11:19 pm ]
Post subject:  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;
        }
}

Author:  Vish [ Sat Jul 16, 2011 7:51 am ]
Post subject:  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?

Author:  Vish [ Sat Jul 16, 2011 9:09 am ]
Post subject:  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 && sx<5){
sx+=1;

}
if (j==KeyEvent.VK_LEFT && sx>2){
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<7; i++){
for(int j=0; j<11; j++){
System.out.printf("%s",used[i][j]);
}
System.out.println();
}
}
}
}

There are 3 errors:

non-static variable sx cannot be referenced from a static context
while( sx!=5 ){

non-static variable sx cannot be referenced from a static context
used[sx][10]=ship;
^
non-static variable ship cannot be referenced from a static context
used[sx][10]=ship;
^


^

Author:  jrcdude [ Sat Jul 16, 2011 5:44 pm ]
Post subject:  RE:Java Keyboard Event

That's more of a Java syntax error.

It's the public static main void joke Razz

Author:  Vish [ Sat Jul 16, 2011 6:44 pm ]
Post subject:  Re: Java Keyboard Event

is there any way to make it go away? is there any way to run a program that doesn't extend Applet or anything else, without a static main statement.

Author:  RandomLetters [ Sat Jul 16, 2011 6:49 pm ]
Post subject:  RE:Java Keyboard Event

Nope.

Just create an instance of your program in the main method.

public etc main(String[] args) {
MyClass me = new MyClass();
me.doStuff();
}

Author:  DemonWasp [ Sat Jul 16, 2011 9:30 pm ]
Post subject:  RE:Java Keyboard Event

Well...you could also make the instance variable a static variable by adding "static" to its declaration. In general, though, it's better to make an instance.

Author:  Vish [ Tue Jul 19, 2011 4:11 pm ]
Post subject:  Re: Java Keyboard Event

I'm still having the problem that my program doesn't seem to be able use the key listener.

Here is the debugging version:


import java.awt.*;
import java.awt.event.*;

public class InitArray
implements KeyListener{

final String ship="M";
static int sx=5;

public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) {
int j=e.getKeyCode();
if (j==KeyEvent.VK_RIGHT){
sx+=1;

}
if (j==KeyEvent.VK_LEFT){
sx-=1;
}
}

public static void main(String[] args){
while( sx!=6 ){
String[][] used= {{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"}};
}
}
}

Technically, when it runs the "main" method, if the right arrow key is pushed, the program should print "hey". But it doesn't.

Author:  DemonWasp [ Tue Jul 19, 2011 4:22 pm ]
Post subject:  RE:Java Keyboard Event

Err, no it shouldn't. You don't have "hey" anywhere in your source code. You also haven't bothered to add an instance of the InitArray class to anything, and even if you had, you haven't done anything with the values you've changed in the keyPressed method, so you'll never see the effect of pressing the keys, even if the KeyListener was actively listening.

The pattern for using listeners is as follows:

1. Create a subclass that implements a Listener interface (done: InitArray implements KeyListener).

2. Create an instance of that subclass (not done). Use the new keyword.

3. Add that instance to the dispatcher (not done). In AWT, anything that extends Component is a dispatcher for KeyEvents, and you can add KeyListeners with addKeyListener().

Author:  Vish [ Tue Jul 19, 2011 5:25 pm ]
Post subject:  Re: Java Keyboard Event

import java.awt.*;
import java.awt.event.*;

public class InitArray{

final String ship="M";
static int sx=5;

public static void main(String[] args){
while( sx!=6 ){

Display disp= new Display();

String[][] used= {{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"},
{"|"," "," "," "," "," "," "," "," "," ","|"}};
if (sx==6){
break;
}
}
System.out.println("hey");
}
}

class Display extends InitArray
implements KeyListener{

public void keyReleased( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) {
int j=e.getKeyCode();
if (j==KeyEvent.VK_RIGHT){
super.sx+=1;

}
if (j==KeyEvent.VK_LEFT){
super.sx-=1;
}
}
}

This is what I have so far. Where do I put the "addKeyListener" and what would I add it to? The variable that is an instance of the subclass with the keylistener in it is called "disp".

Author:  DemonWasp [ Tue Jul 19, 2011 8:45 pm ]
Post subject:  RE:Java Keyboard Event

First, I'm not sure why you introduced the Display class. It definitely is NOT a display of any kind. It's a keylistener for the player, so perhaps PlayerKeyListener would be a better name.

Second, why does Display extend InitArray? That means that "A Display is an InitArray," which makes no sense whatsoever. You can remove that "extends InitArray".

You've managed step 2 that I listed above. Now, where are the AWT objects? Component? JButton? JTextArea? Any of those? If you don't have any of those, go read a tutorial on AWT, because you'll need to add some. Once you find the AWT object you want to add the listener to, it's just:

code:

[object that is listening].addKeyListener ( playerKeyListener );

Author:  S_Grimm [ Sun Sep 11, 2011 10:51 pm ]
Post subject:  Re: RE:Java Keyboard Event

DemonWasp @ Tue Jul 19, 2011 8:45 pm wrote:
First, I'm not sure why you introduced the Display class. It definitely is NOT a display of any kind. It's a keylistener for the player, so perhaps PlayerKeyListener would be a better name.

Second, why does Display extend InitArray? That means that "A Display is an InitArray," which makes no sense whatsoever. You can remove that "extends InitArray".

You've managed step 2 that I listed above. Now, where are the AWT objects? Component? JButton? JTextArea? Any of those? If you don't have any of those, go read a tutorial on AWT, because you'll need to add some. Once you find the AWT object you want to add the listener to, it's just:

code:

[object that is listening].addKeyListener ( playerKeyListener );


It appears that he doesn't want to create an AWT object to assign the listener to. Rather he wants to have the user push a key and have the cmd prompt display the output based on what key was pressed.

Author:  Vish [ Mon Sep 12, 2011 6:13 am ]
Post subject:  Re: Java Keyboard Event

That's exactly what I want. I want the command to recognize it as a KeyEvent without tying it down to any one element. Then it should send that event to the keyPressed, keyTyped etc. and then act accordingly.

Author:  DemonWasp [ Mon Sep 12, 2011 8:00 am ]
Post subject:  RE:Java Keyboard Event

It's a little early in the morning so I'm still foggy. Can you explain the use-case here? For example, what kind of application is this? What is the user doing (inputting text, using shortcuts, ...)? What does your code want to do when it gets input?

Author:  S_Grimm [ Mon Sep 12, 2011 8:23 am ]
Post subject:  RE:Java Keyboard Event

It seems that he wants the user to be looking at a cmd prompt and being prompted to push an arrow key. He then seems to be wanting to catch the action event and respond to it by outputting text based on what arrow key was pressed.

Author:  DemonWasp [ Mon Sep 12, 2011 11:10 am ]
Post subject:  RE:Java Keyboard Event

Then he may have a difficult time, because that's not what a command prompt is used for. The prompt itself listens to the arrow keys, insert-delete-home-end-pgup-pgdn and other keys. The command prompt is built for line-by-line input, not for listening to individual keys.

If you want to listen to individual keys in an environment like the command prompt, then you will probably have to build the command prompt yourself (a Window with a scrolling text box, usually).

Author:  Tony [ Mon Sep 12, 2011 11:35 am ]
Post subject:  Re: RE:Java Keyboard Event

DemonWasp @ Mon Sep 12, 2011 11:10 am wrote:
If you want to listen to individual keys in an environment like the command prompt, then you will probably have to build the command prompt yourself

Or use GNU readline library.

Author:  DemonWasp [ Mon Sep 12, 2011 12:36 pm ]
Post subject:  Re: RE:Java Keyboard Event

Tony @ Mon Sep 12, 2011 11:35 am wrote:
Or use GNU readline library.


...which has to be wrapped in a Java library to use in a Java program. For example: http://java-readline.sourceforge.net/


: