locked
[win32] - how using joystick? RRS feed

  • Question

  • these msdn link tell me about the joystick messages: http://msdn.microsoft.com/en-us/library/windows/desktop/dd743687%28v=vs.85%29.aspx

    but how can i activate the joystick?

    Monday, July 28, 2014 12:03 PM

Answers

  • Hi,

    >

    1 -

    joySetCapture(inst->hWnd(),JOYSTICKID1,100,true);

    how can i change that line for accept any number(i know it's from 0-15) of joysticks?

    I think you can change the second parameter uJoyID  of joySetCapture to any one you want, but you must make sure your current driver supports that joystick and the given joystick is physically attached to the system. Such as JOYSTICKID2, JOYSTICKID3 and so on.

    > 2 - the joyGetNumDevs() don't give me the number of conneted joysticks(or i read wrong)?

    From MSDN document, we know that the joyGetNumDevs function queries the joystick driver for the number of joysticks it supports. The joyGetNumDevs function returns the number of joysticks supported by the current driver or zero if no driver is installed. Here we must know that joyGetNumDevs() will provide the number of joysticks which our current driver supports, not the number of connected joysticks.

    We can use the joyGetPos function to determine whether a given joystick is physically attached to the system. If the specified joystick is not connected, joyGetPos returns a JOYERR_UNPLUGGED error value.

    The following example uses joyGetNumDevs and joyGetPos to determine whether the joystick services are available and if a joystick is attached to one of the ports.

    JOYINFO joyinfo; 
    UINT wNumDevs, wDeviceID; 
    BOOL bDev1Attached, bDev2Attached; 
     
        if((wNumDevs = joyGetNumDevs()) == 0) 
            return ERR_NODRIVER; 
        bDev1Attached = joyGetPos(JOYSTICKID1,&joyinfo) != JOYERR_UNPLUGGED; 
        bDev2Attached = wNumDevs == 2 && joyGetPos(JOYSTICKID2,&joyinfo) != 
            JOYERR_UNPLUGGED; 
        if(bDev1Attached || bDev2Attached)   // decide which joystick to use 
            wDeviceID = bDev1Attached ? JOYSTICKID1 : JOYSTICKID2; 
        else 
            return ERR_NODEVICE; 
    

    About Capturing Joystick Input, you can refer to the sample code from this link.

    Joystick Notification Messages

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 29, 2014 7:13 AM

All replies

  • finaly i put the messages to working:

    case WM_NCCREATE:
                    {
                        CREATESTRUCT *p = (CREATESTRUCT *)lParam;
                        inst = (form *)p->lpCreateParams;
                        SetWindowLongPtr(HandleWindow, GWLP_USERDATA, (LONG_PTR)inst);
                        inst->hwnd = HandleWindow;
                        joySetCapture(inst->hWnd(),JOYSTICKID1,100,true);
                        JoystickCount=joyGetNumDevs();
                        if (joyGetNumDevs()==1)
                        {
                            MessageBox(NULL, "joystick activated","hi",MB_OK);
                        }
                    }
                    break;
                    case MM_JOY1BUTTONUP:
                    {
                        MessageBox(NULL,"hi","Hello", MB_OK);
                    }
                    break;

    when i click on a button, the message box is showed. but i see 2 problems in these code:

    1 -

    joySetCapture(inst->hWnd(),JOYSTICKID1,100,true);

    how can i change that line for accept any number(i know it's from 0-15) of joysticks?

    2 - the joyGetNumDevs() don't give me the number of conneted joysticks(or i read wrong)?

    Monday, July 28, 2014 12:27 PM
  • Hi,

    >

    1 -

    joySetCapture(inst->hWnd(),JOYSTICKID1,100,true);

    how can i change that line for accept any number(i know it's from 0-15) of joysticks?

    I think you can change the second parameter uJoyID  of joySetCapture to any one you want, but you must make sure your current driver supports that joystick and the given joystick is physically attached to the system. Such as JOYSTICKID2, JOYSTICKID3 and so on.

    > 2 - the joyGetNumDevs() don't give me the number of conneted joysticks(or i read wrong)?

    From MSDN document, we know that the joyGetNumDevs function queries the joystick driver for the number of joysticks it supports. The joyGetNumDevs function returns the number of joysticks supported by the current driver or zero if no driver is installed. Here we must know that joyGetNumDevs() will provide the number of joysticks which our current driver supports, not the number of connected joysticks.

    We can use the joyGetPos function to determine whether a given joystick is physically attached to the system. If the specified joystick is not connected, joyGetPos returns a JOYERR_UNPLUGGED error value.

    The following example uses joyGetNumDevs and joyGetPos to determine whether the joystick services are available and if a joystick is attached to one of the ports.

    JOYINFO joyinfo; 
    UINT wNumDevs, wDeviceID; 
    BOOL bDev1Attached, bDev2Attached; 
     
        if((wNumDevs = joyGetNumDevs()) == 0) 
            return ERR_NODRIVER; 
        bDev1Attached = joyGetPos(JOYSTICKID1,&joyinfo) != JOYERR_UNPLUGGED; 
        bDev2Attached = wNumDevs == 2 && joyGetPos(JOYSTICKID2,&joyinfo) != 
            JOYERR_UNPLUGGED; 
        if(bDev1Attached || bDev2Attached)   // decide which joystick to use 
            wDeviceID = bDev1Attached ? JOYSTICKID1 : JOYSTICKID2; 
        else 
            return ERR_NODEVICE; 
    

    About Capturing Joystick Input, you can refer to the sample code from this link.

    Joystick Notification Messages

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 29, 2014 7:13 AM
  • Hi,

    >

    1 -

    joySetCapture(inst->hWnd(),JOYSTICKID1,100,true);

    how can i change that line for accept any number(i know it's from 0-15) of joysticks?

    I think you can change the second parameter uJoyID  of joySetCapture to any one you want, but you must make sure your current driver supports that joystick and the given joystick is physically attached to the system. Such as JOYSTICKID2, JOYSTICKID3 and so on.

    > 2 - the joyGetNumDevs() don't give me the number of conneted joysticks(or i read wrong)?

    From MSDN document, we know that the joyGetNumDevs function queries the joystick driver for the number of joysticks it supports. The joyGetNumDevs function returns the number of joysticks supported by the current driver or zero if no driver is installed. Here we must know that joyGetNumDevs() will provide the number of joysticks which our current driver supports, not the number of connected joysticks.

    We can use the joyGetPos function to determine whether a given joystick is physically attached to the system. If the specified joystick is not connected, joyGetPos returns a JOYERR_UNPLUGGED error value.

    The following example uses joyGetNumDevs and joyGetPos to determine whether the joystick services are available and if a joystick is attached to one of the ports.

    JOYINFO joyinfo; 
    UINT wNumDevs, wDeviceID; 
    BOOL bDev1Attached, bDev2Attached; 
     
        if((wNumDevs = joyGetNumDevs()) == 0) 
            return ERR_NODRIVER; 
        bDev1Attached = joyGetPos(JOYSTICKID1,&joyinfo) != JOYERR_UNPLUGGED; 
        bDev2Attached = wNumDevs == 2 && joyGetPos(JOYSTICKID2,&joyinfo) != 
            JOYERR_UNPLUGGED; 
        if(bDev1Attached || bDev2Attached)   // decide which joystick to use 
            wDeviceID = bDev1Attached ? JOYSTICKID1 : JOYSTICKID2; 
        else 
            return ERR_NODEVICE; 

    About Capturing Joystick Input, you can refer to the sample code from this link.

    Joystick Notification Messages

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    thanks for correct me.

    but i need ask more:

    1- i used the MM_JOY1MOVE , but i have seen 1 bug: why these message is activated in 1st time when the form is created, but i didn't pressed the directions?

    2 - the joystick messages only works for 2 joysticks, right? and if i want program more joysticks?

    3 - why i only have access of 4 buttons, when the joystick have much more button?

    Tuesday, July 29, 2014 7:45 AM
  • Hi,

    1- i used the MM_JOY1MOVE , but i have seen 1 bug: why these message is activated in 1st time when the form is created, but i didn't pressed the directions?

    I think you may have a try to use joySetThreshold function to set the movement threshold value. Because this value is initially zero.

    The movement threshold is the distance the joystick must be moved before a joystick position-change message (MM_JOY1MOVE, MM_JOY1ZMOVE, MM_JOY2MOVE, or MM_JOY2ZMOVE) is sent to a window that has captured the device. The threshold is initially zero.

    2 - the joystick messages only works for 2 joysticks, right? and if i want program more joysticks?

    3 - why I only have access of 4 buttons, when the joystick have much more button?

    As to question 2 and 3, this is out of my support range and I am not sure about that. But I think maybe the multimedia Joystick API could not support at that time. Because this API has been supereded by DirectInput, which has more capabilities for today's input and force-feedback devices. You may have a try to use DirectX to do joystick game. And the sample project in link below may be helpful for you.

    Joystick Driver Project

    Hope this can help you.

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 31, 2014 6:30 AM
  • Hi,

    1- i used the MM_JOY1MOVE , but i have seen 1 bug: why these message is activated in 1st time when the form is created, but i didn't pressed the directions?

    I think you may have a try to use joySetThreshold function to set the movement threshold value. Because this value is initially zero.

    The movement threshold is the distance the joystick must be moved before a joystick position-change message (MM_JOY1MOVE, MM_JOY1ZMOVE, MM_JOY2MOVE, or MM_JOY2ZMOVE) is sent to a window that has captured the device. The threshold is initially zero.

    2 - the joystick messages only works for 2 joysticks, right? and if i want program more joysticks?

    3 - why I only have access of 4 buttons, when the joystick have much more button?

    As to question 2 and 3, this is out of my support range and I am not sure about that. But I think maybe the multimedia Joystick API could not support at that time. Because this API has been supereded by DirectInput, which has more capabilities for today's input and force-feedback devices. You may have a try to use DirectX to do joystick game. And the sample project in link below may be helpful for you.

    Joystick Driver Project

    Hope this can help you.

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    theres another way for not be so limited ;)

    i can do a timer for read the joystick actions. then i can test 18 buttons and more actions and more joysticks ;)

    but something isn't right: i can't create the timer :(

    i didn't get an error, only the timer isn't created :(

    just see these code:

    static LRESULT CALLBACK WndProcForm(HWND HandleWindow, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                static POINT PreviousLocation, Location;
                static bool Tracking = false;
                static MouseButtons MBButtons;
                static bool blControl = false;
                static bool blShift = false;
                static bool blResize = false;
                static int xPos = 0;
                static int yPos = 0;
                static UINT_PTR timerid=0;
                static UINT_PTR JoystickTimer=0;
                static bool blnDrag = false;
                static bool KeyPressed=false;
                static int KeyDownCount=0;
                UINT JoystickCount =0;
    
                form *inst = (form *)GetWindowLongPtr(HandleWindow, GWLP_USERDATA);
    
    
    
                if(inst!=NULL && inst->Create!=NULL )
                {
                    static bool i=true;
                     if(i==true)
                     {
                        i=false;
                        SetTimer(inst ->hwnd,JoystickTimer,120,NULL);
                        inst -> Create(inst->getLeft(), inst->getTop());
                     }
                }
    //inst its the form pointer instance

    the Create() is called, so the inst isn't the problem :(

    so what i'm doing wrong?

    these bug don't make sence

    Thursday, July 31, 2014 9:29 AM
  • see the new test:

    case WM_LBUTTONUP:
                    case WM_RBUTTONUP:
                    case WM_MBUTTONUP:
                    case WM_XBUTTONUP:
                    {
                        int a= SetTimer(inst ->hWnd(),JoystickTimer,120,NULL);
                        SetWindowText(inst->hwnd,to_string(a).c_str());

    the 'a' is 1.

    see these:

     case WM_LBUTTONUP:
                    case WM_RBUTTONUP:
                    case WM_MBUTTONUP:
                    case WM_XBUTTONUP:
                    {
                        SetTimer(inst ->hWnd(),JoystickTimer,120,NULL);
                        int a=GetLastError();
                        SetWindowText(inst->hwnd,to_string(a).c_str());

    the GetLastError() give me '0'(zero).

    so why the timer isn't created?

    Thursday, July 31, 2014 11:12 AM
  • see the new test:

    case WM_LBUTTONUP:
                    case WM_RBUTTONUP:
                    case WM_MBUTTONUP:
                    case WM_XBUTTONUP:
                    {
                        int a= SetTimer(inst ->hWnd(),JoystickTimer,120,NULL);
                        SetWindowText(inst->hwnd,to_string(a).c_str());

    the 'a' is 1.

    see these:

     case WM_LBUTTONUP:
                    case WM_RBUTTONUP:
                    case WM_MBUTTONUP:
                    case WM_XBUTTONUP:
                    {
                        SetTimer(inst ->hWnd(),JoystickTimer,120,NULL);
                        int a=GetLastError();
                        SetWindowText(inst->hwnd,to_string(a).c_str());

    the GetLastError() give me '0'(zero).

    so why the timer isn't created?

    my problem was the timers ID.. i used them ID, that's why :(

    now the timers works correctly ;)

    static UINT_PTR timerid=0;
    static UINT_PTR JoystickTimer=1;

    now i'm reading more about the JOYINFOEX structure:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd757112%28v=vs.85%29.aspx

    after reading that page several times... i continue with same question:

    what is dwZpos, dwUpos, dwVpos, dwRpos coordenates?

    at least i know the dwXpos and dwYpos coordenates are for directions ;)

    and what is dwPOV?

    maybe i don't some joystick definitions... but i need ask for understand ;)

    Sunday, August 3, 2014 6:39 PM
  • now i understand what i need... it's a joystick layout ;)

    please see the image:

    https://onedrive.live.com/?cid=C3EF456E15C8DEB6&id=C3EF456E15C8DEB6!1259&v=3

    i have 1 question, from the image:

    - the X and Y axis  and Hat directions are the same or can be programmed separed(for diferent actions)?

    Sunday, August 3, 2014 7:30 PM
  • if i activate the Analog button(when the led is red), the Hat buttons aren't working like a buttons :(

    how can i use the Hat when the Analog is red?

    - can i get the Z value in degrees?(0-360)

    Saturday, August 9, 2014 6:37 PM
  • finally i found something ;)

    the degrees are calculated:

    degrees = arcotangente(X/Y)

    but seems not give me the right results:

    if (b.dwXpos!=0 || b.dwYpos!=0) 
                                    inst->Text=to_string((int)atan(b.dwXpos/b.dwYpos));

    i'm getting a memory leak... what i'm doing wrong?

    Saturday, August 9, 2014 9:52 PM