Advertisement

cant get VK_LBUTTON to work at all.

Started by March 31, 2002 06:17 PM
7 comments, last by csxpcm 22 years, 10 months ago
hello, I''m sure this is a simple question but I can''t get it to work. In the below switch statement Im trying to get the left mouse button to correspond to the commad to move the camera. The VK_DOWN works fine. but why wont VK_LBUTTON? This is the command for the left mouse button right? When I press it, nothing happens at all, the camera doesnt move like it should. Any suggestions anyone? - kind regards. case VK_DOWN: gCamera.moveCamera(-speed); RenderScene(); break; case VK_LBUTTON: <--how do i get program respond to the mouse? gCamera.moveCamera(-speed); RenderScene(); break;
Im surpised the compiler isnt complaining about VK_LBUTTON. Shouldn''t it be WM_LBUTTON?


-=[ Megahertz ]=-
-=[Megahertz]=-
Advertisement
I think this lies within the region of my knowledge :D

This actually depends if you''re using Dx7''s or Dx8''s or directinput. This is how I would check for the left mousebutton with Dx7.

LPDIRECTINPUT7 lpdi; //<-- we need this object.
DIMOUSESTATE mouseState; //<-- this is where the mouse''s state is stored.

LPDIRECTINPUTDEVICE7 lpdidMouse; //<-- We need thise device to get data from the mouse.

DirectInputCreateEx(hinst, DIRECTINPUT_VERSION, IID_IDirectInput7, (void**)&lpdi, NULL); //<-- initialize Dinput like so...

lpdi->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice7,(void**)&lpdidMouse, NULL); //<-- Initialize the device for the mouse like so...

lpdidMouse->SetDataFormat(&c_dfDIMouse); //<-- Choose which type of mouse it is, this is standard.

lpdidMouse->SetCooperativeLevel(*hwindow,DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); //<-- Set the amount of control you want to give the input device.

lpdidMouse->Acquire(); //<--Grabb the mouse!

//That was the initialization bit =)
//Here''s how you read/get data from the darn mouse-thingy =)
lpdidMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mouseState);

Right, and here''s how the DIMOUSESTATE looks like:
typedef struct DIMOUSESTATE {
LONG lX;
LONG lY;
LONG lZ;
BYTE rgbButtons[4];
} DIMOUSESTATE, *LPDIMOUSESTATE;

when you move your mouse you get readings from -x to x in lX,lY and where 0 means it''s not getting any values, the mouse''s still.

If you wanna move a pic. with the mouse you simply do the following:
pic.x += mouseState.lX;
pic.y += mouseState.lY;

lZ is... um.. the wheel. If the mouse doesn''t support a wheel this variable is zeroed. I''ve never used it though...

now HERE comes the darn BUTTONS. The buttons you wanted to get the info on, right?

This is what it says in the SDK:
rgbButtons[4]
Array of button states. The high-order bit of the byte is set if the corresponding button is down.

Mhm.. I used the following to check if a button is down:
if(mouseState.rgbButtons[1]) //do some crap.

Well, here''s the info:
rgbButtons[0] = Left mousebutton
rgbButtons[1] = Right mousebutton
rgbButtons[3] = Middle mousebutton
rgbButtons[4] = dunno, never used it

There. put the initialization in some MouseInit() func. and the getDevicestate in some MouseUpdate(&mouseState) or MouseUpdate() func.

then you would use these functions like this:
//where you init everything
MouseInit();
//where you use everything
MouseUpdate(&mouseState) or MouseUpdate();

There you go.





Js
Js
hello
Thanks for the advice both of u! much appreciated. Im sure when my game gets better i''ll use DirectInput, I have a great chapter on it in my OpenGL Game Programming book, and the advice is this thread will also help me loads.
However, for now I don''t want to code in DirectInput stuff and wanted a quick hack to get the program to respond to the left mouse button. I discovered most of the keyboard definitions are in winuser.h, so i thought the one for the left mouse button was VK_LBUTTON, and the one for the keyboards left button was VK_LEFT.
I tried the advice from Megahertz, but I got the following error.

''WM_LBUTTON'' : undeclared identifier

so do I need to include the appropriate header file? if so what is it anyone? All I want it the left mouse button to respond in my program, but not using DirectInput at this stage.

Thanks for the previous advice, its much apprecaied!
Thanks in advance, kind regards.
What does your switch statement look like? The three ways to obtain the keystate that I know of are GetAsyncKeyState, GetKeyState and GetKeyboardState. I would hope you''re using the third, because it is the only one that obtains the entire state. That done, I can''t see how you would test virtual key states using a switch statement, given that each key is represented by a separate byte.

Post the code above your case labels, let''s have a looksee.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
ok, this is the code that doesnt work :-(



LRESULT CALLBACK wndProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
GLApp *glWindow = (GLApp*)GetWindowLong(hWnd, GWL_USERDATA);

if ((glWindow == NULL) && (uMsg != WM_CREATE))
return DefWindowProc(hWnd, uMsg, wParam, lParam);

switch (uMsg)
{
case WM_CREATE: // window creation
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
glWindow = (GLApp*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
SetWindowLong(hWnd, GWL_USERDATA, (LONG)glWindow);
glWindow->getHandle() = hWnd;
return glWindow->create();
}

case WM_QUIT:
case WM_CLOSE:
glWindow->destroy();
PostQuitMessage(0);
return 0;

case WM_DESTROY:
glWindow->destroy();
PostQuitMessage(0);
return 0;

case WM_PALETTECHANGED:
glWindow->paletteChanged(wParam);
return 0;

case WM_SIZE:
if (wParam != SIZE_MINIMIZED)
{
glWindow->width = LOWORD(lParam);
glWindow->height = HIWORD(lParam);
glWindow->size();
}
return 0;

case WM_KEYDOWN:


switch(wParam) { // Check if we hit a key
case VK_ESCAPE:
DeInit();
PostQuitMessage(0);
break;

case VK_UP:
gCamera.moveCamera(+speed);
RenderScene();
break;

case VK_DOWN:
gCamera.moveCamera(-speed);
RenderScene();
break;

case WM_LBUTTON: <----- DOESNT WORK
gCamera.moveCamera(-speed);
RenderScene();
break;

case VK_SPACE:
particles->fireWeapon();
break;
}
break;

default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

[edited by - csxpcm on April 1, 2002 8:06:20 AM]
Advertisement
Shouldn''t you use WM_LBUTTONDOWN and WM_LBUTTONUP ?

like (ripoff from my engine):


struct MOUSE_BUTTON_S // 9 bytes (~12)
{
bool down; // 0, 1,
POINT point; // 1, 8,
};
MOUSE_BUTTON_S RMB,LMB,MMB;

...

case WM_LBUTTONDOWN:
SetCapture(hWnd); LMB.down=true;
break;
case WM_LBUTTONUP:
if(LMB.down)ReleaseCapture();LMB.down=false;
break;

case WM_MOUSEMOVE:
GLint curX=LOWORD(lParam),curY=HIWORD(lParam);

if(curX&1<<15) curX -= (1<<16);
if(curY&1<<15) curY -= (1<<16);

GLint dY = LMB.point.y-curY;
GLint dX = LMB.point.x-curX;

if(LMB.down)
{
thingy.rotation.y += GLfloat((dX)*0.0125F);
thingy.rotation.x += GLfloat((dY)*0.0125F);
}
if(RMB.down)
{
thingy.translation.x += GLfloat((dX)*0.0125F);
thingy.translation.y -= GLfloat((dY)*0.0125F);
}
if(MMB.down)
{
thingy.translation.z+= GLfloat((dY)*0.0125F);
}
LMB.point.x = curX;
LMB.point.y = curY;
break;


cya
In my hand I hold a quite not-so-serious book by André LaMothe and I shall now reveal the secrets of this weird phenomenon!
First of all we have WM_MOUSEMOVE, here you can check the mouse''s state while it''s MOVING.
When checking for the buttons'' state when the mouse is moving we have the following states:
MK_LBUTTON //left is being pressed
MK_RBUTTON //right is being pressed
MK_MBUTTON //middle is being pressed
MK_CONTROL //ctrl-key is being pressed
MK_SHIFT //shift-key is being pressed
This is how you check the buttons:

case WM_MOUSEMOVE:
{
int mouse_x = (int)LOWORD(lParam);
int mouse_y = (int)HIWORD(lParam);
int buttons = (int)wParam;
if(buttons & MK_LBUTTON) Shoot();
if(buttons & MK_RBUTTON) MoveForward();
if(buttons & MK_MBUTTON) MoveBackward();
if(buttons & MK_CONTROL) Shoot2nd();
if(buttons & MK_SHIFT) Reload();
}
break;

And HERE''s the way to do it when the mouse is NOT MOVING...
We have the following messages to look out for:

WM_LBUTTONDBLCLK //left double click
WM_LBUTTONDOWN //left button is down
WM_LBUTTONUP //left button is released

WM_MBUTTONDBLCLK //middle double click
WM_MBUTTONDOWN //middle button is down
WM_MBUTTONUP //middle button is released

WM_RBUTTONDBLCLK //right double click
WM_RBUTTONDOWN //right button is down
WM_RBUTTONUP //right button is released

and as such you handle each and one of them individually, example:

case WM_LBUTTONDBLCLK:
{
int mouse_x = (int)LOWORD(lParam);
int mouse_y = (int)HIWORD(lParam);
SelectItemAtCoord(x,y);
}
break;

case WM_LBUTTONDOWN:
{
int mouse_x = (int)LOWORD(lParam);
int mouse_y = (int)HIWORD(lParam);
if(counter > 2.0f) ChargeWeapon();
else counter++;
}

case WM_LBUTTONUP:
{
int mouse_x = (int)LOWORD(lParam);
int mouse_y = (int)HIWORD(lParam);
if(counter > 2.0f) FireCharge();
else Fire();
counter=0;
}
break;

You see? There''s how it''s done, so skip the VK, they''re for KEYBOARD CODE ONLY.

Here''s a thing for you to use instead of those nasty windows messages for the keyboard:

#define KEY_DOWN(vk_code) ((GetAyncKeyState(vk_code) & 0x8000) ?1 : 0)

#define KEY_UP(vk_code) ((GetAyncKeyState(vk_code) & 0x8000) ?0 : 1)

There you go =)
-Js
Js
Oh.. forgot to mention how to use the key_down/up thingies...
Remember, this is an OLD sniper rifle i''m firing with, this means i''ll have to reload every time i''ve fired a bullet.
if(KEY_DOWN(VK_ENTER)) Fire(); //Haha! Eat that you....err.. bastard!

if(KEY_UP(VK_ENTER)) Reload(); //Haha! Lucky you I''ve gotta reload you....err....thing(?)!
-Js
Js

This topic is closed to new replies.

Advertisement