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

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

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

17 Dec 2017, 13:52

Mod edit:
Topic starter thought is necessary to remove the content of his posts.
For reference, his initial script kept looping its output even after releasing the triggering key and some posts were:


Sorry guys! I don't know what to do. This script keeps getting stuck on an infinite loop and I really can't fix this.

Sometimes it gets stuck in the loop after I release the key and continuously sends the hotkeys until I press the key 1 again or reload the script.

Please, I need some help.


@Helgef

Thank you so much for your time trying to help me. I really appreciate your help. But even using loop 1, it's very slow compared to what I'm using.
I tried this:

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





The faster it is, the better the performance in the game. I have tested it several times and the script needs to be very fast. The script I'm using, has the speed I need. But it keeps getting stuck in an infinite loop.

Anyway, thank you very much!


Nextron, I guess that you found the problem man!

I'm testing this:

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

Last edited by Nextron on 25 Jan 2018, 19:17, edited 7 times in total.
Reason: Nextron: Restored some content
User avatar
joedf
Posts: 6344
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: 2515
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: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 15:41

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:16, edited 4 times in total.
Rastahammer
Posts: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 15:43

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:16, edited 1 time in total.
User avatar
joedf
Posts: 6344
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: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 18:39

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:15, edited 1 time in total.
User avatar
joedf
Posts: 6344
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: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 19:37

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:16, edited 2 times in total.
geekgarage
Posts: 22
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: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 20:09

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:16, edited 2 times in total.
geekgarage
Posts: 22
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: 22
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: 22
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: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 20:42

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:17, edited 2 times in total.
geekgarage
Posts: 22
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: 6344
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: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 21:01

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:17, edited 2 times in total.
Rastahammer
Posts: 23
Joined: 17 Dec 2017, 13:09

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

17 Dec 2017, 21:40

Solved.
Last edited by Rastahammer on 25 Jan 2018, 17:17, edited 2 times in total.
geekgarage
Posts: 22
Joined: 17 Dec 2017, 11:03
GitHub: geekgarage

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

18 Dec 2017, 15:55

Rastahammer wrote:Solved!



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


You are very welcome :)

I've optimized it a bit further and now you can also use the keys as normal keys when scroll lock isn't enabled (so you can write in in-game chat and such. Be aware that if you set the delay timer lower than 8 ms you can experience that it's not jumping out of the while loop due to the speed of GetKeyState (this is also an issue in the other versions of the script and any script that uses GetKeyState. Not only that but by changing the outer while to IF you can now repeat use hotkeys without having to disable scroll lock after first use, that was a mistake from my side, sry about that, but it is fixed now.
If you run it with lower delay than 8ms and you get that issue, by tapping one of the keys REALLY fast as that is the only way to trigger it, you just press the same key again and it stops, but by using the 8ms delay in the loop this should be gone and it would still represent a single key being pushed 125 times a second, and that is just ridiculous many keys, now with the hotkey 1 that sends 3 character that would be 375 keys a second!

But here is the code

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


Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bad husband, Divvy and 64 guests