Long / Short press a key ??

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
yfjuu6
Posts: 136
Joined: 28 Apr 2023, 15:28

Long / Short press a key ??

04 Dec 2023, 05:51

I would like to do this :
if RShift was pressed down for more than 200ms
It doesn't anything
if the key was pressed down for less than 200ms send x
else ( if short press, if the key was pressed down for less than 200ms)
Send "b"

I've tried this :

Code: Select all

$RShift:: {
    startTime := A_TickCount ;record the time the key was pressed
    KeyWait("RShift", "U") ; wait for the key to be released
    keypressDuration := A_TickCount - startTime ;calculate the duration the key was pressed down
    if (keypressDuration > 200) ; if the key was pressed down for more than 200ms :
    {
        Return ; Nothing happens
    }
    else ; if the key was pressed down for less than 200ms
    {
          Send "b"
    }
}
When I replace RShift with any other key it does what I want But with RShift specifically It doesn't.

Any help to do so Please!


[Mod edit: Removed all the text that was repeated (almost all of it).]
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Long / Short press a key ??

04 Dec 2023, 07:14

AHK will not allow you to make up your own syntax. ChatGPT is not good with AHK, especially v2. See the :arrow: KeyWait options.

Code: Select all

#Requires AutoHotkey v2.0

~RShift:: {
 KeyWait 'RShift'
 If A_TimeSinceThisHotkey < 200
  Send 'b'
}
User avatar
yfjuu6
Posts: 136
Joined: 28 Apr 2023, 15:28

Re: Long / Short press a key ??

04 Dec 2023, 13:47

mikeyww wrote:
04 Dec 2023, 07:14
AHK will not allow you to make up your own syntax. ChatGPT is not good with AHK, especially v2. See the :arrow: KeyWait options.

Code: Select all

#Requires AutoHotkey v2.0

~RShift:: {
 KeyWait 'RShift'
 If A_TimeSinceThisHotkey < 200
  Send 'b'
}
It does what I want, Thx.
But there is something very strange with just Ctrl :

Code: Select all

~Ctrl:: {
    KeyWait 'Ctrl'
    If A_TimeSinceThisHotkey < 200
        Send "b"
}
I use Ditto clipboard manager and have assigned a keyboard shortcut to it from itself (from the main app (Ditto) not from AHK),
when I press the keyboard shortcut I've assigned to activate the clipboard window of Ditto to paste some copied texts, and then select a text to paste it by pressing Enter or LButton, Nothing is pasted from the clipboard but the letter "v" is written. This happens in any program in which I want to paste some copied texts from Ditto. the same problem happens with the code that I posted above. I don't know where the letter "v" came from. :crazy:
when I comment the code or replace Ctrl with other modifier key, and try to paste something from the Ditto's clipboard, any copied text in the clipbord is pasted normally , As usual, without any problem and without the letter "v".
Do you know what's happening with Ctrl?
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Long / Short press a key ??

04 Dec 2023, 14:34

AHK is possibly capturing the Ctrl of Ctrl+V, which leaves V. If your script uses the same keys as other programs, software conflicts can arise.
User avatar
yfjuu6
Posts: 136
Joined: 28 Apr 2023, 15:28

Re: Long / Short press a key ??

04 Dec 2023, 15:23

mikeyww wrote:
04 Dec 2023, 14:34
AHK is possibly capturing the Ctrl of Ctrl+V, which leaves V. If your script uses the same keys as other programs, software conflicts can arise.
Even though I have changed the shortcut of Ditto to a different shortcut, the problem still persists.
Is there any other method for Ctrl?
Attachments
Capture d'écran 2023-12-04 214640.png
Capture d'écran 2023-12-04 214640.png (9.27 KiB) Viewed 946 times
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Long / Short press a key ??

04 Dec 2023, 18:06

If Ditto sends Ctrl+V to paste, then it does not matter what shortcuts you have chosen in Ditto, because none of them affect how Ditto pastes text. The shortcuts affect only what triggers the pasting.

Ditto is, more or less, superfluous software in the presence of AHK, because you can design your script to do what Ditto does. Various AHK scripts have already been written to manage the clipboard, so you don't even have to code anything. See LintaList, build your own, see others on the forum, etc. Clipjump
emp00
Posts: 197
Joined: 15 Apr 2023, 12:02

Re: Long / Short press a key ??

05 Dec 2023, 14:36

@yfjuujy6 I have also assigned shortcuts to automate Ditto with AHK (for certain reasons). I used commandline switches like this:

Code: Select all

#q:: Run "X:\Path-to-your-Ditto\Ditto.exe /Open"
^q:: Run "X:\Path-to-your-Ditto\Ditto.exe /PlainTextPaste"
Here's the list of available commands, maybe you can use one of those also for your tasks: https://github.com/sabrogden/Ditto/wiki/Command-Line-Options

Good luck!
User avatar
NPerovic
Posts: 40
Joined: 31 Dec 2022, 01:25
Contact:

Re: Long / Short press a key ??

06 Dec 2023, 02:15

yfjuujy6 wrote:
04 Dec 2023, 13:47
mikeyww wrote:
04 Dec 2023, 07:14
AHK will not allow you to make up your own syntax. ChatGPT is not good with AHK, especially v2. See the :arrow: KeyWait options.

Code: Select all

#Requires AutoHotkey v2.0

~RShift:: {
 KeyWait 'RShift'
 If A_TimeSinceThisHotkey < 200
  Send 'b'
}
It does what I want, Thx.
But there is something very strange with just Ctrl :

Code: Select all

~Ctrl:: {
    KeyWait 'Ctrl'
    If A_TimeSinceThisHotkey < 200
        Send "b"
}
I use Ditto clipboard manager and have assigned a keyboard shortcut to it from itself (from the main app (Ditto) not from AHK),
when I press the keyboard shortcut I've assigned to activate the clipboard window of Ditto to paste some copied texts, and then select a text to paste it by pressing Enter or LButton, Nothing is pasted from the clipboard but the letter "v" is written. This happens in any program in which I want to paste some copied texts from Ditto. the same problem happens with the code that I posted above. I don't know where the letter "v" came from. :crazy:
when I comment the code or replace Ctrl with other modifier key, and try to paste something from the Ditto's clipboard, any copied text in the clipbord is pasted normally , As usual, without any problem and without the letter "v".
Do you know what's happening with Ctrl?
This should work. (Tested it on win 11)

Code: Select all

~Ctrl:: {
    if KeyWait("Ctrl", "T0.2")
        Send("{Blind}b")
}
By the way, I don't understand why a software that operates the clipboard (Ditto) needs to ask the user for permission to pass through the firewall.
✨ Dark Theme for Everything
✨ Other Scripts

spinachrag
Posts: 9
Joined: 28 Aug 2023, 05:24

Re: Long / Short press a key ??

06 Dec 2023, 11:38

Seems like you got the same problem as I did, you can check my post if it helps, I got only 2 posts.
User avatar
yfjuu6
Posts: 136
Joined: 28 Apr 2023, 15:28

Re: Long / Short press a key ??

06 Dec 2023, 11:56

spinachrag wrote:
06 Dec 2023, 11:38
Seems like you got the same problem as I did, you can check my post if it helps, I got only 2 posts.
where is your post?
User avatar
yfjuu6
Posts: 136
Joined: 28 Apr 2023, 15:28

Re: Long / Short press a key ??

09 Dec 2023, 13:46

NPerovic wrote:
06 Dec 2023, 02:15
yfjuujy6 wrote:
04 Dec 2023, 13:47
mikeyww wrote:
04 Dec 2023, 07:14
This should work. (Tested it on win 11)

Code: Select all

~Ctrl:: {
    if KeyWait("Ctrl", "T0.2")
        Send("{Blind}b")
}
Yes, This is working well and The Ditto's problem has been solved with this. Thank you so much.
But It stills only one problem.

Now, I have these :

Code: Select all

~Ctrl:: {
    if KeyWait("Ctrl", "T0.2")a
        Send("{Blind}a")
}

^k:: Send("c")
when pressing very fast ^k or any combinision with Ctrl e.g (^x ^c ^z) It sends "ck" (According to the example shown above).
- I want when pressing very fast ^k sends just "c" not "ck"
- when pressing and release RCtrl for less than 200 ms it sends just "a"
Any help to do so Please!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: DavidP, FanaticGuru and 96 guests