Code Needs a tweak, and im not sure what to do (Holding Keys) Topic is solved

Ask gaming related questions (AHK v1.1 and older)
badnecros
Posts: 19
Joined: 11 Nov 2016, 17:21

Code Needs a tweak, and im not sure what to do (Holding Keys)

17 Nov 2016, 14:52

So! im playing diablo, and i dont have a NumPad on my laptop, so im trying to creat a hotkey to synthisize the autopress action the numbpad serves.

in essence, press a button to toggle a button to be held down
there are 4 buttons, in which i need to seperately choose which to hold, and which to manually press.
QWER are the buttons, which need to be activated.

i have gotten them working seperately.
the issue i have, is if i try to have E, as well as R running at the same time, it does not work.
since it is still registering a hold from the first one, the second will not start, until i deactivate the first.

thank you so much for any help!

The code is as follows;

Code: Select all

;;#######################
;;##### HOLD Q Down #####
;;#######################
1::
Loop
    {
        If A_Index = 1
            Sleep, 500
        GetKeyState, State, 1, P
        If State = D
            Break
        Send {q Down}
    }
Send {q Up}
Sleep, 500
Return


;;#######################
;;##### HOLD W Down #####
;;#######################
2::
Loop
    {
        If A_Index = 1
            Sleep, 500
        GetKeyState, State, 2, P
        If State = D
            Break
        Send {w Down}
    }
Send {w Up}
Sleep, 500
Return


;;#######################
;;##### HOLD E Down #####
;;#######################
3::
Loop
    {
        If A_Index = 1
            Sleep, 500
        GetKeyState, State, 3, P
        If State = D
            Break
        Send {e Down}
    }
Send {e Up}
Sleep, 500
Return


;;#######################
;;##### HOLD R Down #####
;;#######################
4::
Loop
    {
        If A_Index = 1
            Sleep, 500
        GetKeyState, State, 4, P
        If State = D
            Break
        Send {r Down}
    }
Send {r Up}
Sleep, 500
Return
badnecros
Posts: 19
Joined: 11 Nov 2016, 17:21

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

17 Nov 2016, 16:35

evilC wrote:The GetKeyState technique of detecting key events only works for one hotkey at a time.
See here: https://autohotkey.com/boards/viewtopic.php?f=7&t=19745
Thats great information.
would you be able to show me how that intigrates into my code in order to toggle the up/down of my keys? ive never used the tilde before, and im relatively new to this.

to be clear, i need a code that will allow me to toggle a key, or use it as an active. and i need to choose multiple keys to do that with, sometimes multiple combinations of keys
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

17 Nov 2016, 16:51

Code: Select all

keyheld := 0

; Do this on PRESS of key
F12::
	keyheld := 1
	Loop {
		if (keyheld == 0)	; Up Event hotkey set keyheld to 0 - END!
			break
		; Put your code here that needs to repeat
		Tooltip % A_TickCount	; Just some code so you can see something is happening
		Sleep 100	; Always use a sleep of at least 10 in a loop like this, else you will use lots of CPU!
	}
	return

; Do this on RELEASE of key
F12 up::
	keyheld := 0
	return
badnecros
Posts: 19
Joined: 11 Nov 2016, 17:21

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

17 Nov 2016, 16:54

but i dont want ANYTHING to happen on the release of the key. i want a second press to turn the toggle off

P.S. Thank you for all the help
badnecros
Posts: 19
Joined: 11 Nov 2016, 17:21

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

17 Nov 2016, 16:57

so pretty much i want a full Down/Up of the button "1" to hold "Q" down, until i press "1" down/up again
however, at the same time, i want a full Down/Up of the button "2" to hold "W" down, until i press "2" down/up again,

the issue is, they both cannot go at the same time, and thats what i need bypassed.

perhaps, should i make a loop, where instead of holding it, it just spams the button?


########EDIT###########

Even making it loop infinitely by spamming the key, with a hotkey to pause the script doesnt work. it will only run one at a time.
User avatar
eventhorizon
Posts: 158
Joined: 27 Oct 2016, 14:22

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)  Topic is solved

17 Nov 2016, 21:55

Since computers are really stupid, but supremely fast they can only do one thing at a time very quickly. given what you said about trying to toggle your qwer keys we can get one key at a time but we can combine them to send one after the other. I don't know if that will solve the
problem you're facing but here is some code i worked up that might help to that end.

Code: Select all

; set all the toggles to 0 first
qtoggle := 0
wtoggle := 0
etoggle := 0
rtoggle := 0

;loop through the keys to send
Loop
{	gosub SendMyKeys
}
return

esc:: ;exit from the script
	ExitApp
	
1:: ;set the q toggle
	keywait,1
	qtoggle := !qtoggle
	;msgbox,0x1000,,qtoggle = %qtoggle%
	return
2:: ;set the w toggle
	keywait,2
	wtoggle := !wtoggle
	;msgbox,0x1000,,wtoggle = %wtoggle%
	return
3:: ;set the e toggle
	keywait,3
	etoggle := !etoggle
	;msgbox,0x1000,,etoggle = %etoggle%
	return
4:: ;set the r toggle
	keywait,4
	rtoggle := !rtoggle
	;msgbox,0x1000,,rtoggle = %rtoggle%
	return

;this sends the keys one after the other with a 10ms delay to allow the
;keys to settle; the loop above will keep this thing running even if no
;toggles are active. You may want to find another way to activate the 
;SendMyKeys subroutine so you can use other keys too but this is just a
;proof of concept.
SendMyKeys:
	if (qtoggle)
	{	send, q
		sleep 10
	}
	if (wtoggle)
	{	send, w
		sleep 10
	}
	if (etoggle)
	{	send, e
		sleep 10
	}
	if (rtoggle)
	{	send, r
		sleep 10
	}
	return

A computer lets you make more mistakes faster than any invention in human history – with the possible exceptions of handguns and tequila.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

18 Nov 2016, 01:17

Put each hotkey from your first post in a separate script. You can make one script to start and close them all if you want.
badnecros
Posts: 19
Joined: 11 Nov 2016, 17:21

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

18 Nov 2016, 10:05

eventhorizon wrote:Since computers are really stupid, but supremely fast they can only do one thing at a time very quickly. given what you said about trying to toggle your qwer keys we can get one key at a time but we can combine them to send one after the other. I don't know if that will solve the
problem you're facing but here is some code i worked up that might help to that end.

Code: Select all

; set all the toggles to 0 first
qtoggle := 0
wtoggle := 0
etoggle := 0
rtoggle := 0

;loop through the keys to send
Loop
{	gosub SendMyKeys
}
return

esc:: ;exit from the script
	ExitApp
	
1:: ;set the q toggle
	keywait,1
	qtoggle := !qtoggle
	;msgbox,0x1000,,qtoggle = %qtoggle%
	return
2:: ;set the w toggle
	keywait,2
	wtoggle := !wtoggle
	;msgbox,0x1000,,wtoggle = %wtoggle%
	return
3:: ;set the e toggle
	keywait,3
	etoggle := !etoggle
	;msgbox,0x1000,,etoggle = %etoggle%
	return
4:: ;set the r toggle
	keywait,4
	rtoggle := !rtoggle
	;msgbox,0x1000,,rtoggle = %rtoggle%
	return

;this sends the keys one after the other with a 10ms delay to allow the
;keys to settle; the loop above will keep this thing running even if no
;toggles are active. You may want to find another way to activate the 
;SendMyKeys subroutine so you can use other keys too but this is just a
;proof of concept.
SendMyKeys:
	if (qtoggle)
	{	send, q
		sleep 10
	}
	if (wtoggle)
	{	send, w
		sleep 10
	}
	if (etoggle)
	{	send, e
		sleep 10
	}
	if (rtoggle)
	{	send, r
		sleep 10
	}
	return

the input is much appreciated. ill be able to test the script to seeif it is what i need in a few hours, and ill get back to you. ^.^
badnecros
Posts: 19
Joined: 11 Nov 2016, 17:21

Re: Code Needs a tweak, and im not sure what to do (Holding Keys)

18 Nov 2016, 15:44

eventhorizon wrote:Since computers are really stupid, but supremely fast they can only do one thing at a time very quickly. given what you said about trying to toggle your qwer keys we can get one key at a time but we can combine them to send one after the other. I don't know if that will solve the
problem you're facing but here is some code i worked up that might help to that end.

Code: Select all

; set all the toggles to 0 first
qtoggle := 0
wtoggle := 0
etoggle := 0
rtoggle := 0

;loop through the keys to send
Loop
{	gosub SendMyKeys
}
return

esc:: ;exit from the script
	ExitApp
	
1:: ;set the q toggle
	keywait,1
	qtoggle := !qtoggle
	;msgbox,0x1000,,qtoggle = %qtoggle%
	return
2:: ;set the w toggle
	keywait,2
	wtoggle := !wtoggle
	;msgbox,0x1000,,wtoggle = %wtoggle%
	return
3:: ;set the e toggle
	keywait,3
	etoggle := !etoggle
	;msgbox,0x1000,,etoggle = %etoggle%
	return
4:: ;set the r toggle
	keywait,4
	rtoggle := !rtoggle
	;msgbox,0x1000,,rtoggle = %rtoggle%
	return

;this sends the keys one after the other with a 10ms delay to allow the
;keys to settle; the loop above will keep this thing running even if no
;toggles are active. You may want to find another way to activate the 
;SendMyKeys subroutine so you can use other keys too but this is just a
;proof of concept.
SendMyKeys:
	if (qtoggle)
	{	send, q
		sleep 10
	}
	if (wtoggle)
	{	send, w
		sleep 10
	}
	if (etoggle)
	{	send, e
		sleep 10
	}
	if (rtoggle)
	{	send, r
		sleep 10
	}
	return

OMG I LOVE YOU!!! THIS IS WHAT I NEEDED!

Thank you for everyones assistance, i learned a lot of new things here, and i appreciate it all. ^.^

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 51 guests