3

I am a beginner in Ada programming. I am trying to make a "2048" game using Ada. I did "2048" in C++, and I used "kbhit()" to detect that is there any keyboard button pressed. I want to know that is there any similar function available like "kbhit()" in Ada, or how to write a function to detect is there any keyboard button pressed, or should I just use the function by interface in C++.

Amar Kumar
  • 2,392
  • 2
  • 25
  • 33
  • 1
    It's impossible to answer this without knowing precisely what `kbhit` does. What does it return if there is a character in the buffer, but no key depressed? What does it return if the buffer is empty and the Ctrl key is pressed? How dioes it work if standard input has been redirected? – Jeffrey R. Carter Apr 05 '20 at 08:55

1 Answers1

4

Yes, you can use any of several overloaded versions of Ada.Text_IO.Get_Immediate. The declarations are (copied from the 2012 edition of the Ada standard):

procedure Get_Immediate(File : in File_Type;
                        Item : out Character);
procedure Get_Immediate(Item : out Character);

Reads the next character, either control or graphic, from the specified File or the default input file. Mode_Error is propagated if the mode of the file is not In_File. End_Error is propagated if at the end of the file. The current column, line and page numbers for the file are not affected.

procedure Get_Immediate(File : in File_Type;
                        Item : out Character;
                        Available : out Boolean);
procedure Get_Immediate(Item : out Character;
                        Available : out Boolean);

If a character, either control or graphic, is available from the specified File or the default input file, then the character is read; Available is True and Item contains the value of this character. If a character is not available, then Available is False and the value of Item is not specified. Mode_Error is propagated if the mode of the file is not In_File. End_Error is propagated if at the end of the file. The current column, line and page numbers for the file are not affected.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    Note that for at least some verions of GPS, if you try to run Get_Immediate from the GPS input console, it will still act as Get because GPS requires a new line or line feed (not sure which) before it passes input into the actual console buffer. – Jere Apr 05 '20 at 20:30