Page 1 of 1

Button on second click

Posted: 20 Mar 2018, 17:17
by Houzuki
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

Re: Button on second click

Posted: 20 Mar 2018, 17:23
by dilof

Code: Select all

z::
toggle := !toggle
if toggle
	SendInput {c}
else
	SendInput {p}
return

Re: Button on second click

Posted: 20 Mar 2018, 18:10
by swagfag

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

Re: Button on second click

Posted: 20 Mar 2018, 19:31
by Cuadrix
Something like this?

Code: Select all

lastShift := 0

$c::
if ((A_TickCount - lastShift) <= 250)
	SendInput, p
else
	SendInput, c
lastShift := A_TickCount
return

Re: Button on second click

Posted: 21 Mar 2018, 13:10
by evilC
My TapHoldManager library will let you detect when a key is pressed any number of times, and also supports long-press, tap-and-press etc.

Re: Button on second click

Posted: 21 Mar 2018, 15:21
by divanebaba
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

Re: Button on second click

Posted: 21 Mar 2018, 16:22
by wolf_II
Also try this one-liner:

Code: Select all

$c:: SendInput, % (Flag := Not Flag) ? "c" : "p"
I hope that helps.

Re: Button on second click

Posted: 21 Mar 2018, 19:11
by Cuadrix
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: