The following piece of code demonstrates how a Perl application that was started from the online -tki command line can be interrupted, either by pressing the space bar or by pressing the Stop button at the toplevel widget.
Notice that the function Spectra::key() works only, if the xterm that started online -tki is brought to the front.
Notice also that the function Util::refresh() is useful in general. It should be called repeatedly from long running Perl programs in order to prevent widgets to be 'frozen'. If Util::refresh() is called, the motor positions are updated and control is transfered to the PerlTk manager which handles button events and invokes callback functions, if necessary.
#!/usr/bin/env perl
use Spectra;
while( 1)
{
my $k = Spectra::key();
#
# if we run online -tki, ...
#
if( Spectra::flag( "perltk"))
{
#
# ... we call the refresh routine to look whether
# the user pressed the Stop button
#
Util::refresh();
#
# Util::refresh() take some time, if many motor positions are updated.
# The following call is fast because it updates the Stop button
# of the toplevel widget only.
#
Util::update_toplevel_stop_button();
}
print " key $k, stop-flag $Spectra::SYM{ flag_tki_stop} \n";
if( $k == 32)
{
last;
}
if( $Spectra::SYM{ flag_tki_stop})
{
last;
}
Spectra::wait( 0.2);
}
#
# clear the stop button flag
#
$Spectra::SYM{ flag_tki_stop} = 0;
print "test script DONE \n";
1;