Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java code from example showing use of Timer and Robot to click mouse to eventually stop a running Impress Slide Show. Works on Windows 10 and Linux Mint 19.
//
// a timer task to tick down the time
// up the tics and click the mouse
//
private class MyTimerTask extends TimerTask {
@Override
public void run()
{
nTimerTics++;
tfTimerTics.setText(String.valueOf(nTimerTics));
// try to click the mouse here
try {
Robot bot = new Robot();
int mask = InputEvent.BUTTON1_DOWN_MASK;
// don't move the mouse in case the user wants to click on Stop Show or
// something else. It will be fine, the show will stop on the click
// if it's at the end.
//bot.mouseMove(100, 100);
bot.mousePress(mask);
bot.mouseRelease(mask);
try {
// hang for a bit before release
Thread.sleep(100);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
System.out.println("MyTimerTask interrupted");
}
bot.mouseRelease(mask);
}
catch (Exception e ) {
System.out.println("MyTimerTask Robot exception");
}
// if the show is running, watch for it to end in a strange way
if ( bShowRunning ) {
try {
// so, rather than a wait, we check for exit value
// and if that tosses an exception, the process is
// still running. Ooooooookkkkkaaaaaayyyyyy No Problem
int exitValue = pShowProcess.exitValue();
// guess we don't do this to get rid of the not referened warning
//(void)exitValue;
// we don't care what the exit value was
exitValue = 0;
// but if we get here, then the show stopped, so
// if we stop it now, it won't need to wait, it will be fine
// we think.
stopShow();
} catch (Exception ex) {
// Process is still running. So just keep going until
// mouse clicks or something else stops the show
}
}
}
}
public void startTimer( long msecsPerTic ) {
try {
if ( bTimerRunning ) {
System.out.println("startTimer already running" );
return;
}
aTimer = new Timer();
timerTask = new MyTimerTask();
aTimer.schedule(timerTask, msecsPerTic, msecsPerTic);
bTimerRunning = true;
System.out.println("startTimer started for "+String.valueOf(msecsPerTic) );
} catch (Exception ex)
{
// just ignore any exceptions
System.out.println("startTimer exception" );
}
}
public void stopTimer() {
try {
if ( !bTimerRunning ) {
System.out.println("stopTimer not running" );
return;
}
timerTask.cancel();
bTimerRunning = false;
System.out.println("stopTimer cancelled" );
} catch (Exception ex) {
System.out.println("stopTimer exception" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment