2017-05-02 68 views
0

我正在编写一个程序,用于扫描鼠标左键是否被按下,然后发送鼠标左键并继续。问题是,由于我正在发送一个鼠标左键,程序将不会继续,因为鼠标左键不再被按下。在发送鼠标左键后,检测鼠标左键按下

下面是一些伪:

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

如何检测鼠标左键后,这是失望?我需要使用驱动程序吗?

+0

_“程序将无法继续,因为鼠标左键不被按下了” _我不明白。 –

+0

它仍然被压在物理鼠标上,我想知道如何检测物理鼠标按钮被按住。 – Ian

+1

不要发送'MOUSEEVENTF_LEFTUP'。 –

回答

0

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

谢谢,这工作:) – Ian

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语句,然后将触发鼠标松开事件。

注: 你也许可以做到while(GetKeyState(VK_LBUTTON) < 0){//code here}

+0

'if(GetKeyState(VK_LBUTTON)<0)'已经在'while(1)'循环中。我尝试了'while(GetKeyState(VK_LBUTTON)<0)',那不起作用,和if语句一样。 – Ian

+0

你对mouseUp事件做了什么? –

+0

我不确定你的意思,我只想发送mouseUp,并且仍然运行if语句的其余部分。 – Ian

1

只需使用一个标志:

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 
}