Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Simple multiple keystroke setup



  • Please log in to reply
9 replies to this topic
manicolo
  • Members
  • 12 posts
  • Last active: Oct 12 2015 07:28 AM
  • Joined: 09 Nov 2013

Hi guys.
I am totally new to this and have been trying to make a simple macro, whereby pressing a particular key, will emulate repetitive keypresses untill the same key is released.
I've did some research, and i came up with the following, but alas ... its not getting the job done

 

 

$Numpad4::

freq:=30
Loop
 
{
 
if getkeystate ("Numpad4","p") = 0
break
send {Numpad4}
Sleep %freq%
 
}
 
 
return
 
This just sends the key off with no way to stop it unless i stop the script.
 
 
P.S I apologize if the quote is in monocolor, I know a lot of your posts are colour coded, but I dont have the application to do that. Please forgive me
 
Thanks
 

 



scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

when you hold down numpad4, what other key you want to be pressed repetitively?



manicolo
  • Members
  • 12 posts
  • Last active: Oct 12 2015 07:28 AM
  • Joined: 09 Nov 2013

Just the numpad4,

Cos as is, when i press it and hold it down, the application im running it on registers it as one keypress, whereas I would like to be able to just press it once (and hold it).

 

Basiically , the current platform i need it on works this way. When I press num4, an ability fires, then there is a small global cooldown before the ability can fire again. When this cooldown passes, you need to press the key again, it being held down doesnt count. As such I need a script which basically makes holding the key down act as if I'm pressing the button repetitively!

 

Thanks again



manicolo
  • Members
  • 12 posts
  • Last active: Oct 12 2015 07:28 AM
  • Joined: 09 Nov 2013

Gonna have to bump this up pls!



scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

you could try this one.. the "sleep, 100" is in there so that numpad4 presses itself every 1/10 of a second. You can change it to "sleep, 500" if you wanted it to press itself every half-second, or "sleep, 1000" for every second, and so on. Or, just remove the "sleep, 100" altogether and it will be like a rapidfire, where it presses itself over and over again with no delay until you release numpad4.

$numpad4::

while GetKeyState("numpad4", "P")
{
sendinput, {numpad4}
sleep, 100
}
return


manicolo
  • Members
  • 12 posts
  • Last active: Oct 12 2015 07:28 AM
  • Joined: 09 Nov 2013

Cheers Scriptor, it works very well and its also very convenient that the speed is adjustable through the sleep.

Thanks alot.

 

 

on a sidenote. What would i need to add , should I need to have it work for more than just one key?

Or should i just make multiple scripts for that?

 

Thanks again



scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

I'm sure there are many ways to do it, but the only way I would know is to do it this way- just put it all in one script:

$numpad4::
while GetKeyState("numpad4", "P")
{
sendinput, {numpad4}
sleep, 100
}
return


$y::
while GetKeyState("y", "P")
{
sendinput, y
sleep, 100
}
return


$a::
while GetKeyState("a", "P")
{
sendinput, a
sleep, 100
}
return


$F3::
while GetKeyState("F3", "P")
{
sendinput, {F3}
sleep, 100
}
return







Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

I think your original code would have been fine except you put a space between GetKeyState and the opening parenthesis (. It ended up evaluating it as an expression I bet. And the passing of a literal string made it so it could never equal zero in the way AHK handles zero and non-zero values.

 

Anyway, for scriptor's most recent post, I would just do this:

#UseHook ; should do the job of $ for all hotkeys, so that may be undesirable. But the code get a bit uglier if we use the $ prefix.
 
Numpad4::
y::
a::
F3::
While GetKeyState(A_ThisHotkey,"p")
{
SendInput, {%A_ThisHotkey%}
Sleep, 100
}
return

If you're interested in the code with the $ prefix (or other prefixes), this is what you'd do:

$Numpad4::
$y::
$a::
$F3::
While GetKeyState(SubStr(A_ThisHotkey,2),"p")
{
SendInput, % "{" SubStr(A_ThisHotkey,2) "}"
Sleep, 100
}
return

The SubStr() function should return the string of A_ThisHotkey from the second character to the end. (In effect stripping the first character, the $. But if you used a hotkey like ~$*F3, then you'd put 4 instead of a 2.)

 

The lone % at the beginning of SendInput tells AHK to interpret the rest of that parameter by evaluating everything as an expression. Anything in quotation marks is treated as a literal string instead of evaluated (but the quotation marks are not in the final output.)

 

Now, the fault of this method is that you must have the same number of modifiers on each hotkey, or else you'd have to go back to scriptor's approach of individual definitions (at least one for each number of modifiers).

 

But! There are functions that can strip out some characters which can be defined as the modifier's character representations. Do that, and life is good. The function I know can do it is RegExReplace() if you replace the characters with nulls or RegExMatch() and you match the substring that would not contain any modifiers, but I am terrible at RegEx so I can't help you there. There might be another function that is slipping my mind, with an Omit Characters parameter.



scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

perfect, Exa - I knew there was a better way ;) I'll be saving this one for future use :)



manicolo
  • Members
  • 12 posts
  • Last active: Oct 12 2015 07:28 AM
  • Joined: 09 Nov 2013
✓  Best Answer

Thanks a lot , both of you :)

You've been of a lot of help!