Jump to content

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

Do something while key is held down



  • Please log in to reply
3 replies to this topic
Lord123
  • Members
  • 7 posts
  • Last active: Mar 26 2015 03:53 AM
  • Joined: 11 Oct 2014

This is my script so far:

RShift::
If(GetKeyState("RShift", "P" = 1)
Send {z}
else
Send {RShift}

The problem I have right now is that when the shift key is held down, the program keep sending "z". What can be changed so that when the shift key is pressed and held indefinately, it will only send "z" once. Then press the same shift key when the key is released.



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011

to tell if a key is held down or not you have to decided how long "held down" is? I used 100ms in this example.

RShift::
now := A_TickCount
while GetKeyState("RShift", "P")
	if (A_TickCount-now > 100)
	{
		Send z
		KeyWait, RShift
		return
	}
Send {RShift}
return


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

Well, your GetKeyState command wouldn't have the = 1 part in it; kind of surprised it works.

 

Anyway, of course it will only send z. You're pressing the hotkey, and then checking if you pressed the hotkey, and deciding to send z if you did press the hotkey. There's now way you can press the hotkey and not press it to send a simple RShift. This can be resolved however you want - change the If statement to something that could evaluate false.

 

On top of that, it's possible your RShift is recursing (right word?) into activating the hotkey. So you won't ever see a net RShift, but can only ever get the end result of a z. This can be resolved by using the $ modifier. The issue is though if you intend for autohotkey to be able to activate its own hotkeys... But that doesn't make much sense, you'll get a net RShift by using a $RShift hotkey otherwise.

 

Also, include a return statement.

 


What can be changed so that when the shift key is pressed and held indefinately, it will only send "z" once. Then press the same shift key when the key is released.

 

I think you're looking for KeyWait.

 

$RShift::
Send z
KeyWait, RShift
Send {RShift}
return


Lord123
  • Members
  • 7 posts
  • Last active: Mar 26 2015 03:53 AM
  • Joined: 11 Oct 2014
I think you're looking for KeyWait.
$RShift::
Send z
KeyWait, RShift
Send {RShift}
return

 

 

Yes, this works perfectly. I wasn't aware there was a KeyWait function.