1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

If QueryKeyWaiting GetKeystroke?

Discussion in 'Plugins' started by vefatica, Jun 6, 2010.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    If, during batch file execution, a plugin does

    Code:
    if ( QueryKeyWaiting() )
    {
        c = GetKeystroke(0x30);
        // ...
    }
    and [Shift] is/was pressed, then QueryKeyWaiting() returns 1 and GetKeystroke() subsequently blocks.

    The same thing happens with INKEY (see below). If you press/release [Shift] the "INKEY /W0" doesn't return.

    Code:
    DO FOREVER
        SET x=0
        INKEY /W0 %%x
        ECHO %_TIME Key: %x
        ENDIFF
        DELAY 1
    ENDDO
    This could be disastrous if you think a batch file with "INKEY /W0" in it is running ... it might not be running!

    Can I avoid this in either the plugin case or the batch file case? If not, is it something you can/will change?

    Thanks.
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    I'm using the function below instead of QueryKeyWaiting() in my plugin and it seems to be satisfactory. But what about the undesirable behavior of INKEY? Please comment.

    Code:
    BOOL IsKeyWaiting(VOID)
    {
        DWORD n = 0, dwRead = 0, i;
        GetNumberOfConsoleInputEvents(STD_IN, &n);
        INPUT_RECORD *pir = (INPUT_RECORD*) AllocMem(n * sizeof(INPUT_RECORD));
        PeekConsoleInput(STD_IN, pir, n, &dwRead);
        for ( i=0; i<dwRead; i++ ) // look for non-modifier keydown
            if (        pir[i].EventType == KEY_EVENT
                    &&    pir[i].Event.KeyEvent.bKeyDown
                    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
                    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_CONTROL
                    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_MENU
                    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_RWIN
                    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_LWIN )
                break; // got one!
        FreeMem(pir);
        return ( i != dwRead );
    }

     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    Some stuff that was missing from the forum version of my last post (I thought
    Code:
     tags protected the LT and GT characters).
    
    [CODE]pir[i].EventType == KEY_EVENT
    &&    pir[i].Event.KeyEvent.bKeyDown
    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_CONTROL
    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_MENU
    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_RWIN
    &&    pir[i].Event.KeyEvent.wVirtualKeyCode != VK_LWIN
     

Share This Page