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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rastahammer
Posts: 22
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.

Code: Select all

#Persistent
#SingleInstance force
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#InstallKeybdHook
#KeyHistory 0
ListLines Off
Process, Priority, , H
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input

$1::		
		While, GetKeyState("1", "P"){
		SendInput {7 Down}{8 Down}{9 Down}
		DllCall("Sleep","UInt",4)
		SendInput {7 Up}{8 Up}{9 Up}
		DllCall("Sleep","UInt",4)

}
return

@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

for k, key in keys
sendinput % "{" key " down}"
dllcall("Sleep", "uint", duration)
for k, key in keys
sendinput % "{" key " up}"
	}
}

Numpad3::
    Reload
  return

$*Ins::Suspend

Code: Select all

#Persistent
#SingleInstance force
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#InstallKeybdHook
#KeyHistory 0
ListLines Off
Process, Priority, , H
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input

$1::		
		if GetKeyState("ScrollLock", "T"){
		While, GetKeyState("1", "P"){
		SendInput 789
		DllCall("Sleep","UInt",5)
	}

}
return

Numpad3::
    Reload
  return

$*Ins::Suspend
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

SetKeyDelay, -1, -1
ListLines Off
Process, Priority, , H


$1::		
		if GetKeyState("ScrollLock", "T"){
		While, GetKeyState("1", "P"){
		SendInput 789
		DllCall("Sleep","UInt",2)
	}

}
return
Last edited by Nextron on 25 Jan 2018, 19:17, edited 7 times in total.
Reason: Nextron: Restored some content
BoBo
Posts: 6564
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: 22
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: 22
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: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

17 Dec 2017, 18:20

It’s possible your loops are not being terminated...
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Rastahammer
Posts: 22
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: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
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

+a::
flag := 0
Loop
{
	;set flag
	flag := 1
	;do stuff
	;....
	;check for flag
	if (!flag)
		break
}
return
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Rastahammer
Posts: 22
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
Contact:

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

#Persistent
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Recommended for catching common errors.
;#InstallKeybdHook ;https://autohotkey.com/docs/commands/_InstallKeybdHook.htm
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , H
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2
SetTitleMatchMode, Fast

$1::
while GetKeyState("ScrollLock", "T"){
	SendInput 109
	DllCall("Sleep","UInt",1)
}
return

$e::
while GetKeyState("ScrollLock", "T"){
	SendInput e
	DllCall("Sleep","UInt",1)
}
return

3::
while GetKeyState("ScrollLock", "T"){
	SendInput {RAW}-
	DllCall("Sleep","UInt",1)
}
return

*Ins::Suspend
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

if (GetKeyState("ScrollLock", "T")){
	msgbox, ScrollLock Is enabled!
} else {
	msgbox, ScrollLock Is not enabled!
}
Rastahammer
Posts: 22
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
Contact:

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

#Persistent
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Recommended for catching common errors.
;#InstallKeybdHook ;https://autohotkey.com/docs/commands/_InstallKeybdHook.htm
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , H
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2
SetTitleMatchMode, Fast

btn1 := 0
btnE := 0
btn3 := 0

loop
{
	while GetKeyState("ScrollLock", "T"){
		if (btn1){
			SendInput 109
		}
		if (btnE){
			SendInput e
		}
		if (btn3){
			SendInput {RAW}-
		}
		DllCall("Sleep","UInt",1)
	}
}

1::
	btn1 := btn1=0 ? 1 : 0
return

e::
	btnE := btnE=0 ? 1 : 0
return

3::
	btn3 := btn3=0 ? 1 : 0
return

*Ins::Suspend
geekgarage
Posts: 22
Joined: 17 Dec 2017, 11:03
Contact:

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
Contact:

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

#Persistent
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Recommended for catching common errors.
;#InstallKeybdHook ;https://autohotkey.com/docs/commands/_InstallKeybdHook.htm
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , H
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2
SetTitleMatchMode, Fast

~1::
while GetKeyState("ScrollLock", "T"){
	while GetKeyState("1", "D"){
		SendInput 109
		DllCall("Sleep","UInt",1)
	}
}
return

~e::
while GetKeyState("ScrollLock", "T"){
	while GetKeyState("e", "D"){
		SendInput e
		DllCall("Sleep","UInt",1)
	}
}
return

3::
while GetKeyState("ScrollLock", "T"){
	while GetKeyState("3", "D"){
		SendInput {RAW}-
		DllCall("Sleep","UInt",1)
	}
}
return

*Ins::Suspend
Rastahammer
Posts: 22
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
Contact:

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: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
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??
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Rastahammer
Posts: 22
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: 22
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
Contact:

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

18 Dec 2017, 15:55

Rastahammer wrote:Solved!

Code: Select all

while GetKeyState("ScrollLock", "T"){
	while GetKeyState("1", "D"){
		SendInput 09
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

#Persistent
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Recommended for catching common errors.
;#InstallKeybdHook ;https://autohotkey.com/docs/commands/_InstallKeybdHook.htm
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , H
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2
SetTitleMatchMode, Fast

$1::
if GetKeyState("ScrollLock", "T"){
	while GetKeyState("1", "P"){
		SendInput 109
		DllCall("Sleep","UInt",8)
	}
} else {
	SendInput 1
}
return

$e::
if GetKeyState("ScrollLock", "T"){
	while GetKeyState("e", "P"){
		SendInput e
		DllCall("Sleep","UInt",8)
	}
} else {
	SendInput e
}
return

$3::
if GetKeyState("ScrollLock", "T"){
	while GetKeyState("3", "P"){
		SendInput -
		DllCall("Sleep","UInt",8)
	}
} else {
	SendInput 3
}
return

*Ins::Suspend

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 133 guests