Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AHK detects ghost joystick controllers



  • Please log in to reply
5 replies to this topic
ghutch92
  • Members
  • 9 posts
  • Last active: Mar 28 2013 12:05 AM
  • Joined: 16 Oct 2012

AHK is fine when detecting new controllers while script is running. It is not good at telling me when a controller disconnects or is no longer in the same port.

 

I have no controllers connected.

I start my script.

I connect two 360 controllers to my PC.

Script detects two new controllers in with port numbers 1 & 2

Script is still running.

I disconnect controller in port 2

Script detects the same controllers in ports 1 & 2 even though controller 2 was disconnected

I close the script in disappointment

I restart the script (controller 1 is connected and controller 2 is disconnected)

Script only detects 1 controller in port 1

I close script in befuddlement

 

Why did ahk detect a ghost controller? How to fix this?

 

If anyone knows another way to verify controller's existing using ahk with some outside source, please share your wisdom and knowledge.

 

The test script press home button to get msgboxes with port number.

http://pastebin.com/DVUXKLjy 



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Why did ahk detect a ghost controller?

AutoHotkey provides neither a function to determine how many controllers there are nor a notification when one is connected or disconnected. Therefore, I can only presume you wrote a script which attempts to detect controllers. I can offer a few possible explanations:

  • AutoHotkey asked Windows for the state of a controller, and Windows gave it even though that controller had been disconnected. In this case, the OS says there's a controller, so who are we to argue? (This does not occur on my Windows 7 system with my Xbox 360 controller, which has default drivers; GetKeyState immediately stops returning numeric values.)

  • Your script is flawed and is falsly detecting a controller which doesn't exist.

  • You are mistaken, and the ghost controller was not detected at all.

Your pastebin link did not work; it says the paste ID is invalid.

 

Also note that AutoHotkey does not decide which Joystick ID corresponds to which device. It simply passes the ID (minus 1) along to the appropriate API, such as getJoyPosEx.



ghutch92
  • Members
  • 9 posts
  • Last active: Mar 28 2013 12:05 AM
  • Joined: 16 Oct 2012

Thanks for responding Lexikos.

 

The code in the pastebin that detects ghost controllers.

Hotkey, home, test
Return
 
test:
        Loop  ; Query each joystick number to find out which ones exist.
                {
                GetKeyState, JoyName, %A_Index%JoyName
                If JoyName {
                        JoystickNumber := A_Index
                        msgbox % JoystickNumber
                }
                If A_Index > 16
                        Break
        }
Return
ExitApp

 

 

I am also the 360 controllers which I bought directly from Microsoft with the latest drivers fro Windows 7 64bit.

 

Plugin two controllers in 

Press the home button to see what id numbers your controllers possess

Unplug one controller, ahk should now only report one number correct?

press home button, ahk reports two controllers

Windows only reports one controller connected under Control Panel.



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer
I did the following:
  • Yesterday, plugged in a USB 360 controller, used it, unplugged it.
  • Today, plugged in a wireless receiver and connected a wireless 360 controller.
  • Ran the following script:
Loop 16
    if name := GetKeyState(A_Index "JoyName")
        list .= A_Index ": " name "`n"
MsgBox % list
This produced the following:
 

2: Microsoft PC-joystick driver

 
The wireless controller is Joystick 2.  If the USB controller is plugged in, it is Joystick 1.  As far as I can tell, Windows remembers each controller, and they are always assigned the same ID regardless of which order they are disconnected/connected in.
 
I have verified that if joyGetDevCaps (which is used by GetKeyState) is called while the controller is connected, calling it again from the same process will succeed (and produce a name) even if the controller is no longer connected.  This API is part of Windows, hence "the OS says there's a controller".
joyGetName(ID) {
    VarSetCapacity(caps, 728, 0)
    if DllCall("winmm\joyGetDevCapsW", "uint", ID-1, "ptr", &caps, "uint", 728) != 0
        return "failed"
    return StrGet(&caps+4, "UTF-16")
}
Loop
    MsgBox % joyGetName(1)
So as I said, your script is flawed - it relies on the name being blank when the controller is disconnected.  This is probably based on a false assumption in the joystick test script.  If you want to know whether a joystick is actually connected and you know that it has at least one axis (all 360 controller's do), query for JoyX.  If it is blank/empty, the controller is not connected.
 
The number of controllers shown in Control Panel is not relevant.  Windows is a complicated mess of numerous components, and has many inconsistencies.  One such inconsistency is that the joystick ID used by the multimedia API and the controller number used by XInput (and lit up on the controller) do not match.  This makes sense, as the multimedia API can support up to 16 joysticks with greatly varying capabilities, whereas I believe XInput supports only 4 controllers and expects them to support certain features.
 
XInput_GetState() in the XInput script (link above) returns 0 if the controller is not connected, or an object containing the state of the controller's buttons, axes, etc. if it is connected.  It also supports other capabilities of the controller that aren't supported by GetKeyState.

ReynoldsCahoon
  • Members
  • 5 posts
  • Last active: Jun 01 2014 08:59 AM
  • Joined: 20 May 2014

This makes sense, as the multimedia API can support up to 16 joysticks with greatly varying capabilities, whereas I believe XInput supports only 4 controllers and expects them to support certain features.

 

Sorry if I'm not understanding XInput correctly, but I've connected more than 4 Xbox 360 controllers and used them for various games, were these games just not utilizing XInput, or does XInput actually support more? I assume these games were likely just using something other than XInput?



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

I believe your assumption is correct.