Dear everyone,
            I can't find any function that similiar to readkey in Pascal or getch() in C in hla standard library. Is there somebody know the source code for these functions.

thanks.
Posted on 2005-10-13 23:20:30 by eru_dio
If you have a look in the MASM32 library reference, that comes with the masm32 package,or the high level help,that
also comes with the package, you might find something in there to help you.
Posted on 2005-10-15 06:28:04 by rags

Dear everyone,
             I can't find any function that similiar to readkey in Pascal or getch() in C in hla standard library. Is there somebody know the source code for these functions.

thanks.


From the win32console library in the HLA examples code:


unit consoleUnit;
#include( "stdin.hhf" );
#include( "win32cons.hhf" );





// peekInput-
//
// Checks for an input event (mouse event, keyboard event, resize event).
// Returns zero in EAX if no events or error, returns one if it successfully
// peeked at an event.

procedure win32cons.peekInput( var input:win.INPUT_RECORD ); @nodisplay;
var
numEvents: dword;

begin peekInput;

push( ebx );
push( ecx );
push( edx );
push( esi );
push( edi );

win.PeekConsoleInput( numEvents, 1, input, stdin.handle() );
if( eax ) then

mov( numEvents, eax );

endif;
pop( edi );
pop( esi );
pop( edx );
pop( ecx );
pop( ebx );

end peekInput;




// readInput-
//
// Waits for an input event (mouse event, keyboard event, resize event).

procedure win32cons.readInput( var input:win.INPUT_RECORD ); @nodisplay;
var
numEvents: dword;

begin readInput;

push( ebx );
push( ecx );
push( edx );
push( esi );
push( edi );

win.ReadConsoleInput( numEvents, 1, input, stdin.handle() );
if( eax ) then

mov( numEvents, eax );

endif;
pop( edi );
pop( esi );
pop( edx );
pop( ecx );
pop( ebx );

end readInput;


// flushInput-
//
// Flushes all the console input events from the internal buffer.

procedure win32cons.flushInput; @nodisplay; @noframe;
begin flushInput;

pushad();
win.FlushConsoleInputBuffer( stdin.handle() );
popad();
ret();

end flushInput;




end consoleUnit;


Cheers,
Randy Hyde
Posted on 2005-10-15 15:11:39 by rhyde
Thank you for the helps. I am not familiar with win32 API but I'm trying hard here. With the functions that Randy gave, I

made (not-perfect) readkey-like program. But, when I made it with #include(w.hhf), there is

a little problem. Also I have a few technical question. First, here is the program's code with including win32cons.hhf :

// this program will read a keyboard input character like readkey
program ha;
#include("win32cons.hhf")
#include("stdlib.hhf")
static
c: win.INPUT_RECORD;

begin ha;
// I want to make sure that when I press ctrl+c, the process won't break out
win.SetConsoleMode( win.ENABLE_ECHO_INPUT + win.ENABLE_LINE_INPUT, stdin.handle() );

// It will keep readInput until we hit keyboard and as long as the key is not shift/ctrl/alt/capslock/numslock/

scrolllock.
stdout.put("Input char = ");
repeat
win32cons.readInput(c);
until ( c.Event.KeyEvent.bKeyDown = 1  && !(c.Event.KeyEvent.wVirtualKeyCode = $10 || c.Event.KeyEvent.

wVirtualKeyCode = $11 || c.Event.KeyEvent.wVirtualKeyCode = $12 || c.Event.KeyEvent.wVirtualKeyCode = $14 || c.Event.

KeyEvent.wVirtualKeyCode = $90 || c.Event.KeyEvent.wVirtualKeyCode = $91) );

// Get input char into al and print the input char to console
mov(c.Event.KeyEvent.AsciiChar,al);
stdout.put((type char al),nl);
end ha;

The first question:
- What is the meaning of ENABLE_LINE_INPUT, ENABLE_ECHO_INPUT, ENABLE_WINDOW_INPUT in the inputmode of SetConsoleMode ?

Next, the code with including w.hhf :

program hb;
#include("w.hhf")
#include("stdlib.hhf")

static
c: w.INPUT_RECORD;

procedure readInput2( var input:w.INPUT_RECORD ); @nodisplay;
var
  numEvents:  dword;
 
begin readInput2;

  push( ebx );
  push( ecx );
  push( edx );
  push( esi );
  push( edi );
 
  w.ReadConsoleInput( stdin.handle(),input,1,numEvents );
 
  if( eax==1 ) then 
      mov( numEvents, eax );     
  endif;
 
  pop( edi );
  pop( esi );
  pop( edx );
  pop( ecx );
  pop( ebx );
     
end readInput2;

begin hb;
w.SetConsoleMode( stdin.handle(), w.ENABLE_ECHO_INPUT + w.ENABLE_LINE_INPUT );
stdout.put("Input char = ");
repeat
readInput2(c);
until ( c.Event.KeyEvent.bKeyDown = 1  && !(c.Event.KeyEvent.wVirtualKeyCode = $10 || c.Event.KeyEvent.

wVirtualKeyCode = $11 || c.Event.KeyEvent.wVirtualKeyCode = $12 || c.Event.KeyEvent.wVirtualKeyCode = $14 || c.Event.

KeyEvent.wVirtualKeyCode = $90 || c.Event.KeyEvent.wVirtualKeyCode = $91) );

mov(c.Event.KeyEvent._uChar.AsciiChar,al);
stdout.put( (type char al),nl);
end hb;

The resulting program will keep running forever. Then I tried to investigate it. I found out there is something weird about

w.INPUT_RECORD, the 'c.Event.KeyEvent.bKeyDown' was not returning the boolean value, instead it returned the numeric values

like 1_D512,0_D512, 1_000. Also the 'c.Event.KeyEvent._uChar.AsciiChar' is supposed to be returning the AsciiChar, but it

returned the virtual key scan-code instead.

When I used this after statement until(...):
stdout.put
(
nl
"Event=", c.EventType, nl,
"KeyDown=", c.Event.KeyEvent.bKeyDown, nl,
"Repeat =", c.Event.KeyEvent.wRepeatCount, nl
"KeyCode=", c.Event.KeyEvent.wVirtualKeyCode, nl
"ScanCode=", c.Event.KeyEvent.wVirtualScanCode, nl,
"ASCIIcode=", (type byte c.Event.KeyEvent._uChar.AsciiChar), nl,
"CtrlState=", c.Event.KeyEvent.dwControlKeyState, nl,
nl
);
The result was:
event=0001
keydown=0000_D512
repeat=0000
keycode=0001
scancode=000D
ascii=1C
ctrlstate=0020_000D

I checked the KEY_EVENT_RECORD in w.hhf, then I found out that the field's type of bKeyDown is not same to the same field of

KEY_EVENT_RECORD in win32cons.hhf. The type is dword, meanwhile win32cons.hhf's uses boolean[4], and also it doesn't have a

padding before it. I want to know if this a bug in w.hhf ?

Why we must put a padding before the bKeyDown ?

I wonder which is right type of dwControlKeyState's field ?. Is it dword or word. The win32 API manual said it's dword but

the win32cons.hhf says it's word.

Once again thanks for the help (sorry for the bad english).
Posted on 2005-10-27 22:09:36 by eru_dio