0

In my old BORLAND PASCAL DOS, I often used keyboard commands in the form of an infinite loop, interrupted when reading some keys:

**Repeat

Write(‘Press the desired button 1/2’); {each selection has a specific command associated with it}. Sk:=readkey;

Until (sk=’1’ or sk=’2’); Case sk of ‘1’:begin execute first command End; ‘2’:begin execute second command End;end;**

Important for me is that I order the selection by directly pressing a chosen key, without positioning the cursor in a window on the screen, and then pressing ENTER. For example, a Mathematica equivalent uses the While[] and InputString[] commands to perform the above operation:

out = True;While[out == True, 
 q = InputString["Press 1 for 1st command; Press 2 for second command; Press e for exit"];
 Which[q == "1", Print["Now I execute 1st command"], q == "2",Print["Now I execute second command"], q == "e", Print["exit"]; out = False; Break[]]]

The disadvantage is that I have to press the selected number, and then press enter; temporally, it takes longer, which I dislike. Another option offered by Mr. Wizard in Abort when key is pressed :

DynamicModule[{stop = False},EventHandler[Row[{"(inactive area) ", 
      Dynamic[t]}], {{"KeyDown", "x"} :> (stop = True)}] // Print;t = 0; While[t < 11! && ! stop, t++]];

has the disadvantage that before pressing the letter x that triggers the stop event, the operator must spend some time to manually position the cursor in the inactive window; the same impediments can be found in procedures based on JLink or NETLink. Finally, other methods such as those based on modifying the "KeyEventTranslations.tr" file or resorting to Autohotkey are laborious.

Is there a simpler option that quickly executes the operations described in Pascal: to identify the key pressed without positioning the cursor in a window and without pressing the ENTER key?

rosu_constantin
  • 121
  • 1
  • 6

0 Answers0