Mouse Input (in WinAPI)

If I am not mistaken, there are 2 ways to get mouse input:

1. Use the keyboard messages (WM_KEYDOWN, WM_KEYUP, etc.) with the virtual key codes corresponding to the mouse buttons.

2. Use mouse messages (WM_LBUTTONDOWN, WM_LBUTTONUP, etc.).

The question is: If I process mouse messages, do I need to send the keyboard messages for mouse VKs to DefWindowProc()? And if I use keyboard messages, do I need to send the mouse messages to DefWindowProc() ?
Nope, you just need to handle one or the other.
Last edited on
For the mouse, you use WM_LBUTTONDOW/UP yes; but you can also use WM_MOUSEMOVE, which stores the co-ordinates of the mouse in LPARAM and WPARAM (I'm not sure on this; a topic I made a little while ago would help you as I got alot of help with mouse input using the Windows API, unfortunately it is no longer available. Shame, it was only a month or two old).

You can include them in your switch statement; I still have the code from that topic;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) {
switch (message) {
    case WM_MOUSEMOVE:
         wmMouseMove(hwnd, wp, lp); // wp & lp store the co-ordinates here
         break;
    case WM_LBUTTONDOWN:
         wmLButtonDown(hwnd, wp, lp);
         break;
    case WM_LBUTTONUP:
         wmLButtonUp(hwnd, wp, lp);
         break;
    case WM_DESTROY:
         PostQuitMessage(0);       // Send WM_QUIT
         break;
    case WM_KEYDOWN:
        wmKeyDown(hwnd, wp, lp);
        break;
    default:
         return DefWindowProc(hwnd, message, wp, lp);
         }

return 0;
}


Credit to Hamurabi for helping me a huge amount with this program.
Last edited on
> If I am not mistaken, there are 2 ways to get mouse input:

Yes, you're completely mistaken.
Read MSDN and Petzold.
Hey, sudden thought: isn't there also WM_INPUT?
Yeah but isn't that to input to other programs? Or am I thinking of something else..? Sort of like sending mouse movements and things.


If you use WM_(L/R)BUTTONDOWN you should include SetCapture() in your function, and ReleaseCapture() in the LBUTTONUP function; that way you won't let other threads/processes catch your input instead.
Last edited on
Topic archived. No new replies allowed.