[Function] GetKeyState isn't working! Please, help!

Get help with using AutoHotkey and its commands and hotkeys
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

[Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 13:52

This is my code and most of the time it get stuck spamming the hotkeys (it gets stuck sending the hotkeys even with the key not pressed). GetKeyState (if not GetKeyState("1", "P") isn't working! I'm trying to fix this for a long time and I can't fix it.

If I change DllCall("Sleep","UInt",1) to DllCall("Sleep","UInt",5) the problem disapears. But the script get slower and I need it to be as fast as possible. PLease, I need some help.

Thank you!

EDIT: Is getting stuck even with 5 :(

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by Rastahammer on 17 Dec 2017, 16:04, edited 3 times in total.
User avatar
joedf
Posts: 6101
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 15:27

whats wrong with Sleep ?
BoBo
Posts: 1870
Joined: 13 May 2014, 17:15

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 15:32

@ Rastahammer
The following statement points out when to post at "Scripts & Functions" (what you've done before we had to forward your thread to this section on your behalf) :eh: ...

Scripts and Functions
Post your working scripts, libraries and tools
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 15:41

joedf wrote:whats wrong with Sleep ?


No problem. If I set the Sleep time to 5, the script slows down and doesn't get stuck anymore. But I'm using to play MMORPG and decreases my performance using 5. The difference is significant.

EDIT: Is getting stuck even with 5 :(
Last edited by Rastahammer on 17 Dec 2017, 16:03, edited 2 times in total.
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 15:43

BoBo wrote:@ Rastahammer
The following statement points out when to post at "Scripts & Functions" (what you've done before we had to forward your thread to this section on your behalf) :eh: ...

Scripts and Functions
Post your working scripts, libraries and tools


Sorry!
User avatar
joedf
Posts: 6101
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 18:20

It’s possible your loops are not being terminated...
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 18:39

joedf wrote:It’s possible your loops are not being terminated...


True! Do you have any idea to fix it?
User avatar
joedf
Posts: 6101
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 19:10

Maybe use a global flag, where each press resets the flags?
something like

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 19:37

I tried this and got stuck (spamming) sometimes. Same problem.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

geekgarage
Posts: 13
Joined: 17 Dec 2017, 11:03
GitHub: geekgarage

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 19:45

removed your basic loop and replaced them with a while loop so the code is optimized. tested and working on my system. Also you should use SendInput instead of send as it is faster. i get that by setting SendMode to input that you are using SendInput by writing Send, but in case that sendMode isn't loaded correct you have issues

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



As a side note, when using some sort of test operation, use brackets, not using it is the old way of coding AHK and i've seen it cause issues with code
do this instead

Code: [Select all] [Download] GeSHi © Codebox Plus

if (GetKeyState("ScrollLock", "T")){
msgbox, ScrollLock Is enabled!
} else {
msgbox, ScrollLock Is not enabled!
}
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:09

@geekgarage

Thank you for your time trying to help me.

But with your script, the only way to stop the loop is pressing scrolllock. And like I said, I'm using this script for gaming. And this way isn't viable to play a MMORPG. The best way is when I "release" the key and stops the loop.
geekgarage
Posts: 13
Joined: 17 Dec 2017, 11:03
GitHub: geekgarage

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:12

an even better version would be where you can toggle it on and off and i've attached it below, i would recommend that you play with the single delay as it is CPU intensive as you are sending about 4000 characters a second if all is activated

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

geekgarage
Posts: 13
Joined: 17 Dec 2017, 11:03
GitHub: geekgarage

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:14

Rastahammer wrote:@geekgarage

Thank you for your time trying to help me.

But with your script, the only way to stop the loop is pressing scrolllock. And like I said, I'm using this script for gaming. And this way isn't viable to play a MMORPG. The best way is when I "release" the key and stops the loop.

well i've given you two versions of the same script, from here if you mix them with a WHILE for each key as in the first script with the loop in the second, you will be able to do what you want. Or you could just nest the while loops :)
geekgarage
Posts: 13
Joined: 17 Dec 2017, 11:03
GitHub: geekgarage

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:27

Just in case you still cant figure it out :)
By using ~ in front it allows for the key to be used as the normal key while also triggering the hotkey
Using ~ also imply $ by default so you don't need to add $~ to be able to send the same key that you are using for hotkey, in the script
as 3 is not sent by it's own hotkey i just use the normal 3 as hotkey without any operators in front of it
you could also change the outer while loop for each key to an IF statement for the ScrollLock, this way this test will only be run when you hit the hotkey. This way it's a bit more optimized (less check) but you won't be able to stop this script while you are holding one of the hotkeys.

With the below script you are able to stop the script by releasing either the hotkey or press ScrollLock while holding any of the hotkeys. if you change the outer while loop to an IF statement you will only be able to break the loop by releasing the hotkey

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:42

Something is wrong with this script. When I press "3" and send (RAW -), hotkey "1" that would send (109) stops working.

And the loop gets much slower by using ~

I don't know why.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

geekgarage
Posts: 13
Joined: 17 Dec 2017, 11:03
GitHub: geekgarage

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:48

Well time for you to mix the scripts then, i'm on my way to bed, and I might have a look at it tomorrow, nothing is wrong with it, it's just not working as you expected, it might be time for you to really describe how you want it to work ;) I've given you 3 different versions that all have their good and bad sides, and you can't get it all, compromises will always have to be made.
User avatar
joedf
Posts: 6101
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 20:58

So what is this script trying to achieve? Just remap some keys??
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 21:01

@geekgarage
I really appreciate it! Thank you for your time trying to help. But isn't working.
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 21:08

joedf wrote:So what is this script trying to achieve? Just remap some keys??


Joedf, I said on my first post. I'm trying to stop the infinite loop. Like I said on my first post, I'm getting stuck spamming the loop without break. EVEN using getkeystate.
Rastahammer
Posts: 15
Joined: 17 Dec 2017, 13:09

Re: [Function] GetKeyState isn't working! Please, help!

17 Dec 2017, 21:40

Solved!



Thank you @geekgarage! I removed the key "1" and now it's working like a charm!

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], Esoeth and 42 guests