Jordan B
Jordan B

Reputation: 109

Detect if mouse button is down

I am new to c++ and I am trying to activate a line of code only when the left mouse button is held down. In this example, my code works but it seems that it just toggles it. When I click, it spams the H key then, when I click again, it stops.

Currently I have this code:

if ((GetKeyState(VK_LBUTTON)))
{
    keybd_event(VkKeyScan('H'),0,0,0);
    Sleep ( 30 );
}

Edit:

I have inside the function:

int WINAPI WinMain ( HINSTANCE hInst, HINSTANCE P, LPSTR CMD, int nShowCmd );

Upvotes: 5

Views: 62724

Answers (4)

pippin1289
pippin1289

Reputation: 4939

Use this to determine if the button is pressed.

if((GetKeyState(VK_LBUTTON) & 0x8000) != 0)

http://vcpptips.wordpress.com/tag/vk_lbutton/

Upvotes: 16

Madara Premawardhana
Madara Premawardhana

Reputation: 31

Use the below to detect left mouse button press

if(GetAsyncKeyState(VK_LBUTTON)){
   //your code controls here
 }

You can find more controls here : https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate

Also if the GetAsyncKeyState(VK_LBUTTON)shows a syntax error, try including winuser.h by adding #include <winuser.h> in the includes of your code.

Here is an example

if(GetKeyState(VK_LBUTTON))
    {   //finding clicked position
        HWND hWnd = FindWindowA(0,("Motion Paths"));
        ::GetWindowThreadProcessId(hWnd,&pid);
        if (hWnd) { cout << "Found" << endl;}

            POINT p;
            GetCursorPos(&p);
            if (ScreenToClient(hWnd, &p))
            {
                int mouseX = p.x;
                int mouseY = p.y;
                cout<< p.x << " "<< p.y <<endl;
            }
    }

Upvotes: 3

Lyusten Elder
Lyusten Elder

Reputation: 3303

In first - need DEFINE BUTTON ID(or another object ID) in begin code:

#define ID_BUTTON1      105

Then AFTER creationale of hWnd - we make button:

   HWND HwndButton1 = CreateWindow( 
    L"BUTTON",  // Predefined class; Unicode assumed 
    L"OK",      // Button text 
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
    10,         // x position 
    10,         // y position 
    100,        // Button width
    100,        // Button height
    hWnd,     // Parent window
   (HMENU) ID_BUTTON1, // ID кнопки в меню
    NULL,            // Сущность мне неведомая 8-)
    NULL);         // Pointer not needed.

And then add trigger in function:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId=0, wmEvent;  //wmId NEED DEFINE null - if he is not available in event, else be ашипка
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmEvent = HIWORD(wParam);  //  Name of EVENT - имя события
        wmId    = LOWORD(wParam);  // ID element for event - элемент с которым оно случилось

        case WM_LBUTTONDOWN: MessageBox(NULL, L"MouseL_Click", L"WndHeader", MB_OK | MB_ICONEXCLAMATION);  // Left Mouse Button pressed

        if( LOWORD(wParam) == 105 && WM_COMMAND == WM_LBUTTONDOWN){ // Клик по ID_BUTTON1 левым мышком
        EndDialog(hWnd,0);
        }
................  // Many another function
}

Upvotes: -3

alk
alk

Reputation: 70893

The application can catch messages and process being sent to your window indicating a state change of any mouse button.

When the left button is pressed a

WM_LBUTTONDOWN

is sent.

When it is released

WM_LBUTTONUP

is sent.

Please read here for various messages being sent to indicate mouse events.

Upvotes: 4

Related Questions