• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Any key problem.

In REPEAT block, how to  wait the user press ANY KEY to continue every loop?
0
dech
Asked:
dech
  • 9
  • 6
  • 5
  • +3
1 Solution
 
BatalfCommented:
Could this help you :

Procedure.....

REPEAT
    {code}
     readkey; {wait for a key-input}
UNTIL .......


0
 
dechAuthor Commented:
To Batalf:

1. In my PC it can not work :-( Instesd or readkey I must use any := readkey;
2. Even use any := readkey, it still got problem:
If we press a key other than the printable keys, e.g. the arrow key, the program continued to run without stopping in the later repeats!

Thank you.
0
 
My name is MudSystems EngineerCommented:
>>In my PC it can not work...

Use the X+ Directive, to discard the values returned by the functions...



Repeat
  If KeyPressed Then
    Begin
      Key:=ReadKey;   {"Printable" Key}
      If Key = #0 Then
        Key:=ReadKey  {Especial Keys}
    End;
Until Something;
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
vikiingCommented:
As Whatboy showed you, special keys (Alt and F's) return TWO bytes. That's the reason of that code: if first byte equals 0, then catch the next one.

The problem with Whatboy's code is that if user presses no key, the cycle keeps going on (KeyPressed function returns TRUE if there is, at least, one character at keyboard buffer, but it DOES NOT stop program execution as ReadKey does). In that case, that code will run, no matter user presses a key or not.

If you want to do a cycle which executes one round each time any key is pressed, just do:

Repeat
      Key:=ReadKey;
      If Key=#0 Then Key:=ReadKey;
      < code to be executed on a key press >
Until < condition be true >;
0
 
My name is MudSystems EngineerCommented:
Oh, sh... miss that one... oh well guess i didn't read well the Q...
0
 
Fatman121898Commented:
Hi dech,
You may try this:

....
var Key:char;
....

repeat

  <your code here>

  {next loop will be executed until key has been pressed}
  repeat until KeyPressed;
  Key:=ReadKey;
  if Key=#0 then Key:=ReadKey;
until <your loop exit condition here>

As you can see, it is necessary to call ReadKey after realizing that KeyPressed is True. This will clear internal flag and prepare KeyPressed for next key pressing.

Regards.
Jo.
0
 
My name is MudSystems EngineerCommented:
Bravo!!! WOOOOWWWWwwwww Mr. FatAss. You sure got the answer... :P
0
 
vikiingCommented:
>>repeat until KeyPressed;

That sequence is totally unuseful; more than that, its use must be discouraged, 'cause the following instruction is which does the true job.

While Repat until KeyPressed simply eats CPU time just for NOTHING, Key:=ReadKey waits for an interrupt (keyboard's) to let program execute.

ReadKey function works this way:
a) If there's a char at kbd buffer, pick it up
b) If not, wait for it.

Step b) makes your cycle unuseless; program really stops (without your intervention) until you press a key, without CPU time consumption (it waits for an interrupt to occur).

Now, the cycle Repat until KeyPressed does this:
a) Is there a char at keyboard?
b) Yes; return TRUE. Then exit from cycle.
c) No; return FALSE. Then, repeat from a)

These are the differences between the distincts options you have to perform an I/O operation on any device: two of them are "programmed I/O" (Repat until KeyPressed) and "wait for interrupt" (Key:=ReadKey).

While prog.I/O wastes all CPU process time (the only job CPU does is to check the existence of a char in keyboard buffer) and does not let the processor to perform another activity, wait for int. frees the CPU to do any other job (perhaps nothing); when character arrives, int. calls CPU attention, but only then.

Watch for the difference:

- Repeat until KeyPressed
  You are expecting someone calls you at phone, and your phone does not have a bell (¿what do you need it for?. Let's assume busy sign does not exist): Then, you're seated in a chair, and:
a) Pick up the phone and hear
b) Anybody there?
c) If not, hang up, wait a second and go to a)
d) If yes, take the call.

You (the CPU) don't have a single second to go to bathroom; all your life is dedicated entirely to wait for the coming call.


On the other hand, now your phone has that marvelous bell. You're wondering around your house, going back an forth, and playing with your chick in bed. Just only when interrupt comes (the bell sounds; ReadKey returns the char to be stored at Key), you pick up the call. Meantime, you were free, and not chained to the seat in front of the phone.
0
 
BatalfCommented:
Someone are trying to steal som points here :-(
0
 
vikiingCommented:
>>Someone are trying to steal som points here

Worst af all, with wrong suggestions... :(
0
 
Fatman121898Commented:
Should I remind the question?

>>In REPEAT block, how to  wait the user press ANY KEY to continue every loop?

More of that, I heard nothing about multytasking or multythreading (in DOS -ha,ha,ha).

I didn't understood what CPU has to do if there is a loop wich has to be stopped every time and wait until key is pressed? Maybe somebody thinks that CPU has to go to toilet or smt.?
:-)))

FAT
0
 
vikiingCommented:
>>I heard nothing about multytasking or multythreading

Shut your smile, dude; I supose you (should) know you can open several DOS instances under Windoze, and run a different process in each one.

Therefore, ¿why to eat all CPU time if you can let it do another job provided that your process needs no attention?. But sure, you must know about multiuser systems to have a minimal idea about this.


>>I didn't understood what CPU has to do

Exactly NOTHING.
0
 
My name is MudSystems EngineerCommented:
Hmmmmm... i might add that the fact wasn't your code, mr. Fatty, is the act you did... you see, the code you gave is in essence same as mine, but i didn't post as answer, but you did, now that's greed, and greed mister is a capital offence, so here by declare you to go to hell, with all my sympathy of my part. Now that might or not be quit right, the sympathy stuff, since i don't know you, i might did something wrong by stating that, but there are rules here, and since you don't know them, then you did something wrong... :P
0
 
Fatman121898Commented:
Okay, Okay,

I overdid it all.
I was not right, so suppose the "answer" I gave will be rejected.
Sorry for loosing your time.
Keep on listening...

FAT.
0
 
My name is MudSystems EngineerCommented:
Hmmmm... i'm amaze you understood... sometimes i over talk to the point i don't even know what the heck am saying, oh well...
0
 
vikiingCommented:
Knock knock... Hey, Dech !!! are you stil there?
0
 
dechAuthor Commented:
You guys have been arguing over this every time I look that you have left me speech less.
0
 
dechAuthor Commented:
No point in giving someone elses answer Fatman!
0
 
dechAuthor Commented:
Post your comment as an answer vikiing and I will give you the points.
0
 
vikiingCommented:
Repeat
      Key:=ReadKey;
      If Key=#0 Then Key:=ReadKey;
      < code to be executed on a key press > 
Until < condition be true >;
 
 

If you think this is the right answer, thank for the points, Dech !!!
0
 
VIBESPROCommented:
here you go Dech!

var
  Key: Ch; { or String }

Repeat
  Key:= ReadKey;
  if Key = #0 then Key:= ReadKey;
  if KeyPressed then writeln(Key);
Until Key = #27;

0
 
vikiingCommented:
>>Repeat
>>  Key:= ReadKey;
>>  if Key = #0 then Key:= ReadKey;   [1]
>>  if KeyPressed then writeln(Key);   [2]
>>Until Key = #27;

Writeln at [2] will never be executed, because, as Readkey was recently done [1], there is no key in the keyboard buffer, making KeyPressed to return FALSE always. It will work only in the case in which there be a very heavy code between instructions [1] and [2].

Besides that, ¿what is the difference of your code and what I've previously suggested on Nov 14th? ¿Do you really want to get points so easily?

Please, Dech; give us a response...

0
 
dechAuthor Commented:
Why wont you post your comment as answer vikiing?
0
 
vikiingCommented:
Thanks for the points, Dech, but, first of all, you must reject Vibes' answer so I can post mine's...
0
 
VIBESPROCommented:
sorry about that Dech.  I work with some heavy code that's y I wrote it that way..  but here you go:

here you go Dech!

var
  Key: Ch; { or String }

Repeat
  Key:= ReadKey;
  if Key = #0 then Key:= ReadKey;
  writeln(Key);
Until Key = #27;

here's a nice function for you

function CheckKey(Key: Char): Boolean;
var
  Ch: Char;
begin
CheckKey:= FALSE;
If KeyPressed then
 Ch:= UpCase(ReadKey);
 if Ch = Key then CheckKey:= TRUE;
end;


sorry Vikiing but I've only answered one question, ever!  so I would like to answer some more..

but Dech!  if u want to give him the points go ahead.  it's your question :(
0
 
vikiingCommented:
It's time to pay, Dech...
0
 
dechAuthor Commented:
sorry vib, vikiing was there first
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 9
  • 6
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now