Simple loop script (keypress)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
merzibow
Posts: 2
Joined: 22 Aug 2018, 05:15

Simple loop script (keypress)

22 Aug 2018, 05:20

Hi All!

As i am very new to this, I am asking for help. Trying to create a very simple keypress script (hold down key spams key until released). I have gotten the following to work, but i would like to also use keys such as F,E and so on.

"#ifWinActive World of Warcraft
{
$1::
Loop
{
if not GetKeyState("1", "P")
break
Send 1
sleep 1
}
return
$2::
Loop
{
if not GetKeyState("2", "P")
break
Send 2
sleep 1
}
return
$3::
Loop
{
if not GetKeyState("3", "P")
break
Send 3
sleep 1
}
return
$4::
Loop
{
if not GetKeyState("4", "P")
break
Send 4
sleep 1
}
return
$5::
Loop
{
if not GetKeyState("5", "P")
break
Send 5
sleep 1
}
return
$6::
Loop
{
if not GetKeyState("6", "P")
break
Send 6
sleep 1
}
return
$7::
Loop
{
if not GetKeyState("7", "P")
break
Send 7
sleep 1
}
return
}"


I have tried the following, as an example (looping the key F), with no success:

$F::
Loop
{
if not GetKeyState("F", "P")
break
Send F
sleep 1
}
return


Hope somewhat can help! Thanks!
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Simple loop script (keypress)

22 Aug 2018, 06:10

Hi,
don't use F, E etc.
Use f, e etc.
AHK converts F automatically to Shift+f.

And please use the code-tag when adding codes.
You can try this one:

Code: Select all

$f::
Loop
{
  if not GetKeyState("F", "P")
  {
    break
  }  
  Send f
  Sleep 50
}
return
I think this one will work as well:

Code: Select all

$f::
while GetKeyState("F", "P")
{
  Send f
  Sleep, 50
}
return
P.S: Sleep times like 1 ms make no sense in my opinion.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
merzibow
Posts: 2
Joined: 22 Aug 2018, 05:15

Re: Simple loop script (keypress)

22 Aug 2018, 06:42

Thank you so much, Scr1pter, for your prompt and helpful reply.

Another quick question. Would it be possible to add a "pause" command to the script, and if so, can i bind it to mousekey 5? I believe the key is named XButton2 in AHK. I have tried the following with no success:

""XButton 2::
Suspend
Pause,,1
return""
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Re: Simple loop script (keypress)

22 Aug 2018, 06:49

This should work for you:

Code: Select all

XButton2:: Pause
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Simple loop script (keypress)

23 Aug 2018, 04:32

Hi,
These two scripts work for me.
However, I don't know if they are "best practice".

Anyway, I suggest you not to work with XButton.
I, for example, always remap my mouse buttons within my Logitech Gaming Software.
If you have 8 extra mouse buttons for example, you can map them to Numpad keys.
Then, instead of working with XButton, you can work with Numpad keys.

Code: Select all

$f::
if pause = false
{
  while GetKeyState("F", "P")
  {
    Send f
    Sleep, 50
  }
}
return

XButton2::
while GetKeyState("XButton2", "P")
{
  pause = true
}
pause = false
return
I tried it with rbutton by the way, not with xbutton2.

P.S. And remember to use the code-tags ;)

Image

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, imbelicalcord, jollyjoe, Rohwedder and 225 guests