Thread: Direct Input and Mouse Coords

  1. #1
    Registered User
    Join Date
    Dec 2003

    Direct Input and Mouse Coords

    Hello. I am working on using Direct Input for my mouse instead of windows, and am running into a problem. I have looked at about 100 tutorials, and they all have things set up the same way. I am guessing the problem is that I am overlooking something, or I am not using the proper header. Anyways, the problem that is occuring is when I get the mouse coordinates, they are not right. So, for example, if the cursor is at 64, 64 : it returns 78, 96 as its coords. Here is my code - what i am showing you is the includes I am using, the mouse initialization, and inside the main where I look for the mouse coords. let me know if you need more information, please.


    #pragma once
    #define WIN32_LEAN_AND_MEAN  
    #define INITGUID 
    #include <windows.h>   
    #include <windowsx.h> 
    #include <mmsystem.h>
    #include <iostream> 
    #include <conio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <memory.h>
    #include <string.h>
    #include <stdarg.h>
    #include <stdio.h> 
    #include <math.h>
    #include <objbase.h>
    #include <io.h>
    #include <fcntl.h>
    using namespace std;
    #include <ddraw.h>
    #pragma comment(lib, "ddraw.lib")
    #include <dinput.h>
    #pragma comment (lib, "dinput8.lib")
    #pragma comment(lib, "dxguid.lib")

    LPDIRECTINPUT8				lpdi				= NULL;   
    LPDIRECTINPUTDEVICE8  lpdikey			= NULL;   
    LPDIRECTINPUTDEVICE8  lpdimouse	= NULL;    
    LPDIRECTINPUTDEVICE8  lpdijoy			= NULL;     
    UCHAR								keyboard_state[256]; 
    DIMOUSESTATE					mouse_state;        
    DIJOYSTATE    					joy_state;
    int										mouse_x, mouse_y;

    if(FAILED(DirectInput8Create(hinstance_app,DIRECTINPUT_VERSION,IID_IDirectInput8, (void **)&lpdi,NULL))) return 0;
    	if(FAILED(lpdi->CreateDevice(GUID_SysMouse, &lpdimouse, NULL))) return 0;
    	if(FAILED(lpdimouse->SetCooperativeLevel(main_window_handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))) return 0;
    	if(FAILED(lpdimouse->SetDataFormat(&c_dfDIMouse))) return 0;
    	if(FAILED(lpdimouse->Acquire())) return 0;
    	mouse_x = SCREEN_HEIGHT/2;
    	mouse_y = SCREEN_WIDTH/2;

    lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state);
    	mouse_x += (mouse_state.lX);
    	mouse_y += (mouse_state.lY);
    I appreciate any help you can give.



  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Make sure you have the mouse interface in the right mode. Relative mode will return the coords relative to the last position of the mouse. Absolute mode will give the absolute screen coordinates regardless of where the mouse was.

  3. #3
    Registered User
    Join Date
    Dec 2003


    I have tried both modes...perhaps the way i am setting them is wrong?

    DIPROPDWORD         dipdw;
    dipdw.diph.dwSize = sizeof(DIPROPDWORD);
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    dipdw.diph.dwObj = 0;
    dipdw.diph.dwHow = DIPH_DEVICE;
    dipdw.dwData = DIPROPAXISMODE_REL; //or ABS here
    lpdimouse->SetProperty(DIPROP_AXISMODE, &dipdw.diph);
    Also, I tried setting the Cooperation to EXCLUSIVE and FOREGROUND and that combination works, except there is no mouse visible. Any other combination gives the wrong coords. Is there an efficient way to make a mouse visible with that combination, or is the only way to make a bitmap follow the mouse coords and act as the mouse?

    Thanks again for any help.


  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Perhaps you have corrupted the heap prior to doing all this mouse stuff. It does not sound like you are using the mouse incorrectly - incorrect values being returned sounds like a memory leak has occurred somewhere.

    This has happened to me in DOS and in DirectX.

  5. #5
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    If I'm assuming correctly, you're doing this in a console mode program. Your problem is, DirectInput is returning the exact pixel where you clicker. Console mode programs don't work that way, they work in characters. A character (unless you have your console font set to bigger than usual) is 7x12 pixels. Therefore, you have to add 7 to the x value that it returns to you and add 12 to the y value. That should work.
    If I assumed incorrectly and you're not in a console program, ignore this .

    Draco dormiens nunquam titallandus.
    Console Graphics Library:

  6. #6
    Registered User
    Join Date
    Dec 2003
    Thanks for the replies. Ill try out your suggestions. Im in Win32 console project, with application settings set to Windows Application.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console Mouse and Key Input
    By Yuri in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2006, 04:56 PM
  2. Mouse Input
    By DeepFyre in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2004, 08:19 AM
  3. DirectInput8 Action Mapping Using Mouse Input
    By LuckY in forum Game Programming
    Replies: 11
    Last Post: 08-09-2004, 12:09 PM
  4. Mouse Input
    By gvector1 in forum C Programming
    Replies: 7
    Last Post: 10-01-2003, 10:27 AM
  5. Mouse Input????
    By Brandon in forum Game Programming
    Replies: 1
    Last Post: 11-11-2001, 10:04 AM