将左鼠标按钮向上发送后,检测左键按钮

Detect left mouse button down after sending left mouse button up

本文关键字:按钮 检测 鼠标      更新时间:2023-10-16

我正在编写一个程序,如果按下左鼠标按钮,则扫描,然后将左鼠标按钮向上发送并在此之后继续。问题是,由于我要发送一个左鼠标按钮,因此程序将不会继续,因为左鼠标按钮不再被固定。

这是一些伪:

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

在此之后,如何检测左鼠标按钮被下降?我必须使用驱动程序吗?

只使用标志:

bool lButtonWasDown=flase;
if(GetKeyState(VK_LBUTTON) < 0){
   Sleep(10);
   lButtonWasDown = true;
   mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
} 
if (lButtonWasDown)
{
  // rest of the code for lButtonWasDown = true
}

从这里阅读您对程序的描述是我的实现。使用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;
{

我尚未测试此代码,但应该起作用

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

它应该可以工作,因为如果您在循环重新运行时持有LMouse按钮,则会触发IF语句,然后触发鼠标UP事件。

注意:您可能可以做while(GetKeyState(VK_LBUTTON) < 0){//code here}