2017-05-02 168 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 
} 
相關問題