Press down and release input

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Aibabos
Posts: 5
Joined: 13 Dec 2024, 13:53

Press down and release input

Post by Aibabos » 13 Dec 2024, 14:11

Hello everyone.

I am struggling to code script, that simulates a person holding down keyboard key '1' for 1 second.

I tried a lot, but it never do "the same" as a person can. In result there would be a lot of "11111111111"

This does not work as intended:

Code: Select all

#Persistent
a:: 
SendInput, {1 Down}
Sleep, 1000
SendInput, {1 Up}
return ;
Last edited by Ragnar on 13 Dec 2024, 14:16, edited 1 time in total.
Reason: code tags; moved from Wish List to Ask for Help (v1)

User avatar
mikeyww
Posts: 29727
Joined: 09 Sep 2014, 18:38

Re: Press down and release input

Post by mikeyww » 13 Dec 2024, 14:27

Welcome to this AutoHotkey forum!

A regular Send will get you a "down" and "up".

Code: Select all

#Requires AutoHotkey 1
a::
end := A_TickCount + 1000
While (A_TickCount < end)
 Send 1
Return
If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.

Aibabos
Posts: 5
Joined: 13 Dec 2024, 13:53

Re: Press down and release input

Post by Aibabos » 13 Dec 2024, 16:27

wow. thanks for quick response.

I have looked to you suggested code. If I understand it well, it repeatedly sends number 1, until loop ends. Basically result is the same as holding it down.
What I am not sure, how fast it sends it? Is it like 'as fast as possible' = as fast as CPU processes repeated scripted Send 1?
Pardon my insufficient knowledge of how hardware and software works. I am no IT specialist.

My goal was to simulate the hardware keyboard button 1 being pressed and held. In as raw as I could. That was my reason for use sendinput instead of send.

Thanks for your patience with me.

User avatar
mikeyww
Posts: 29727
Joined: 09 Sep 2014, 18:38

Re: Press down and release input

Post by mikeyww » 13 Dec 2024, 17:06

Have you run the script?

Regarding the key delay: :arrow: SetKeyDelay.

Aibabos
Posts: 5
Joined: 13 Dec 2024, 13:53

Re: Press down and release input

Post by Aibabos » 13 Dec 2024, 17:54

Yes. I just did 20 times. Here are my little something results of how many ones my PC did in notepad
3x 62
8x 63
8x 64
1x 65

What I also did, is I modified your code. I added Sleep 10 just to see if previous is faster or not. Here are results:

1x 61
6x 62
7x 63
5x 64
1x 65

Code: Select all

#Requires AutoHotkey 1
a::
end := A_TickCount + 1000
While (A_TickCount < end)
 Send 1
 sleep 10
Return
Unfortunately I cant myself hold number one exactly 1 second. But there is definitely a delay between first 1, second 1, then ones just keeps coming seemingly as fast as in your code.
Last edited by SKAN on 14 Dec 2024, 02:44, edited 1 time in total.
Reason: code tag

User avatar
mikeyww
Posts: 29727
Joined: 09 Sep 2014, 18:38

Re: Press down and release input

Post by mikeyww » 14 Dec 2024, 08:48

  1. You are welcome to alter the key delay as already noted.
  2. A code block is always defined by braces.
  3. If the hotkey remains held when the hotkey subroutine has finished, the subroutine will execute again.

Aibabos
Posts: 5
Joined: 13 Dec 2024, 13:53

Re: Press down and release input

Post by Aibabos » 14 Dec 2024, 09:08

Ahh. I found a big mistake in my code. Basically I did my code wrong. Here is my corrected code

Code: Select all

a::
end := A_TickCount + 1000
While (A_TickCount < end)
{
Send 1
sleep 10
}
Return
Now I get consistently almost same 33 ones in 1 second. Just occasionally get 32.

Still if I understand well, Send command makes Windows understand pressdown + pressup. But many times in quick succession. My original question was, if there is option for for just PRESSDOWN and later in the code PRESSUP.
Because now this does not work:

Code: Select all

Send {1 down}
; some other code
Send {1 up}
Last edited by joedf on 14 Dec 2024, 10:35, edited 1 time in total.
Reason: use [code] tags

User avatar
mikeyww
Posts: 29727
Joined: 09 Sep 2014, 18:38

Re: Press down and release input

Post by mikeyww » 14 Dec 2024, 10:46

Yes, your second ("up") Send statement will execute whenever that statement is encountered as the script runs.

If you are wondering how many times the text will be sent during a defined period, that number is not determined by the script alone, since your computer is also doing other things during the same period. If you need a specific number of iterations to occur, use Loop.

Code: Select all

#Requires AutoHotkey 1
Send {Shift down}
SoundBeep 1500
Send 247 ; some other code
Send [[[Now sending key up]]]
Send {Shift up}
Send [[[Done]]]247
SoundBeep 1000
@$&{{{NOW SENDING KEY UP}}}[[[Done]]]247

Aibabos
Posts: 5
Joined: 13 Dec 2024, 13:53

Re: Press down and release input

Post by Aibabos » 14 Dec 2024, 18:03

thanks for some interesting code.

But I still does not solved my problem with holding down a key.
Yes, I can simulate a lot of individual clicks with really rapid clicks with rapid Send command. In the human eye it has almost same appearance. But it is 'workaround' solution, even if it is elegant.

I was pointing out, that Send key down and Send key up, does not work in AHK as I expected. Or maybe I used wrong command for the job...

User avatar
mikeyww
Posts: 29727
Joined: 09 Sep 2014, 18:38

Re: Press down and release input

Post by mikeyww » 15 Dec 2024, 07:17

From what I can tell, both of your questions have been answered in this thread: how to hold a key, and how to send a key repeatedly. In doing either, you are welcome to use SendInput if it helps.

Post Reply

Return to “Ask for Help (v1)”