Software Archive
Read-only legacy content
17061 Discussions

Escape key detection in console mode

rarussell
Beginner
675 Views
I have been using Lahey EM32 compiler, am considering moving to VF5.6 to make native Win app. Program is basically console mode, but during execution needs to check periodically if Escape key has been pressed, signalling user wants to interact. On browsing on line doc, it appears that I can use PEEKCHARQQ and related rtns, or use a thread if not in console mode, but I wonder if there isn't a canned routine that I can use to answer simple question "Escape pressed - yes or no". Examples I've seen in browsing the forum for "escape" entries seem cumbersome. I would think that Escape key querying is common enough to warrant a canned routine.
0 Kudos
5 Replies
Steven_L_Intel1
Employee
675 Views
There is not a specific routine for checking for the Escape key - this is actually a rather uncommon request - I've never seen it before. PEEKCHARQQ can do what you want very simply - just look at the character and test it.

Steve
0 Kudos
rarussell
Beginner
675 Views
Thanks. One thing: does inquiring about a key having been pressed clear it from the buffer so the next read from the console doesn't get the character, or are just certain keys left for a legitimate read? As to need for looking at escape, I would think that any lengthy calculation might be a candidate for interrupting. If status messages are displayed as to progress of calculations, convergence, etc., a curious user might well want to say "hold on, there; I want to see what you have so far, perhaps change things." In chemical process simulation, the need is essential. Not all number crunching is for weather prediction.
0 Kudos
Steven_L_Intel1
Employee
675 Views
No, peeking doesn't remove the character from the keyboard buffer.

I can understand having a key to interrupt calculations, but what that key is might vary by application. Escape tends to be interpreted by Windows itself so is not a good choice here.

Steve
0 Kudos
NH_Veldhuijzen
Beginner
675 Views
I want to interrupt programs, too. I use the SIGNAL function for the signal SIGINT, which is raised by using Ctrl-C.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
675 Views
You can use PEEKCHARQQ but in conjunction with GETCHARQQ (which does remove the character from the buffer). Quote from docs:

If there is no keystroke waiting in the keyboard buffer, GETCHARQQ waits until there is one, and then returns it. Compare this to the function PEEKCHARQQ, which returns .TRUE. if there is a character waiting in the keyboard buffer and .FALSE. if not. You can use PEEKCHARQQ to determine if GETCHARQQ should be called. This can prevent a program from hanging while GETCHARQQ waits for a keystroke that isn't there.

Also, you can use a thread in a console mode, but I think that would be far more complicated than the problem justifies.

Jugolsav
0 Kudos
Reply