我正在编写一个程序,该程序扫描是否按住了鼠标左键,然后向上发送鼠标左键,然后继续。问题是,由于我将鼠标左键向上发送,因此该程序将不会继续,因为不再按住鼠标左键。

这是一些伪:

if(GetKeyState(VK_LBUTTON) < 0){
   Sleep(10);
   mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
   //Rest of code
}


此后如何检测鼠标左键按下?我需要使用驱动程序吗?

最佳答案

通过阅读您对程序的描述,这里仅是我的实现。
使用Windows API:

while (true) {
     //check if left mouse button is down
     if (GetKeyState(VK_LBUTTON) & 0x8000) {
         //send left mouse button up
         //You might want to place a delay in here
         //to simulate the natural speed of a mouse click
         //Sleep(140);

         INPUT    Input = { 0 };
         ::ZeroMemory(&Input, sizeof(INPUT));
         Input.type = INPUT_MOUSE;
         Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
         ::SendInput(1, &Input, sizeof(INPUT));
     }
}


如果要执行其他操作,同时又要强行阻止用户单击和拖动,则可以将此代码放入线程中调用的函数中。

void noHoldingAllowed() {
     //insert code here used above...
 }

 int main(void) {
  std::thread t1(noHoldingAllowed);
  //other stuff...

  return 0;
{

10-04 21:22
查看更多