SendInput Not working for Games

时光怂恿深爱的人放手 提交于 2019-12-03 20:25:24

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!