Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

If QueryKeyWaiting GetKeystroke?

May
12,937
171
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.
 
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 );
}


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.
 
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
 
Back
Top