Subj : Switch/Case with inkey() not working To : Codefenix From : Digital Man Date : Wed Jan 08 2025 11:49:39 Re: Switch/Case with inkey() not working By: Codefenix to All on Wed Jan 08 2025 11:36 am > Having an odd problem with a script I'm writing. > > Consider the following JS code: > > while (!script_exit) { > switch (console.inkey().toUpperCase()) { > case "A": > doThingOne(); > break; > case "B": > doThingTwo(); > break; > case KEY_ESC: > case "Q": > case "\r": > script_exit = true; > break; > default: > log("switch default"); // hits this every time > } > } > > I'm trying to get a single key from the user and let the switch/case block > handle accordingly. However, instead of waiting for input, it immediately > falls to the default conition (as commented above). by default, console.inkey() immediately returns null when there's no pending keyboard input. I recommend passing a non-zero timeout value to inkey() (so you don't peg the CPU while polling for input) and then checking for null (no input) as a possible return value. > If I change inkey to getkey, it works as expected. I'd prefer to use inkey > instead of getkey though, to avoid showing the cursor while waiting for > input. The cursor is displayed by the terminal, no the BBS, so using inkey() or getkey(), you'll still get a cursor (somewhere). -- digital man (rob) This Is Spinal Tap quote #46: "Not an Exit" - we don't want an exit. Well that's true. Norco, CA WX: 62.0øF, 19.0% humidity, 3 mph NW wind, 0.00 inches rain/24hrs --- þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net .