Locating keypress in Delphi console application

Posted by DelphiLand Team on August 14, 2004

In Reply to: Locating keypress posted by Joey p12386 on August 11, 2004

: Heya... i known this forum is for GUI but i was wondering if i could ask a quick question about console applications...

: How can i capture a key press?? As in... if the user presses the 'J' key then i can mask it with a '*' after adding it to a variable.

: Thanks, Joey ^__^

: P.S. How come my numbers come up after my name and *DClub* like other people??

In a console application, Read(var) waits for the user typing a character on the keyboard. So you can code it as follows:

var
  C: Char;
  ....
begin
  ...
  // Program waits here until a key is pressed
  // the corresponding character is placed in C
  Read(C); 
  // Do something depending on C :
  if C = 'a' then ...
  else if C = 'b' then ...
  else ...
  ...
end;

On the other hand, ReadLn(var) waits for the user typing a string and ending it with "ENTER", so for VAR you use a string variable. Example:

var
  S: string;
  ....
begin
  ...
  // Program waits here until ENTER key
  // is pressed 
  ReadLn(S); 
  // Do something with S :
  ...
end;

Regards,
the DelphiLand Team

PS: From time to time, we process the forum messages and we replace the membership-id's with *DClub*, meaning "DelphiLand Club" :)


Find related articles

Suggested keywords:
  readln,  read,  onkeypress,  keypress,  onkeydown


Related Articles and Replies


[ Delphi Tutorials -- by DelphiLand ]
Compiling Console Applications
Learn Pascal with simple Delphi Console Applications