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 ?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With