News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

readkey and testkey routines

Started by Randall Hyde, November 17, 2005, 02:09:20 AM

Previous topic - Next topic

Randall Hyde

Hi All,
I'm playing around with a couple of oft-requested functions for the HLA Standard Library - readkey and testkey (read a single character from the console and test to see if a character is available). I've attached the current attempt at this. Feedback would be helpful (I'm particularly concerned about whether testkey works in all cases).

At some point I'll have to do the Linux versions, too. If anyone has any suggestions, let me know.
Cheers,
Randy Hyde



unit consoleUnit;

#includeonce( "excepts.hhf" )
#includeonce( "console.hhf" )
#includeonce( "os.hhf" )
#includeonce( "stdin.hhf" )

#include("stdout.hhf")
?@nodisplay := true;
?@noalignstack := true;

#if( os.linux )

#elseif( os.win32 )

    type
        KEY_EVENT_RECORD:
            record
                bKeyDown            :boolean[4];
                wRepeatCount        :word;
                wVirtualKeyCode     :word;
                wVirtualScanCode    :word;
                AsciiChar           :char[2];
                dwControlKeyState   :dword;

            endrecord;

        INPUT_RECORD:
            record

                EventType   :dword;
                KeyEvent    :KEY_EVENT_RECORD;

            endrecord;

    procedure ReadConsoleInput
    (
            handle          :dword;
        var inputBuffer     :INPUT_RECORD;
            numEvents       :dword;
        var numEventsFound  :dword
    ); @stdcall; @external( "_ReadConsoleInputA@16" );

    procedure PeekConsoleInput
    (
            handle          :dword;
        var inputBuffer     :INPUT_RECORD;
            numEvents       :dword;
        var numEventsFound  :dword
    ); @stdcall; @external( "_PeekConsoleInputA@16" );



    procedure console.readkey;
    var
        numEvents   :dword;
        input       :INPUT_RECORD;
       
    begin readkey;

        push( ecx );
        push( edx );
        repeat

            ReadConsoleInput( stdin.handle(), input, 1, numEvents );
            if( eax = 0 ) then
               
                raise( ex.EndOfFile );
               
            endif;

        until( input.KeyEvent.bKeyDown[0] );
        movzx( (type word input.KeyEvent.AsciiChar), eax );
        pop( edx );
        pop( ecx );
           
    end readkey;



    procedure console.testkey;
    var
        numEvents   :dword;
        input       :INPUT_RECORD;
       
    begin testkey;

        push( ecx );
        push( edx );

        forever

            PeekConsoleInput( stdin.handle(), input, 1, numEvents );
            if( eax = 0 ) then
               
                raise( ex.EndOfFile );
               
            endif;
            breakif( numEvents = 0 || input.KeyEvent.bKeyDown[0] );
            ReadConsoleInput( stdin.handle(), input, 1, numEvents );       

        endfor;
        mov( numEvents, eax );
        pop( edx );
        pop( ecx );
           
    end testkey;

#endif
   
end consoleUnit;

dwzavitz

I finally had a chance to try out your new 'readKey' function
It seems to work ok
A demo program using 'readKey' is over at the HLA site in the files section in the folder 'makeVersesPS'

Thanks Randy ... a welcome addition to the HLA library.   :U

Shalom,

David