Long-press '1' to send '!' - Issues

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
iamMG
Posts: 28
Joined: 12 Nov 2017, 11:32

Long-press '1' to send '!' - Issues

Post by iamMG » 22 Oct 2021, 02:27

This code sends '1' when I press and hold the '1' key for less than 0.3 seconds, and '!' if held for more than 0.3 s.

Code: Select all

$1::
    keyWait 1 , T0.3
	if !ErrorLevel
		send {1}
	else send {!}
	keyWait 1 
return
This works as expected. However, if I press any other key in a quick succession after a short-press '1', for example '2', then my computer registers it as '21' instead of '12'.
How to fix this?

Edit: Using 'Critical' before the first 'keywait' helps with the short-press issue but causes multiple '!' when long-pressing '1'.

Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Long-press '1' to send '!' - Issues

Post by Rohwedder » 22 Oct 2021, 02:46

Hallo,
try:

Code: Select all

$1::
1Up := False
Input, Keys, T.3
IF !1Up
	send {1}%Keys%
else send {!}%Keys%
keyWait 1
return
1 Up::1Up := True

iamMG
Posts: 28
Joined: 12 Nov 2017, 11:32

Re: Long-press '1' to send '!' - Issues

Post by iamMG » 08 Dec 2021, 02:32

Thanks for the reply. I got busy and couldn't reply earlier.

I ended up doing this:

Code: Select all

keyWait %key% , D
send {%key%}
keyWait %key% , T%timeout%
if ErrorLevel
	send {Backspace}{%secondary%}
key can be any digit (0 to 9), secondary is their respective 'Shift' usages (0: ), 1: ! , .... , 9: ( , etc.). timeout is set to 0.20

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Long-press '1' to send '!' - Issues

Post by Xtra » 08 Dec 2021, 04:21

SendInput would be worth trying to prevent mixed sends from multiple hotkeys.
SendInput and SendPlay [v1.0.43+]: SendInput and SendPlay use the same syntax as Send but are generally faster and more reliable. In addition, they buffer any physical keyboard or mouse activity during the send, which prevents the user's keystrokes from being interspersed with those being sent.

antonio1475
Posts: 2
Joined: 19 May 2021, 10:51

Re: Long-press '1' to send '!' - Issues

Post by antonio1475 » 23 Jun 2022, 03:58

iamMG wrote:
08 Dec 2021, 02:32
Thanks for the reply. I got busy and couldn't reply earlier.

I ended up doing this:

Code: Select all

keyWait %key% , D
send {%key%}
keyWait %key% , T%timeout%
if ErrorLevel
	send {Backspace}{%secondary%}
key can be any digit (0 to 9), secondary is their respective 'Shift' usages (0: ), 1: ! , .... , 9: ( , etc.). timeout is set to 0.20
Hi. What is the full code for this? (variables definition, etc). Thank you!

antonio1475
Posts: 2
Joined: 19 May 2021, 10:51

Re: Long-press '1' to send '!' - Issues

Post by antonio1475 » 23 Jun 2022, 04:11

@Rohwedder

Thank you for this. I was able to make my mouse side buttons Copy/Cut and Paste/Win-V (Windows clipboard) with your code and Xbutton1 & Xbutton2:

Code: Select all

$XButton1::
XButton1Up := False
Input, Keys, T.2
IF !XButton1Up
	Send ^x
else Send ^c
keyWait XButton1
return
XButton1 Up::XButton1Up := True

$XButton2::
XButton2Up := False
Input, Keys, T.2
IF !XButton2Up
	Send #v
else Send ^v
keyWait XButton2
return
XButton2 Up::XButton2Up := True

Post Reply

Return to “Ask for Help (v1)”