[Help] While GetKeyState RButton dont work Topic is solved

Ask gaming related questions (AHK v1.1 and older)
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

[Help] While GetKeyState RButton dont work

23 Jan 2024, 14:03

RButton Up:: this work, at least that what I see :oops:
RButton:: wont change my XButton1 to a different key while I hold RButton :(

Code: Select all

~RButton::RButton
  While GetKeyState("RButton","P")
	{
		XButton1::l
	}
Return
  
RButton Up::
	{
	XButton1::
		While GetKeyState("XButton1","P")
		{
		Send {XButton1}
		Sleep rand
		Send {y}
		Sleep rand2
		}
	return
	}
Return
User avatar
boiler
Posts: 17332
Joined: 21 Dec 2014, 02:44

Re: [Help] While GetKeyState RButton dont work

23 Jan 2024, 21:46

There are several things that are fundamentally wrong in this script. I’ll add comments with questions for you to answer.

Code: Select all

~RButton::RButton ; what specifically do you expect this line to do?
  While GetKeyState("RButton","P") ; when do you expect this line to execute?
	{
		XButton1::l ; this is not how hotkeys/remaps are made conditional. are you aware of the #If directive?
	}
Return
  
RButton Up::
	{
	XButton1:: ; what is putting this line inside a code block after RButton Up supposed to accomplish?
		While GetKeyState("XButton1","P")
		{
		Send {XButton1}
		Sleep rand ; what is the value of rand?  where does it come from?
		Send {y} ; what is the reason for the { } around the y?
		Sleep rand2 ; what is the value of rand2?  where does it come from?
		}
	return
	}
Return
[/quote]
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 01:00

boiler wrote:
23 Jan 2024, 21:46
There are several things that are fundamentally wrong in this script. I’ll add comments with questions for you to answer.
And Ill add answers ;)

Code: Select all

Random, rand, 20, 80
Random, rand2, 30, 60

~RButton::RButton ; what specifically do you expect this line to do? - I need RButton to still work as usually. 
  While GetKeyState("RButton","P") ; when do you expect this line to execute? - while I hold RButton. 
	{
		XButton1::l ; this is not how hotkeys/remaps are made conditional. are you aware of the #If directive? - never did anything with #if but would happily try it. 
	}
Return
  
RButton Up::
	{
	XButton1:: ; what is putting this line inside a code block after RButton Up supposed to accomplish? - when the RButton is Up I want XButton1 to have other script. 
		While GetKeyState("XButton1","P")
		{
		Send {XButton1}
		Sleep rand ; what is the value of rand?  where does it come from? - sorry I have cropped my code and forgot to include that part
		Send {y} ; what is the reason for the { } around the y? - I am a newbie in AHK is this a good reason? 
		Sleep rand2 ; what is the value of rand2?  where does it come from? - I have cropped my code and forgot to include that part
		}
	return
	}
Return

[Mod edit: Fixed quote tags so it doesn’t look like WekizZ‘s entire reply is part of a quote by boiler.]
User avatar
boiler
Posts: 17332
Joined: 21 Dec 2014, 02:44

Re: [Help] While GetKeyState RButton dont work  Topic is solved

24 Jan 2024, 02:17

Line 4: That’s what the ~ does. When you put the RButton on the end, you make it a single~line key remap, and none of the code below it is associated with it.

Line 5 - It never executes. Because of what you did on line 4, this is stranded code that is not associated with anything.

Line 7 - The #If directive is how you make hotkeys/remaps conditional, so read about it and look at the examples. You can’t put key remaps inside of regular blocks of code like what you showed. The while has no effect on it.

Line 13 - That’s not how it works. Again, hotkeys are made conditional using the #If directive.

Lines 17 and 19 - Where you put the Random commands, they will not be random each time but will have the same values the whole time the script is running. Is that what you want?

Line 18 - Regular single-letter key names are not to be included in braces except for a specific reason. From the Send documentation:
The characters {} are used to enclose key names and other options, and to send special characters literally. For example, {Tab} is Tab and {!} is a literal exclamation mark.

[v1.1.27+]: Enclosing a plain ASCII letter (a-z or A-Z) in braces forces it to be sent as the corresponding virtual keycode, even if the character does not exist on the current keyboard layout. In other words, Send a produces the letter "a" while Send {a} may or may not produce "a", depending on the keyboard layout. For details, see the remarks below.
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 06:10

Thanks for a detailed explanation @boiler. And as I said, most of the things I am doing in AHK I don't understand, could be my ADD. I basically take codes I find on the internet and try to put them together. I will definitely try to follow your guides. But don't be surprised if I will come up with something as bad as this one is XD
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 06:54

@boiler - I think I got something to work as I want, but random sleep timer does not work as I was expecting. I need it to be random every loop, but what I see it is random only first loop.
I don't have a mouse with XButtons right now, so I am using RCTRL instead.
Also this code works in notepad as intended, still haven't tested in game.

Code: Select all

Random rand, 15, 500

~RCTRL::
    if (GetKeyState("RButton", "P"))
    {
        SendInput, u
    }
    else
    {
		{
		Send, z
		Sleep %rand%
		Send, y
		}
	return
    }
return
User avatar
boiler
Posts: 17332
Joined: 21 Dec 2014, 02:44

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 07:25

To get a new random number every time, put the Random command inside the hotkey routine.
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 07:40

It looks like this works :dance:
Now I need to do some in game testing :beer:

Code: Select all

~RCTRL::
    if (GetKeyState("RButton", "P"))
    {
        SendInput, u
    }
    else
    {
		{
		Random rand, 15, 500
		Send, z
		Sleep %rand%
		Send, y
		}
	return
    }
return
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 11:18

@boiler strange, on my gaming pc this code don't loop. Am I missing something?

Code: Select all

~XButton1::
    if (GetKeyState("RButton", "P")) ;this only triggers one click, I expect it to work like a normal button, like if I hold, u should hold. 
    {
        SendInput, u
    }
    else
    {
		{
		Random rand, 15, 135 ; same with this only triggers one time, I expect it to auto click.
		Send, {XButton1}
		Sleep %rand%
		Send, y
		}
	return
    }
return
User avatar
boiler
Posts: 17332
Joined: 21 Dec 2014, 02:44

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 11:44

WekizZ wrote:
24 Jan 2024, 11:18
Am I missing something?
Yes, you're missing a loop of any kind. There is nothing in that code that would cause it to loop. If gets evaluated one time. You could replace it with while, but there is no else associated with while, so you'd have to adjust your logic.
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: [Help] While GetKeyState RButton dont work

24 Jan 2024, 12:09

boiler wrote:
24 Jan 2024, 11:44
WekizZ wrote:
24 Jan 2024, 11:18
Am I missing something?
Yes, you're missing a loop of any kind. There is nothing in that code that would cause it to loop. If gets evaluated one time. You could replace it with while, but there is no else associated with while, so you'd have to adjust your logic.
I did it! I added a loop where I need, and left just a click where I don't. And tbf I don't even know how, but this works!
Let me know if you see anything weird :lol:
This is my full code for now:

Code: Select all

~XButton1::
    if (GetKeyState("RButton", "P"))
    {
        SendInput, u
    }
    else
    {
	Loop {
		{
		Random rand, 15, 135
		Send, {XButton1}
		Send, y
		Sleep %rand%
		}
		} Until (!GetKeyState("XButton1", "P"))
    }
return

XButton2::
    if (GetKeyState("RButton", "P"))
    {
        SendInput, i
    }
    else
    {
		Send, {XButton2}
    }
return

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: ItzAd4m and 34 guests