Is there any difference between keybd_event() and SendKeys.SendWait()?

*爱你&永不变心* 提交于 2019-12-11 05:08:59

问题


Is SendKeys.SendWait() just a wrapper and these two code snippets are identical?

const int VK_ESCAPE = 0x1B;
keybd_event(VK_ESCAPE, 0, 0, 0);
keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);

and

System.Windows.Forms.SendKeys.SendWait("{ESC}");

If not, is there any reason to use one over the other?


回答1:


keybd_event() is a legacy API, you're supposed to use SendInput() these days. SendKeys either uses a journaling hook or SendInput, depending on .config file setting. The journaling hook is legacy and still the default, SendInput works better on Vista and up, available since .NET 3. So, roughly, yes they are the same. The config setting is:

<appSettings> 
    <add key="SendKeys" value="SendInput"/> 
</appSettings> 



回答2:


The most recent versions of System.Windows.Forms.SendKeys use SendInput, although you have to configure it to do so. Hans has explained about the alternative journaling hook method of SendKeys.

SendInput is the officially sanctioned API call for faking input. The documentation to keybd_event states:

Note This function has been superseded. Use SendInput instead.

You should probably use SendKeys because its lots easier.



来源:https://stackoverflow.com/questions/5097187/is-there-any-difference-between-keybd-event-and-sendkeys-sendwait

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