Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On keydown in Ada

Tags:

ada

I would need to execute some function when the user presses the "escape" key in my Ada program. I know we can retrieve what he enters thanks to get_line but it's not exactly what I need to do. Indeed, I don't want to stop the program until he enters "escape".

First, is it possible ?

like image 626
user2302725 Avatar asked Oct 23 '13 11:10

user2302725


1 Answers

It is possible to get the characters without the need to press enter using :

Ada.Text_IO.Get_Immediate (Answer)

with Answer, a Character.

And the escape key is ASCII 27, so you can check whether Character'Pos(Answer) equals 27 or not. Also, as suggested in the comments, you can also compare Answer to Ada.Characters.Latin_1.ESC.

Here is an example of a program that display "Yeah!!!1!!1!" in a loop until the key ESC is pressed.

with Ada.Characters.Latin_1;
with Ada.Text_IO;

procedure Test is
    Finished : Boolean := False;

    task Escape_Task;

    task body Escape_Task is
        Answer : Character;
    begin
        loop
            Ada.Text_IO.Get_Immediate(Answer);
            if Answer = Ada.Characters.Latin_1.ESC then
                Finished := True;
                exit;
            end if;
        end loop;
    end Escape_Task;

begin
    while not finished loop
        Ada.Text_IO.Put_Line("Yeahh!!!1!!1!");
    end loop;
end Test;
like image 174
Maxime Chéramy Avatar answered Oct 01 '22 04:10

Maxime Chéramy