: :
This message was edited by hl8 at 2005-1-13 1:46:43
: : : : :
This message was edited by hl8 at 2005-1-12 5:22:24
: : : : :
: : : : : I am reading a string or an integer[involve 2 digit~!], can the user quit by pressing ESC?
: : : : : Can I use readkey?
: : : : :
: : : : :
: : : : :
: : : : When I use readkey, can I display the input at the same time?
: : : :
: : : The escape-key is represented by the #27 char. You can use ReadKey and display the input at the same time. Here is a function, which you can use:
: : :
: : : function ReadText(const X, Y: integer): string;
: : : { This is a blocking user entry function based on readkey
: : : Handles the following key-strokes:
: : : - Backspace: removes last character
: : : - Enter: Sets the result to true
: : : - ASCII Code 32 (space) and upwards are valid characters
: : : - Removes extended (multi-byte) keypresses
: : : Other features:
: : : - positions the input field at X, Y relative to top-left of the screen
: : : }
: : : var
: : : UserInput: string;
: : : begin
: : : UserText := '';
: : : repeat
: : : ch := ReadKey; { Get the ASCII key code }
: : : case ch of
: : : #0..#7: ;
: : : #8: Delete(UserInput, Length(UserInput), 1);
: : : #9..#31: ;
: : : else UserInput := UserInput + ch;
: : : end;
: : : while KeyPressed do ReadKey; { Remove extended }
: : : GotoXY(X, Y); { Show the result at X, Y }
: : : write(UserInput);
: : : until ch = #27;
: : : ReadText := UserInput;
: : : end;
: : :
: : : This function ends if the user presses the ENTER key. If you need the non-blocking variant of this function check out this message: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=16&MsgID=287056&Setting=A0003F2002
: : :
: : :
: : Thank you very much!! Your code is very useful~! ^o^
: :
: : By the way, I also want to ask about the Delay function. Is the time delayed various using different CPU?
: :
: :
: :
:
while KeyPressed do ReadKey; { Remove extended }
: What is the use of this line??
: I type this and it said "Invaild variable reference"...
: Can I delete this line?
:
Sevral keys like function keys use 2-bytes to identify themselves. These bytes must be removed, otherwise the user will appear to have pressed 2 keys, while only pressing 1. The line above removes those keys. To solve tghis problem, change it to this:
while KeyPressed do ch := ReadKey; { Remove extended }
and add the ch variable as a local char variable.