# wait_key

Definition: | integer i = wait_key()
Description: | Return the next key pressed by the user. Do not return until a key is pressed.
pwa/p2js: | Not supported, however since it is very useful at the end of a (desktop) program to stop the console from vanishing, and rather than forcing you to put "if platform()!=JS then" around every call, the (fake) version in p2js.js simply does nothing.
Comments: | You could achieve the same result using get_key() as follows:
        while 1 do
            k = get_key()
            if k!=-1 then
                exit
            end if
        end while
However, this "busy waiting" would tend to slow the system down, whereas wait_key() lets the operating system do other useful work while your program is waiting for the user to press a key. You could also use getc(0), assuming file number 0 was input from the keyboard, except that would not pick up the special codes for function keys, arrow keys etc. Note that wait_key() is intended for console applications only and usually has no place in a gui application. It can only catch keystrokes when the console (assuming you have one) has focus; when the gui has focus then the gui absorbs any keystrokes, as you should expect. One obvious exception is that sometimes you might use wait_key() at the very end of a run, after the gui has closed, to prevent the console and any messages on it from immediately dissappearing. As of 1.0.2 can also be invoked as a procedure, ie a lone wait_key() is treated as {} = wait_key(), no longer needing the explicit discard as it did it prior versions (see pmain.e/Call() and search for T_wait_key).
Example: | demo\key.exw
Implementation: | via :%opWaitKey in builtins\VM\pfileioN.e (an autoinclude) - be warned however it is low-level complicated stuff that you do not need to know.
See Also: | get_key, getc