Welcome to Dream.In.Code
Become a Java Expert!

Join 149,614 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,851 people online right now. Registration is fast and FREE... Join Now!




How to read key presses without disrupting the flow of the program?

 
Reply to this topicStart new topic

How to read key presses without disrupting the flow of the program?

novamanss2002
21 Sep, 2007 - 07:52 PM
Post #1

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 29


My Contributions
Hey it's novaman,

I Practice programming by developing games. I am trying to develop a dos based Black Jack game.
Though it is only in dos I am trying to give it a good look and feel.

I would like to know how to input keyboard key depressions without stopping the flow of the program.
This input is required on line 55 as indicated in the code.

This is just the skeleton, I need this solution to continue development.

CODE


public class OpenScreen
{
    byte LoopStop1 = 0;
    byte keypress1 = 0;

    public OpenScreen( )
    {

        System.out.println( "                 ****************" );
        System.out.println( "                *** WELCOME TO ***" );
        System.out.println( "                 ****************\n" );


///////////////////// Clock Stop///////////////////
                try                              
                {                                
                    Thread.sleep(1000);          
                }                                
                catch (InterruptedException ex)  
                   {                                
                        }                              
///////////////////////////////////////////////////

        System.out.println( "A-K-Q-J-10-9-8-7-6-5-4-3-2-A-K-Q-J-10-9-8-7-6-5-4-3-2");
        System.out.println( "A-K-Q-J-10-9-**************************-8-7-6-5-4-3-2");
        System.out.println( "A-K-Q-J-10-9-********BLACK JACK********-8-7-6-5-4-3-2");
        System.out.println( "A-K-Q-J-10-9-**************************-8-7-6-5-4-3-2");
        System.out.println( "A-K-Q-J-10-9-8-7-6-5-4-3-2-A-K-Q-J-10-9-8-7-6-5-4-3-2");
         System.out.printf( "\n\n\n\n\n\n\n\n\n\n\n\n\n" );

///////////////////// Clock Stop///////////////////
                try                              
                {                                
                    Thread.sleep(1000);          
                }                                
                catch (InterruptedException ex)  
                   {                                
                        }                                
///////////////////////////////////////////////////

///////////////////// Clear I/O ///////////////////
        int i = 0;
                                        
        while ( i < 1000 )          
        {                                
             System.out.println( " " );  
                         ++i;                        
        }                                
///////////////////////////////////////////////////


        while ( LoopStop1 == 0 )
        {
            /*    *** This is where I need to be able to imput key presses without
               disrupting the flow of the program. Can anyone help?*** */

            if ( keypress1 == 1 )
            {
                ++LoopStop1;
            }
        }

    }
}/////////////////////////////////////////////////////***END***//////////////////////////////////////////////


User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How To Read Key Presses Without Disrupting The Flow Of The Program?
21 Sep, 2007 - 08:01 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
To run operations at the same time requires threading. Java by nature is a top down language, running each command to completion.

There is a tutorial on threads available here, written by yours truly including updates from 1lacca
User is offlineProfile CardPM
+Quote Post

alpha02
RE: How To Read Key Presses Without Disrupting The Flow Of The Program?
21 Sep, 2007 - 08:13 PM
Post #3

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
In order to "catch" key strokes, a KeyListener must be used in most of the cases. Read about this. By registering a KeyListener a Thread is not absolutely needed, since the KeyListener.keyPressed and other events will be processed (dispatched) as a new thread. Just use variable in the event to determine what to do with the stroke.

This post has been edited by alpha02: 21 Sep, 2007 - 08:15 PM
User is offlineProfile CardPM
+Quote Post

novamanss2002
RE: How To Read Key Presses Without Disrupting The Flow Of The Program?
21 Sep, 2007 - 08:32 PM
Post #4

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 29


My Contributions


Thanks for the help. Using threads will help with other problems I anticipated!!! icon_up.gif

QUOTE(alpha02 @ 21 Sep, 2007 - 09:13 PM) *

In order to "catch" key strokes, a KeyListener must be used in most of the cases. Read about this. By registering a KeyListener a Thread is not absolutely needed, since the KeyListener.keyPressed and other events will be processed (dispatched) as a new thread. Just use variable in the event to determine what to do with the stroke.


Thanks! This is much easier than threads, though I will still find threads usefull!
User is offlineProfile CardPM
+Quote Post

Programmist
RE: How To Read Key Presses Without Disrupting The Flow Of The Program?
22 Sep, 2007 - 04:30 AM
Post #5

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Wait a minute. Something that no one has mentioned is that Swing does threading differently. All swing events should be handled by the Swing Event Dispatch Thread (created automatically). You heard right. One, single thread handles ALL Swing events. This is because most Swing components are not thread safe. I've found tat this comes as a surprise to many people and it's easy to see why when you look at a beginner Java book. Rarely have I seen them mention this. Maybe it seems like an advanced concept, but I think threads, like object orientation, is such a huge part of Java that it should be taught from the beginning. Anyway, read this Tutorial trail for more info on the proper way to employ concurrency in Swing:
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/

This post has been edited by Programmist: 22 Sep, 2007 - 04:35 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:26AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month