Joy hotkey in #If causing massive cpu spike

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Joy hotkey in #If causing massive cpu spike

Post by doubledave22 » 12 May 2021, 11:19

Hi, I just got help in the AHK discord and they mentioned to make a post looking for help.

Note: This could possibly be moved to bugs.

The following script with no game controller/joystick connected is causing a severe CPU spike ~ 20% of cpu. Once a game controller is connected it goes down to 0 CPU.

Code: Select all

joy1::return
My actual use case is more like:

Code: Select all

#if Winactive("ahk_id " HotkeyPicker)
joy1::
.
.
.
and since #If directives are run before the script begins it is trying to initialize the Joy1:: hotkey but no joystick is found. I don't think this should be intended (and its certainly not what I need) because I have users who will open the software without a game controller then attach one later if they want.

Certainly open to ideas. Thanks!
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Joy hotkey in #If causing massive cpu spike

Post by mikeyww » 12 May 2021, 11:38

I ran your script without a joystick. I did not experience any such issues. There is probably some other issue at hand.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Joy hotkey in #If causing massive cpu spike

Post by doubledave22 » 12 May 2021, 11:42

Yeah this doesn't happen 100% of the time and a restart can fix it temporarily so it's hard to say exactly the cause. The help I received in the discord was able to reproduce the issue with the 1 line joy hotkey script.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Joy hotkey in #If causing massive cpu spike

Post by mikeyww » 12 May 2021, 11:45

If another key on your keyboard is actually triggering joy1 somehow (repeatedly), it might do this. KeyHistory may tell you more. That is all that comes to mind. Others may know more.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Joy hotkey in #If causing massive cpu spike

Post by doubledave22 » 12 May 2021, 11:53

I was able to induce the problem by plugging in my PS4 controller and removing it then running the script again. Key history doesnt show any keys pressed.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Joy hotkey in #If causing massive cpu spike

Post by mikeyww » 12 May 2021, 11:56

I do not use PS, but if you remove that line, perhaps the problem will be fixed. If you are actually trying to use the joy1 hotkey, then you might have a search here on the forum for others who found the way. Nonetheless, if KeyHistory does not show anything, then I am not sure why you would be experiencing this. Perhaps somehow removing the PS is causing the issue-- so I wonder if restarting the script at that point would fix it. You indicated that this is the case (as I looked back), so perhaps this is an easy fix: you restart the script after you unplug the device.

It might be interesting to change the script to joy1::MsgBox...., to see if this is actually being triggered.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Joy hotkey in #If causing massive cpu spike

Post by doubledave22 » 12 May 2021, 12:00

Well it could be any controller or Joystick I just don't have another to test. But yeah unfortunately I am forced to remove all joystick support from my software for now because of this. Removing the Joy1:: etc hotkeys does stop the lag/high cpu issue but I do think this is somewhat of a problem/bug

I will be investigating alternative ways to add joystick support like evilC's joystick wrapper however I think if the options to add Joy1:: as a hotkey exists natively it shouldn't have this possible issue.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Joy hotkey in #If causing massive cpu spike

Post by mikeyww » 12 May 2021, 12:06

Sure. If you can detect the issue within the script, you can Reload. Others may have additional ideas here.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Joy hotkey in #If causing massive cpu spike

Post by doubledave22 » 13 May 2021, 16:51

So I found this post from @Helgef here: https://www.autohotkey.com/boards/viewtopic.php?t=28889

"I don't know if this is related, but if I recall correctly, there's a bug in windows causing high CPU usage from querying unplugged sticks. Are all sticks plugged in?"

Perhaps its a windows bug and not an ahk bug. Either way I'd love to know how I should proceed.

Here's my options
1) Remove all Joy1::, Joy2:: etc hotkeys from the script and use getkeystate instead. Problem was when hotkeys get initialized next time the script runs without a controller the line: hotkey, Joy1, SomeLabel, On gets ran and triggers the cpu issue. I could do a check if the hotkey name has "Joy" and make sure a joystick is connected but this is getting extreme.

2) I tried evilC's joystickwrapper but my wired PS4 controller couldn't get recognized. I followed all the instructions and got the "No sticks found"error. I thought maybe it was driver related so I grabbed DS4Windows and still no luck. I tried asking in the AHK Discord and I don't want to bump an old evilC thread so maybe someone can point me in the right direction on joystickwrapper usage for PS4 controllers here.

Thanks to anyone who can take a look

edit: I went out an bought an Xbox controller and followed the instructions on a separate PC and still received the "No Sticks found" message. Quite baffled now.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Joy hotkey in #If causing massive cpu spike

Post by boiler » 13 May 2021, 17:55

Try the Joystick Test Script from the AHK documentation’s Script Showcase and see if it recognizes your controller. If not, do you know if the driver for it was successfully installed? It should have automatically found it and installed it when you first plugged it in.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Joy hotkey in #If causing massive cpu spike

Post by doubledave22 » 13 May 2021, 20:49

Yeah everything registers well in the joystick test script and windows picks both controllers just fine. I just cant get the controllers picked up by the joystickwrapper class.

Of note: I think the bug primarily occurs when a game controller is connected at some point then removed, then a script is run with a Joy#:: hotkey or hotkey, Joy#, ..., On but no controller is connected.

I was trying to use evilC's joystickwrapper per the suggestion of the AHK discord as well as it could possibly bypass the need for Joy hotkeys inside AHK.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Joy hotkey in #If causing massive cpu spike

Post by boiler » 14 May 2021, 03:30

I don't know anything about evilC's joystickwrapper class. Perhaps you can see if he would have any insight.

Regarding the CPU problem, I had some software that had some joystick hotkeys, and many users of the software reported the same CPU spike issue, and I was able to reproduce it myself. I'm pretty sure it was exactly what you said -- that the user had a controller connected at some point during their Windows session but no longer had it connected. My solution was to remove controller support just as you had suggested as an option.

I didn't try this, but perhaps the script could only create those hotkeys if it finds a controller connected. I don't mean via an #If directive. I'm thinking you would only create the hotkeys using the Hotkey command if a controller is first determined to exist.
Post Reply

Return to “Ask for Help (v1)”