Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Keyboard Event
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Vish




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
jrcdude




PostPosted: 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;
        }
}
Vish




PostPosted: 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?
Vish




PostPosted: 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;
^


^
jrcdude




PostPosted: 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
Vish




PostPosted: 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.
RandomLetters




PostPosted: 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();
}
DemonWasp




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Vish




PostPosted: 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.
DemonWasp




PostPosted: 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().
Vish




PostPosted: 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".
DemonWasp




PostPosted: 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 );
S_Grimm




PostPosted: 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.
Vish




PostPosted: 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.
DemonWasp




PostPosted: 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?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: