Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

KeyListener and MouseListener witihout Interface

807589Jul 31 2008 — edited Jul 31 2008
Hi,

I need a program without an interface to listen for key and mouse input.
(I'm deploying a kiosk-pc and need to restart my browser after 5 minutes of inactivity).

Because there is no interface, there's nothing to give focus to, does anybode has any idea how to work around this problem?

Code:
public class CheckInput implements KeyListener, MouseListener{

private Teller teller;
private int iReset;

public CheckInput(){
iReset=0;
teller = new Teller();
teller.start();
//this.setFocusable(true);
//this.getF
}

//----------------------------------Input Listeners
//--Keyboard
@Override
public void keyTyped(KeyEvent e){
System.out.println("KEYBOARD INPUT");
controlTeller(1);
}
@Override
public void keyReleased(KeyEvent e){
System.out.println("KEYBOARD INPUT");
controlTeller(1);
}
@Override
public void keyPressed(KeyEvent e){
System.out.println("KEYBOARD INPUT");
controlTeller(1);
}
//--Mouse
@Override
public void mouseClicked(MouseEvent e){
System.out.println("MOUSE INPUT");
controlTeller(1);
}
@Override
public void mouseExited(MouseEvent e){
System.out.println("MOUSE INPUT");
controlTeller(1);
}
@Override
public void mouseEntered(MouseEvent e){
System.out.println("MOUSE INPUT");
controlTeller(1);
}
@Override
public void mouseReleased(MouseEvent e){
System.out.println("MOUSE INPUT");
controlTeller(1);
}
@Override
public void mousePressed(MouseEvent e){
System.out.println("MOUSE INPUT");
controlTeller(1);
}

private void close(){
//this method closes my browser
ProcessBuilder pb = new ProcessBuilder("close.bat");
try{
Process p = pb.start();
}catch(IOException e){System.out.println(e);}
controlTeller(0);
}

private void controlTeller(int iAction){
if(iAction==0){
teller.run();
}else{
teller.reset();
}
}

class Teller extends Thread{
public int iMin;

public Teller(){
iMin=5;
}

@Override
public void run(){
while(iMin >= 0){//loop to count down iMin minutes
if(iMin > 0){
System.out.println(String.valueOf(iMin));
sleeping(60000);
if(iReset==1){
reset();
}else{
iMin--;
}
}else{
System.out.println(String.valueOf(iMin));
close();
}
}
}

private void sleeping(int time){
try{Thread.sleep(time);}
catch(InterruptedException e){System.out.println(e);}
}

public void reset(){
iMin=5;//on input, reset countdown loop
iReset=0;
System.out.println("RESET: "+String.valueOf(iMin));
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 28 2008
Added on Jul 31 2008
1 comment
116 views