Is it a bug to pass a single-element array to SendInput?
给出以下代码
1 2 3 4 5 6 7 8 | void foo() { INPUT input{}; input.type = INPUT_MOUSE; input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; SendInput(1, &input, sizeof(input)); input.mi.dwFlags = MOUSEEVENTF_LEFTUP; SendInput(1, &input, sizeof(input)); }; |
在连续调用中将单元素数组传递给SendInput是一个错误吗?这似乎得到了文档的完美支持。
简短答案:也许。
更长的答案:取决于。
要查看它所依赖的内容,以及何时重要,它有助于理解Windows API中为何引入了SendInput:例如,它将keybd_event和mouse_event API合并为一个API调用。更重要的是,它增加了以前的呼叫无法使用的重要功能。在文档中对此进行了标注:
The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.
换句话说:
当输入由一系列单个事件组成时(例如在问题中),原子地注入输入通常很重要。该代码在对
同样,注入由键组合组成的键盘输入通常需要原子性保证。注入Ctrl C要求所有四个输入事件都在单个事务中。否则,(恶意)输入源可能会在按下Ctrl键之后立即合成一个Ctrl键事件,从而使代码注入一个C,并附带一个杂散的Ctrl键事件尾随。这可能也不是预期的。
总结:如果满足以下条件,则反复调用
- 输入由一系列单独的输入事件组成。
- 需要将输入解释为单个单位。