% scanEsc.m
%
% continuously scan keyboard until ESC key pressed

%initialize Cogent 2000 resources
config_keyboard;
config_display(0,1); %640x480 in window-mode
start_cogent;

%read C2K list of key codes
keymap=getkeymap;

%loop until ESC key pressed
clearkeys;  %empty C2K keyboard events buffer
loop = 1;
while (loop > 0)
    %collect any buffered keyboard events then clearkeys
    readkeys;
    %fetch buffered keyboard events (if any)
    [keys,times,nkeys]=getkeydown;
    %start interpretation if any keys pressed
    if(nkeys>0)
        %exit if first key in buffer was an ESC-key event
        if (keys(1) == keymap.Escape)
            loop = 0; %exit scan loop
            disp(' ')
            disp('ESCAPE key was detected')
            disp('Program terminated')
        end
    end
    %allow Windows to service any queued-up events
    %equivalent to 'DoEvents()' in Visual Basic
    drawnow;
end
        
%close windows and reclaim C2K resources
stop_cogent;
