Button on second click

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Houzuki
Posts: 9
Joined: 13 May 2016, 16:20

Button on second click

20 Mar 2018, 17:17

Hi, I need help with a code where i for example press the C button once, but when i press it another time it presses the P button
so its like c:: c
but on the second c click is like c:: p
dilof
Posts: 4
Joined: 20 Mar 2018, 08:07

Re: Button on second click

20 Mar 2018, 17:23

Code: Select all

z::
toggle := !toggle
if toggle
	SendInput {c}
else
	SendInput {p}
return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Button on second click

20 Mar 2018, 18:10

Code: Select all

#NoEnv
#WinActivateForce
#SingleInstance, Force
#MaxThreadsPerHotkey 2
SendMode, Input
SetBatchLines, -1
SetTitleMatchMode, 2
SetWorkingDir, %A_ScriptDir%

class Robot {
	shouldSendAlternate := false

	sendAlternate(key := "") {
		RegExMatch(A_ThisHotkey, "(\w+)", thisHotkey)
		key := (this.shouldSendAlternate := !this.shouldSendAlternate) ? thisHotkey : key
		Send, {%key%}
	return	
	}
}

robot := new Robot()

$x::robot.sendAlternate("p")

z::ExitApp
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Button on second click

20 Mar 2018, 19:31

Something like this?

Code: Select all

lastShift := 0

$c::
if ((A_TickCount - lastShift) <= 250)
	SendInput, p
else
	SendInput, c
lastShift := A_TickCount
return
Last edited by Cuadrix on 21 Mar 2018, 19:10, edited 1 time in total.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Button on second click

21 Mar 2018, 15:21

dilof wrote:

Code: Select all

z::
toggle := !toggle
if toggle
	SendInput {c}
else
	SendInput {p}
return
Nice solution, dilof, for learning and teaching, but not exactly desired solution. :mrgreen: :mrgreen:
Try this:

Code: Select all

$c::
toggle := !toggle
if (toggle)
	SendInput c
else
	SendInput p
return
Einfach nur ein toller Typ. :mrgreen:
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Button on second click

21 Mar 2018, 16:22

Also try this one-liner:

Code: Select all

$c:: SendInput, % (Flag := Not Flag) ? "c" : "p"
I hope that helps.
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Button on second click

21 Mar 2018, 19:11

divanebaba wrote:
dilof wrote:

Code: Select all

z::
toggle := !toggle
if toggle
	SendInput {c}
else
	SendInput {p}
return
Nice solution, dilof, for learning and teaching, but not exactly desired solution. :mrgreen: :mrgreen:
Try this:

Code: Select all

$c::
toggle := !toggle
if (toggle)
	SendInput c
else
	SendInput p
return
Damn it, made the same mistake :headwall:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 241 guests