Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Key Listener (Press Any Key To Continue) In Java
New Posts  All Forums:Forum Nav:

Key Listener (Press Any Key To Continue) In Java

post #1 of 6
Thread Starter 
Hi guys, I am trying to make a simple "Press Any Key to Continue" in Java. Basically, I have a timer, and I want to run my timer.cancel(); when the key is pressed. So far I have just been using an if() statement. So if (int stop != -1), I am pretty new to this all, so I am sure there is a better way to do it. (This program is the start of a program that is supposed to sort data entered into it into a table for a UAV drone contest in the spring.).
Code:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;


public class DataSort extends Run {
        
        public static void main(String[] args) throws IOException {

                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                Scanner console = new Scanner(System.in);
                Timer timer = new Timer("Timer1");
                Run ht = new Run();

                System.out.println("Welcome to Data Sort! This Program is designed to sort information about targets discoverd by UAV and place the data in a table.\n");
                System.out.println("****************************************************\n");
                System.out.print("For help, press any key. To continue, please wait. ");
                timer.schedule(ht, 4000);

                int stop = in.read();

                        if(stop != -1){
                                timer.cancel();
                        }

        }
}

class Run extends TimerTask {

        public void run(){

                ArrayList<String> color = new ArrayList<String>();
                ArrayList<String> ancolor = new ArrayList<String>();
                ArrayList<String> geoloc = new ArrayList<String>();
                ArrayList<String> relat = new ArrayList<String>();
                
                System.out.println("Hi");
                Scanner console = new Scanner(System.in);

                String help = console.nextLine();
        }
}
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #2 of 6
You want to run a timer simutaneously with waiting for user input?

You have to thread or poll.



Just a note... "\n" is not portable. Use System.getProperty("line.separator") instead for best pratices.
Edited by DuckieHo - 3/29/13 at 8:27am
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #3 of 6
Thread Starter 
Ok, will do!

I think I'll just wait and use what I have now, considering I don't have the knowledge to thread my program. I don't want to go jumping ahead and doing things I don't full understand yet.

Thanks!
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #4 of 6
static final String EOL = System.getProperty("line.separator");
Now just use "EOL" variable where you need another line.



It looks like Timer is threaded already: http://docs.oracle.com/javase/6/docs/api/javax/swing/Timer.html

Instantiate an ActionListener with a task.
Instantiate the Timer with a delay and the ActionListener.
Start the timer.

Your console I/O should run seperately from the Timer.
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #5 of 6
Thread Starter 
Sounds good. Yea I have been using the line.separator with great success now.

What do you mean by instantiate ActionListener with a task. As I said I am pretty new to this, so forgive my basic questions redface.gif.

EDIT: Also, just to be clear, ActionListener will allow me to detect command line input without pressing Enter? I always thought it was for GUIs.
Edited by snelan - 3/29/13 at 5:42pm
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #6 of 6
Quote:
Originally Posted by snelan View Post

Sounds good. Yea I have been using the line.separator with great success now.

What do you mean by instantiate ActionListener with a task. As I said I am pretty new to this, so forgive my basic questions redface.gif.

EDIT: Also, just to be clear, ActionListener will allow me to detect command line input without pressing Enter? I always thought it was for GUIs.

I've only used action listeners for GUIs I don't see how they would work for console use are they are tied to a button.
Edited by Bobicon - 3/30/13 at 6:04pm
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Key Listener (Press Any Key To Continue) In Java