Jump to content

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

Fast way to detect double key presses



  • Please log in to reply
4 replies to this topic
vito
  • Members
  • 2 posts
  • Last active: Mar 21 2014 01:45 PM
  • Joined: 15 Mar 2014

Hi :)
I am new to autohotkey and I have been trying to find a way to detect double key presses.
I know this had been asked before but my question is a bit different!
When I press an "a" once it should write "a" but when I press it twice it should write "Hello World!". That sounds easy but there where two Problems with all approaches I found:
- The Problem with the fist solution I found was that when I define a hotkey for a double pressed a the single pressed a will be delayed. So if I want to write "as" very fast it will write "sa", because the a is delayed.
- The second solution I found always sent an a before sending "Hello World" so a double pressed a will lead to "aHelloWorld" but it should just Send "Hello Word"

So is there a different solution?
I spend already 12 hours to fix it! Please help me!

===================

PS:(I tried to just send a backspace when the a is hit the second time, but that does not work for keys like Escape because the Escape will always been send to the program and close the window)



tyrer
  • Members
  • 270 posts
  • Last active: Apr 10 2015 05:10 PM
  • Joined: 23 Sep 2012
~$a::
KeyWait, a, U
KeyWait, a, D, T0.2
If (ErrorLevel = 0)
send, {BS 2}hello world
return


duppy
  • Members
  • 13 posts
  • Last active: Oct 31 2015 02:14 AM
  • Joined: 16 May 2013

Here's one based on the example in the manual...seems to work for me.

~a::
	if (A_PriorHotkey <> "~a" or A_TimeSincePriorHotkey > 200)
	{
		KeyWait, a
		return
	}
	Send {backspace 2}Hello World!
return


hachi
  • Members
  • 330 posts
  • Last active: Jul 14 2015 02:09 AM
  • Joined: 24 Jan 2010
✓  Best Answer

why not use a hotstring, that's what they're for

:*:aa::hello world

 

it is logically impossible to do what you want without some kind of delay or replacement, since the double press must be handled if you want your script to detect it.



vito
  • Members
  • 2 posts
  • Last active: Mar 21 2014 01:45 PM
  • Joined: 15 Mar 2014

Thank you! :)

The Idea with a backspace works great but not every key.

Hotstring is a very cool thing! I will try to use that in future! :)