I can't detect precisely when the left mouse button is pressed with C++ in Windows 10 .
I tried 2 ways :
catching WM_LBUTTONDOWN
message
using directly GetKeyState(VK_LBUTTON)
Each time, the behavior is the same :
If I press the left button during one second,
the WM_LBUTTONDOWN
is sent about 0.5 seconds after I pressed down
the left button
the GetKeyState(VK_LBUTTON)
value returned changes exactly when WM_LBUTTONDOWN is sent, that means 0.5 seconds after I pressed the mouse button
the WM_LBUTTONDOWN
is sent :
But I still don't know how to detect immediately when the left button is down in the case where I press the left button down a long time without moving the mouse. Is there an event I don't know about? Does Windows force us to think its way and use its "CLICK" and its "MOUSEDOWN" ? I mean, is there no way with windows to detect exactly when the mouse button is pressed ? How to detect precisely when the left button is pressed with C++ in windows ?
EDIT :
Thank you for your answers. I made a Minimal Complete and Verifiable code to show you.
The program below compiles with Visual C++ 2017 It shows a blue rectangle that moves each time GetMessage gets a message. The rectangle becomes red and is translated down when WM_LBUTTONDOWN is detected.
As I described in my first post : you will see that the rectangle becomes red when you click or when you press the mouse button down while moving the mouse, but it takes about 0.5 seconds to become red if you press down the left button and don't move the mouse.
Thank you again for your help.
Here is the code :
#include <Windows.h>
#include <GL/GL.h>
#include <math.h>
#pragma comment(lib, "opengl32.lib")
HDC hdc;
HGLRC hrc;
bool ButtonL;
void MyDisplay()
{
static float kk=0; kk+=0.04f;
glMatrixMode(GL_MODELVIEW); glLoadIdentity();glTranslated(0.5*sin(kk),0,0);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if ((GetKeyState(VK_LBUTTON) & 0x100) != 0) glColor3f(1,0,0); else glColor3f(0,0,1);
if (ButtonL) glTranslated(0,-0.5,0);
glBegin(GL_QUADS); glVertex2f(0,0); glVertex2f(0,1); glVertex2f(1,1); glVertex2f(1,0); glEnd();
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_LBUTTONDOWN : ButtonL=true; break;
case WM_LBUTTONUP : ButtonL=false; break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int main(int argc, char** argv)
{
ButtonL=false;
WNDCLASS wc;
RECT WindowRect;
WindowRect.left =(long)0; WindowRect.right =(long)400;
WindowRect.top =(long)0; WindowRect.bottom =(long)300;
HINSTANCE hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";
if (!RegisterClass(&wc)) return(0);
AdjustWindowRectEx(&WindowRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
HWND hWnd=CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,"OpenGL","Titre",WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0,
WindowRect.right-WindowRect.left,WindowRect.bottom-WindowRect.top,NULL,NULL,hInstance,NULL);
PIXELFORMATDESCRIPTOR pfd=
{ sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,32, 0, 0, 0, 0, 0, 0,
0,0,0,0, 0, 0, 0,16,0,0,PFD_MAIN_PLANE,0,0, 0, 0
};
hdc=GetDC(hWnd);
GLuint PixelFormat=ChoosePixelFormat(hdc,&pfd);
SetPixelFormat(hdc,PixelFormat,&pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc , hrc);
ShowWindow(hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
MSG Msg;
while( GetMessage( &Msg, hWnd, 0, 0 ) != 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
MyDisplay();
SwapBuffers(hdc);
}
return 0;
}
Your program works fine on my computer : I don't notice any delay when I press button down. It seems windows messages are intercepted at a very low level on your computer. Do you use a mouse recognition engine like "Sensiva" or "Just Gesture" or "Stroke it" ? It could explain your problem.