This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes reading input from keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading input from keys" Watch "reading input from keys" New topic
Author

reading input from keys

Jimbo Jenkins
Greenhorn

Joined: Apr 03, 2009
Posts: 5
So I have a project for my class, where I need to make a pacman game. So I want to be able to control pacman using the arrow keys. We are using gridworld(if any of you have heard of it) to make the game in, which is a grid thing that you can put objects in and manipulate them. So basically, I have been looking at KeyListener, buts seems like it requires a textfield(something I can't use for the project). Are there any other ways to check if a button was pressed. This is a turn based version, so it doesn't have to read continuously.

Hopefully this clarifies things, thanks in advance.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Welcome to the Ranch.

For your question, you definitely need KeyListener or KeyAdapter. But only text field I doubt that. Ok ask yourself this, what component supports key listeners?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
You can use things like System.in.read(); which reads one keystroke at a time. Have a look at the API documentation and see whether that is of any use.
Jimbo Jenkins
Greenhorn

Joined: Apr 03, 2009
Posts: 5
Alright, so K. Tsang, I'm not really sure what a component is, but at least I know I don't need a textfield. I tried implementing Keylistener, but it gives me a "cannot find symbol" error. I'm pretty sure you import java.awt.* but I might be wrong. Campbell, I'm not really sure how to use read, but it would probably be something like this? I'm not sure.


Please correct me if I'm wrong, but I presume this is how it is used, or would I have to make an object of sorts? Thanks for the replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Try it. It looks promising. You might be better off with a switch-case-default block however. You will have to try it, and put a "System.out.println(x);" line in, just to see whether you get a useful input from the four arrow keys. You may need it inside a while loop, which tests whether you have entered a key representing "exit."

Beware: The System.in.read() method declares it throws an IOException, which is checked, so you must deal with that exception somehow.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Oh, another point. Look up the return type of System.in.read(). I think it is int not char, so you might need a cast.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
I tried this to see what happensDo it yourself and see what happens.
Jimbo Jenkins
Greenhorn

Joined: Apr 03, 2009
Posts: 5
Alright, so I tried the program using keys and arrows, but nothing happened, and yes I did copy it correcly. I ran it, and pressed some keys, but no output.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
So it didn't work? I got it to work, but only by using the enter key. It obviously doesn't answer your problem.

KeyListeners might work, but I think that would require a GUI component which has the focus. I have never found a way of implementing the "press any key to continue" in Java.

Has anybody else got any ideas, please?
Jimbo Jenkins
Greenhorn

Joined: Apr 03, 2009
Posts: 5
Well my teacher mentioned something about ActionListener. Not sure what that is all about though, thought it was related to KeyListener. I guess I should check that out. Also, what happened when you hit enter?
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
You will have more luck looking in the Games section of JavaRanch, but besides that I have done many games in Java and KeyListeners work great (including arrow keys, and no you don't need a text box). I am at work so I do not have any code with me at the moment but I will get some out when I get home for you.

As this is a learning site and not a "do your homework for you" site I'll only be posting code fragments and psudo-code. It should be enough though for you to figure the rest out from (sorry). When is your assignment due by? Have you gotten the rest of the program done? Is anything else holding you up? Are you adding mouse functionality to it (not much more code then keyboard reading)?

I don't know what your level of experience or how involved the pacman game is but keep in mind that if you're programming correctly using object oriented methods then even if you are completely hammered on the key-reading part you should still be able to write everything else. Hope that made sense. I should be able to get something posted for you in 2-3 hours.

SCJA
~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
PM sent
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I've never used the Gridworld API, (never even heard of it until about 5 minutes ago). But from just a cursory look at the documentation for info.gridworld.world.World<T>, it exposes a method named keyPressed(String, Location) who's documentation reads:

This method is called when a key was pressed. Override it if your world wants to consume some keys (e.g. "1"-"9" for Sudoku). Don't consume plain arrow keys, or the user loses the ability to move the selection square with the keyboard.

Parameters:
description - the string describing the key, in this format.
loc - the selected location in the grid at the time the key was pressed
Returns:
true if the world consumes the key press, false if the GUI should consume it.


Perhaps, this is the method you should be looking to implement.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jimbo Jenkins
Greenhorn

Joined: Apr 03, 2009
Posts: 5
Hey Brian, thanks for the PM. I know that I shouldn't ask people for homework, which is why I asked specifically for the key input. The rest of my program is done, it's a turn based game, so the ghosts wait for pacman to move before they move(I know it weird, but its how gridworld is set up). Also Garrett, we never received that part of the API, which would explain why I never saw that. I will check it out and that should be much easier to integrate into the program.


Thanks everybody fr the help. It's much appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Brian Legg wrote:PM sent
Please don't. We all want to see the solution. Look at this FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading input from keys
 
Similar Threads
Question about sprite movement
Possible Java Applications
Refactoring
This was my idea for a project... I would like some feedback.
Handle multiple things at once