Tech Support > Microsoft Windows > Development Resources > Code Doesn't Work
Code Doesn't Work
Posted by Michael R. Copeland on April 26th, 2007


The following code (I forget where I got it) doesn't work for
Control-Key data. It seems to work for Alt-Key data, etc., but it
always returns 0 for any Control-key data. Any thoughts on how to fix
it? TIA
void getKeys(int *Choice, int *escChoice)
{
bool bScanCode = false;
int keyDown = 0;
int flags = 0;
int scanCode = 0;
int keyCode = 0;
INPUT_RECORD irInBuf[128];
while (1) // Loop to read and handle the input events
{ // Wait for the events
if(!ReadConsoleInput(hStdIn, // input buffer handle
irInBuf, // buffer to read into
128, // size of read buffer
&cNumRead)) // number of records read
{
MessageBox(NULL, "ReadConsoleInput", "Console Error", MB_OK);
pause(2, 25); // ***** my own routine...*****
}
for(iX = 0; iX < cNumRead; iX++)
{
if(irInBuf[iX].EventType == KEY_EVENT)
{
keyDown = irInBuf[iX].Event.KeyEvent.bKeyDown;
flags = irInBuf[iX].Event.KeyEvent.dwControlKeyState;
scanCode = irInBuf[iX].Event.KeyEvent.wVirtualScanCode;
keyCode = irInBuf[iX].Event.KeyEvent.uChar.AsciiChar;
*escChoice = *Choice = '\0';
if(keyDown)
{
if (flags & 0x0002) // Alt-key?
{
if (bScanCode)
{
bScanCode = false;
*escChoice = scanCode, *Choice = '\0';
return;
}
else bScanCode = true;
break;
}
if (flags & 0x0008) // Ctrl-key?
{
if (bScanCode)
{
bScanCode = false;
*Choice = '\0', *escChoice = keyCode;
return;
}
else bScanCode = true;
break;
}
if ((flags & 0x000F) == 0) //
{
if (keyCode == 0)
{
*escChoice = scanCode, *Choice = '\0';
return;
}
else
{
*Choice = keyCode, *escChoice = '\0';
return;
}
break;
} // if
} // if
} // if
} // for
} // while

return;
} // getKeys

Posted by tom87.21@gmail.com on May 1st, 2007


On 27 avr, 01:34, mrc2...@cox.net (Michael R. Copeland) wrote:
Hi

On the MSDN I found that :
LEFT_ALT_PRESSED 0x0002 The left ALT key is pressed.
LEFT_CTRL_PRESSED 0x0008 The left CTRL key is pressed.
RIGHT_ALT_PRESSED 0x0001 The right ALT key is pressed.
RIGHT_CTRL_PRESSED 0x0004 The right CTRL key is pressed.

So, the 0x000F == 0 means that none of those keys are down.

A thing I don't understand is why it use the value of scanCode for ALT
or no extra keys cases, and the value of keyCode when CTRL is
pressed :
"*escChoice = keyCode;" in spite of "*Choice = scanCode"

Always from the MSDN :
wVirtualKeyCode : Virtual-key code that identifies the given key in a
device-independent manner.
wVirtualScanCode : Virtual scan code of the given key that represents
the device-dependent value generated by the keyboard hardware.

I hope it could help you.
Tom87@21



Similar Posts