function stop=stop_keyboard(t)

%stop_keyboard  Stop the program by pressing 's'
%
% stop=stop_keyboard(t)
% t: time step
% stop: it returns 1 when 's' was pressed, 0 otherwise.


% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 8: Monte Carlo II
% Autores: Eva Besada-Portas, Sergey M. Plis


hf = findall(0,'Tag','STOP_KEY_CATCHER');
deadkey=char(1);

if t==2    
    % Find the "dbloop key catcher" figure, or create it if it doesn't exist    
    if isempty(hf)
            % Make the figure elusive
        hf = figure('Tag','STOP_KEY_CATCHER', ...
            'Units','Normalized','Position',[1.1 1.1 .2 .1], ...
            'Menubar','None','CurrentCharacter',deadkey, ...
            'IntegerHandle','Off','NumberTitle','Off', ...
            'Name','stop key catcher','Resize','Off', ...
            'HandleVisibility','off');    
    end           
end

if isempty(hf)
    stop=1;
    return;
end

% Make the "key catcher" figure active so we can assuredly catch keys
figure(hf)
drawnow

% If a specified key was pressed, make a breakpoint so it stops on the next
% execution, or else clear that potential breakpoint
cc = get(hf,'CurrentCharacter');
if  strcmp(cc,'s') || strcmp(cc,'S')
    set(hf,'CurrentCharacter',deadkey);
    stop=1;
else
    stop=0;    
end