• DOS keyboard polling in Windows

    From Trifle Menot@triflemenot@protocol.invalid to alt.bbs.pcboard on Fri Apr 3 21:28:24 2015
    From Newsgroup: alt.bbs.pcboard

    DOS keyboard polling in Windows can be tricky. How to avoid hogging the
    CPU with bioskey() polling while at the same time giving the DOS program
    enough horsepower is a question of some interest to me.

    Typically the polling is too fast and hogs the CPU, or too slow and the
    program can't get much work done. I spent some time tinkering with this,
    and here is a solution I found.

    It runs best with maximum idle sensitivity. You can copy the _default
    shortcut (.pif extension is hidden) from the Windows directory, rename
    it to match the .exe you build, and change its properties; that gives
    you a custom .pif file.

    Trying to retrofit this technique into PCBoard looks difficult though. I
    may never get that far ...


    # include <stdio.h>
    # include <dos.h>
    # include <bios.h>
    # include <time.h>

    static int tick[2];

    void interrupt
    ticktock (void)
    {
    tick[0] = 1;
    }

    int
    main (void)
    {
    static int key;
    static unsigned long count;
    static FILE *file;
    static char *fn = "z.txt";
    static void interrupt (*int1c) (void);
    static time_t now;

    file = fopen (fn, "w");
    if (!file) {
    fprintf (stderr, "failure opening %s\n", fn);
    goto fail;
    }

    int1c = getvect (0x1c);
    setvect (0x1c, ticktock);

    for (count = 0; count < 20000; ++count) {

    now = time (NULL); /* secret ingredient */
    if ((key = bioskey (1)) != 0) {
    if (key == -1 || bioskey (0) == 0x011b) {
    break;
    }
    }

    disable ();
    tick[1] = tick[0];
    tick[0] = 0;
    enable ();

    if (tick[1]) {
    tick[1] = 0;
    fprintf (file, "count = %lu, time = %ld\n", count, now);
    }

    }

    done:
    if (int1c)
    setvect (0x1c, int1c);
    return 0;

    fail:
    if (int1c)
    setvect (0x1c, int1c);
    return 1;
    }
    --- Synchronet 3.17a-Linux NewsLink 1.108