Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi equivalent of kbhit or keypress

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I'm porting some code from C++ to Delphi Pascal and got stuck when I couldn't find the Delphi equivalent of kbhit on a console application. I've been scouring the net for the last 2 days and can't seem to find anything. Guess nobody writes console apps nowadays.

Anyway, what I've come up with is a crude 2 thread jobby. One thread resets a variable and sits on a readln. When the user hits return, it sets the variable. The other thread polls the variable. If it is set, then the user has hit return and something else can happen. It is not ideal but it works.

Is there a better way like a kbhit routine?

Alternatively, is it OK practice to pinch the one in the MS DLL (have to find out which DLL first). I'm a bit weary about this one as it is a completely different I/O system and may cause problems (like mixing stdio and iostream in C++).
 
faq102-7109

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thanks - tried it on Dephi 5. I'm now hitting a different problem.
[Error] dlltest.dpr(20): Types of actual and formal var parameters must be identical
The code is as follows
Code:
uses
  SysUtils, Windows;

var
   pp: integer;
   hConsoleInput: THandle;

procedure KeyInit;
   begin
      hConsoleInput := TTextRec (Input).Handle;
   end;

function  KeyPressed: boolean;
   var
      numberOfEvents: integer;
   begin
      numberOfEvents := 0;
      { Getting error 20 on this line }
      GetNumberOfConsoleInputEvents (hConsoleInput, numberOfEvents);
      result := numberOfEvents > 0;
   end;
...
I don't really know what to declare hConsoleInput as. THandle/integer as declared in the CRT program doesn't seem to work. I tried THandleStream but then I don't know how to get the value. Any ideas?
 
I'm on Delphi 3. You have to define a lot of things as "Cardinal" beyond a certain point. Try defining the values on that function as such.

Beyond that, and FWIW, here's what you need if you just strip the unit down to that function alone:

Code:
unit crt;

interface

uses windows,messages;

function KeyPressed:boolean;

var
  HConsoleInput:thandle;

implementation
  uses sysutils;

function keypressed:boolean;
  { handles ANY events - might need restrict it to only keyboard }
  var
    NumberOfEvents:integer;
  begin
    GetNumberOfConsoleInputEvents(hconsoleinput,NumberOfEvents);
    result := NumberOfEvents > 0;
  end;

procedure init;
var
  lpmode: integer;
begin
  // checks/sets so mouse input does not work
  SetActiveWindow(0);
  GetConsoleMode(HConsoleInput, lpMode);
  if (lpMode and ENABLE_MOUSE_INPUT) = ENABLE_MOUSE_INPUT then
    SetConsoleMode(HConsoleInput, lpMode xor ENABLE_MOUSE_INPUT);
  // get input file handle
  reset(input);
  HConsoleInput := TTextRec(Input).Handle;
end;

initialization
  init;
end.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
The number of events is now a DWORD. That is what it was moaning about: not the handle. Amazing what a good nights sleep does for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top