Holding the key for a time

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sSsSss
Posts: 5
Joined: 22 Jun 2021, 12:11

Holding the key for a time

Post by sSsSss » 22 Jun 2021, 12:24

Hello. I need a script that can hold key 1 (not numpad), but after a certain time (for example, 100 milliseconds) would release.

Ideally, a structure like this:
I click on the key J, and the key 1 is held down for 100 ms, and so that the action can be repeated.

Is that possible?
grantmartin2002
Posts: 3
Joined: 22 Jun 2021, 11:57

Re: Holding the key for a time

Post by grantmartin2002 » 22 Jun 2021, 12:42

Code: Select all

j::
Send {1 down}
Sleep 100
Send {1 up}
return
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Holding the key for a time

Post by braunbaer » 22 Jun 2021, 12:52

Code: Select all

j::
send {1 down}
sleep 100
send {1 up}
return
Note that the key will not autorepeat this way, though.


Edit: I was slower. I noticed that autorepeat does not work, so I looked in the documentation. It's explained there
sSsSss
Posts: 5
Joined: 22 Jun 2021, 12:11

Re: Holding the key for a time

Post by sSsSss » 22 Jun 2021, 14:11

grantmartin2002 wrote:

Code: Select all

j::
Send {1 down}
Sleep 100
Send {1 up}
return
Thanks for your help! But this is a bit different from what I wanted. I wanted the key to be holded for a 100 millisecond for example, but not just pressed with 100 milliseconds sleep
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Holding the key for a time

Post by braunbaer » 22 Jun 2021, 18:39

sSsSss wrote:
22 Jun 2021, 14:11
I wanted the key to be holded for a 100 millisecond for example, but not just pressed with 100 milliseconds sleep
I don't understand what you mean. What is the difference between "holding" the key and "pressing" the key for 100ms?
sSsSss
Posts: 5
Joined: 22 Jun 2021, 12:11

Re: Holding the key for a time

Post by sSsSss » 23 Jun 2021, 02:42

braunbaer wrote:
sSsSss wrote:
22 Jun 2021, 14:11
I wanted the key to be holded for a 100 millisecond for example, but not just pressed with 100 milliseconds sleep
I don't understand what you mean. What is the difference between "holding" the key and "pressing" the key for 100ms?
for example PRESSING is 1. holding is 1111111111111111111111
Rohwedder
Posts: 7556
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Holding the key for a time

Post by Rohwedder » 23 Jun 2021, 04:33

Hallo,
then perhaps:

Code: Select all

j::
SendMode, Event
SetKeyDelay,, 20
Loop, 5
	Send, {1 Down}
Send, {1 Up}
Return
sSsSss
Posts: 5
Joined: 22 Jun 2021, 12:11

Re: Holding the key for a time

Post by sSsSss » 23 Jun 2021, 09:03

Rohwedder wrote: Hallo,
then perhaps:

Code: Select all

j::
SendMode, Event
SetKeyDelay,, 20
Loop, 5
	Send, {1 Down}
Send, {1 Up}
Return
thank you, working! how can i set the key hold time?
Rohwedder
Posts: 7556
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Holding the key for a time

Post by Rohwedder » 23 Jun 2021, 09:21

Key hold time is (approximately) the product of Count and PressDuration.

Code: Select all

j:: ;key hold time ≈ Count * PressDuration
T := A_TickCount ;only for test
SendMode, Event
SetKeyDelay,, 20 ;PressDuration = 20ms
Loop, 5 ;Count = 5
	Send, {1 Down}
Send, {1 Up}
ToolTip,% A_TickCount - T "ms" ;only for test
Return
Post Reply

Return to “Ask for Help (v1)”