I am using the following standard GenerateKey Code :
void GenerateKey ( int vk , BOOL bExtended)
{
KEYBDINPUT kb={0};
INPUT Input={0};
// generate down
if ( bExtended )
kb.dwFlags = KEYEVENTF_EXTENDEDKEY;
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1,&Input,sizeof(Input));
// generate up
::ZeroMemory(&kb,sizeof(KEYBDINPUT));
::ZeroMemory(&Input,sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if ( bExtended )
kb.dwFlags |= KEYEVENTF_EXTENDEDKEY;
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
::SendInput(1,&Input,sizeof(Input));
}
I call this function to simulate the arrow keys(up, down, left, right). However, this works in the normal explorer window and small flash games. However, when I try it on games like Need for Speed or Roadrash it does not work.. Any possible reasons for this behavior?
Your games most likely use DirectInput which works at a lower level so you can not inject events to it using SendInput(). A keyboard filter driver may be required to do what you want. I did quick Googling but didn't come up with anything definite but I hope this gives you some idea of which way you need to go.
There should be a keyboard filter driver sample with the WDK (Windows Driver Kit) that you could modify for your purposes. It would be pretty involved.
来源:https://stackoverflow.com/questions/5762007/sendinput-not-working-for-games