
If this is the case I think I will just go with the Lexikos xinput stuff and build out my own loops with settimer like you mentioned. I think (correct me if I'm wrong) yours still needs to run a loop to check button states. I wanted to use your library because I thought it was possible to implement controller support and button detection without some sort of polling loop. I imagine much of this would be solved with multiple actual threads but it is what it is. I set the callback thread to be non-critical and called the critical threads with a boundfunc and settimer which seemed to temporarily solve the crashes I was having.

Thanks for the clarification, the critical stuff makes a lot of sense. You should not even have long running loops in your AHK in response to input - you should use SetTimer instead. You should bear in mind that AHK is NOT a multi-threaded language, it fakes it. I mean, think about it, when you move an axis, you are NEVER going to be able to move it by just one unit and hold it precisely there for 500ms, so you will always get multiple updates every time you move the stick.
#Xinput test code
For example, new input comes in, and the C# code tries to execute some AHK code, but because you have a critical thread running, it's uninterruptable, so goes boom The crash you mentioned is possibly because when you press a button, you set the thread critical, and then go into a sleep, you are locking up the thread and it maybe does not like that. There's a TestApp project in there as well as the JoystickWrapper which allows you to test the library entirely within Visual Studio To view the C# source, simply open the JoystickWrapper.sln file using Visual Studio. Oh crap, I completely forgot (I wrote this so long ago.), this library does support XInput. I have added a critical thread when Button1 is pressed and if you run this script you will notice the script then becomes unresponsive. When hitting any basic button you should see a tooltip "Button2: 1" or "Button3: 0". This subscribes to the 10 buttons on my xbox controller. (given your recommendation above I will try directinput next) but I just wanted to illustrate the issue. I changed simpleexample.ahk slightly to use xInput instead of directinput (i know the GetAnyDeviceGuid method is direct but its fine). Here is a very simple modification that can illustrate the issue: I am struggling with critical threads and the script crashing. Lastly, I did send you a direct email which you may have seen, but I had one more question in there I didn't include here. Is there a way to view the C# code? I have only ever worked with AHK so forgive me but I am starting to poke into C# and I like having actual projects to do to learn. Maybe possible by editing the C# code, but not something I implemented Thanks for you contributions and help thus far! If i unsubscribe from all events then remove controller it's fine but I don't want the script crashing if the controller is unplugged preemptively. Perhaps because it's racing whatever is happening inside the dlls. I have tried monitoring WM_DEVICECHANGE and quickly unsubscribing from all events but it doesnt seem reliable.

Unhandled Exception: SharpDX.SharpDXException: HRESULT:, Module:, ApiCode:, Message: The device is not connected.Īt ()Īt _19_0()Īt (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)Īt (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)Īt (ExecutionContext executionContext, ContextCallback callback, Object state)Īt ()
