Detect if mouse button is down
Asked Answered
P

4

5

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 );
Polydactyl answered 19/8, 2013 at 16:43 Comment(5)
What funciton is this inside of? What library are you using for mouse events?Aalii
C++ doesn't have any knowledge of your mouse. You're using some system's API. Which is it? Perhaps Windows?Koziarz
Sleep() (note the capital S) smells like windows.Eugeniaeugenics
@alk: keybd_event() is a Windows API function.Klee
@RemyLebeau: I nearly guessed that. However, its naming is so uncommon (must be from window v1 or so) I was unsure. For the Sleep() I wasn't.Eugeniaeugenics
A
16

Use this to determine if the button is pressed.

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

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

Aalii answered 19/8, 2013 at 16:47 Comment(4)
Thanks. At first it didn't work, but after reading the comments I tried replacing the 0x80 with 0x100 and works now. From what I read its 100 because I am using 64bit.Polydactyl
A less fiddly way is if (GetKeyState(VK_LBUTTON) < 0) { }, fwiw.Aplanatic
0x100 is the wrong value to mask with. It needs to be 0x8000 instead. < 0 is better, thoughKlee
@RemyLebeau The & method is more fiddly but is faster.Congenial
E
4

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.

Eugeniaeugenics answered 19/8, 2013 at 16:56 Comment(0)
A
3

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;
            }
    }
Arvy answered 1/6, 2020 at 14:50 Comment(0)
S
-3

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
}
Screw answered 19/8, 2013 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.