• Switch/Case with inkey() not working

    From Codefenix@VERT/CONCHAOS to All on Wed Jan 8 11:36:44 2025
    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).

    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.

    I've used inkey in other scripts with no issue up to now, so I'm puzzled why it isn't working as expected here. What am I missing?

    |15 þ ù ú codefenix ú ù ú ConstructiveChaos BBS ú ú ù þ þ
    |08 þ þ ù (https/telnet/ssh)://conchaos.synchro.net ú ù þ
    |07

    ...I got some powdered water, but I don't know what to add.
    ---
    þ Synchronet þ -=[ ConstructiveChaos BBS | conchaos.synchro.net ]=-
  • From Digital Man@VERT to Codefenix on Wed Jan 8 11:49:39 2025
    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