Press/held commands

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
subzero
Posts: 12
Joined: 03 Apr 2021, 18:46

Press/held commands

25 Jun 2021, 05:06

I would like to be able to run a command when CTRLC is pressed, and another when CTRLC is held. I would also like a sound to play. My attempt so far is this:

Code: Select all

~^c::
   SoundPlay, C:\Sound.wav
   KeyWait, ^c, T.5
Run, % ErrorLevel ? "D:\Command1" : "D:\Command2"
KeyWait, ^c
return
I'm getting an error on the keywait row, any help appreciated.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Press/held commands

25 Jun 2021, 05:24

KeyWait is looking for a single key to be identified, not a key combination. Change it to just c. That doesn’t mean your hotkey has to change. It just means it won’t be waiting for Ctrl to be released. If you really want to wait for both, you can follow it with another KeyWait.
subzero
Posts: 12
Joined: 03 Apr 2021, 18:46

Re: Press/held commands

27 Jun 2021, 04:48

boiler wrote:
25 Jun 2021, 05:24
KeyWait is looking for a single key to be identified, not a key combination. Change it to just c. That doesn’t mean you're hotkey has to change. It just means it won’t be waiting for Ctrl to be released. If you really want to wait for both, you can follow it with another KeyWait.
Thanks! I've made an edit to the code and have another question - can I get the delayed command (^x) to play a sound in addition to the initial sound? I tried it with AND SoundPlay but got an error.

Code: Select all

~^c::
   SoundPlay, C:\Sound.wav
   KeyWait, c, T.5
SendInput, % ErrorLevel ? "^x"  AND SoundPlay, C:\Sound.wav: "^c"
KeyWait, c
return
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Press/held commands

27 Jun 2021, 06:11

You can’t use the ternary operator in this case — at least not unless you create a function that would be called as part of an expression instead of trying to insert a command. Just use regular if/else:

Code: Select all

~^c::
   SoundPlay, C:\Sound.wav
   KeyWait, c, T.5
   if ErrorLevel
   {
      SendInput, ^x
      SoundPlay, C:\Sound.wav
   }
   else
      SendInput, ^c
   KeyWait, c
return

I would think you don’t really want to use ~ before the hotkey in this case because that would cause ^c to be sent every time (actually just allowing the natural one to stand), sometimes followed by ^x and other times followed by a second ^c. And I would think you would want to replace it with a $ to prevent the hotkey from triggering itself when you send ^c. See Hotkey Modifier Symbols.
subzero
Posts: 12
Joined: 03 Apr 2021, 18:46

Re: Press/held commands

27 Jun 2021, 08:39

boiler wrote:
27 Jun 2021, 06:11
You can’t use the ternary operator in this case — at least not unless you create a function that would be called as part of an expression instead of trying to insert a command. Just use regular if/else:

Code: Select all

~^c::
   SoundPlay, C:\Sound.wav
   KeyWait, c, T.5
   if ErrorLevel
   {
      SendInput, ^x
      SoundPlay, C:\Sound.wav
   }
   else
      SendInput, ^c
   KeyWait, c
return

I would think you don’t really want to use ~ before the hotkey in this case because that would cause ^c to be sent every time (actually just allowing the natural one to stand), sometimes followed by ^x and other times followed by a second ^c. And I would think you would want to replace it with a $ to prevent the hotkey from triggering itself when you send ^c. See Hotkey Modifier Symbols.
Thanks again, ive made another version that pastes text on its first command and opens a folder on its second command - the first command is running correctly, but the folder command does not trigger

Code: Select all

$<^<!1::
   SoundPlay, D:\SOUND.WAV
   KeyWait, 1, T.5
   if ErrorLevel
   {
	Run, D:\FOLDER
SoundPlay, D:\SOUND.WAV
   KeyWait, 1
   }
   else
   SendRaw TEXT
   Sleep 250
   Send {Enter}
[Mod edit: [code][/code] tags added.]
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Press/held commands

27 Jun 2021, 09:49

Do you have a folder on the D: drive named FOLDER? If so, it should open a File Explorer window.

By the way, the last few lines are apparently meant to be executed only on the else condition, but you didn't define a code block for them so only the line immediately after the else would be conditionally executed, and the others would be executed after both the if and the else. It should be like below. Also, if you removed the return at the end, you should add back.

Code: Select all

$<^<!1::
   SoundPlay, D:\SOUND.WAV
   KeyWait, 1, T.5
   if ErrorLevel
   {
      Run, D:\FOLDER
      SoundPlay, D:\SOUND.WAV
      KeyWait, 1
   }
   else
   {
      SendRaw TEXT
      Sleep 250
      Send {Enter}
   }
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Lamron750, mikeyww and 226 guests