Execute Subroutine on press and not on release

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AtleastItried
Posts: 51
Joined: 03 Mar 2017, 04:51

Execute Subroutine on press and not on release

01 May 2018, 08:50

I made some code that adds some buttons over my OneNote Window. It works, but the buttons only fire when I relase by mouse/pen, not when I press it. The problem with that is, that if I hold it a bit too long it gets attention, and as such OneNote loses attention and the whole GUI hides itself for a moment, so it forgets I pressed this button. Is there a way to make the button fire on press instead of on release? Or a better solution for this whole problem?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force
#Notrayicon

    SetTitleMatchMode 2

    Gui +LastFound -Caption +ToolWindow +Border + AlwaysOnTop
    Gui Add, Button, gDublicate x-1 y-1 w60 h25, Dublicate
    Gui Add, Button, gDelete x57 y-1 w60 h25, Delete
    Gui Add, Button, gBackwards x115 y-1 w25 h25, ←
    Gui Add, Button, gForwards x138 y-1 w25 h25, →

    Loop{
        WinWaitActive  ‎- OneNote
        Gui, Show, xCenter y35 NoActivate h23 w162
        WinWaitNotActive  ‎- OneNote
        Gui Hide
    }

    return:

    Dublicate:
        WinActivate  ‎- OneNote
        SendInput ^c
        Sleep 50
        SendInput ^v
    return

    Delete:
        WinActivate  ‎- OneNote   
        SendInput {Del}
    return

    Backwards:
        WinActivate  ‎- OneNote   
        SendInput ^z
    return

    Forwards:
        WinActivate  ‎- OneNote   
        SendInput ^y
    return
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Execute Subroutine on press and not on release

01 May 2018, 10:00

As a sorta janky workaround, you could have a hotkey looking for mouse clicks, then have that hotkey do MouseGetPos to find the control under the mouse and trigger the label based on that. Have the buttons themselves not actually do anything.
AtleastItried
Posts: 51
Joined: 03 Mar 2017, 04:51

Re: Execute Subroutine on press and not on release

01 May 2018, 10:58

MaxAstro wrote:As a sorta janky workaround, you could have a hotkey looking for mouse clicks, then have that hotkey do MouseGetPos to find the control under the mouse and trigger the label based on that. Have the buttons themselves not actually do anything.
On the other hand, I could try to make the GUI not hide when it is pressed on, which would work even better.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Execute Subroutine on press and not on release

01 May 2018, 11:38

Hi AtleastItried,

In this case you should rather use the +Owner option to make the GUI owned by the window (see the example).
If your aim is rather to make the button run a subroutine while it is pressed and not once pushed the solution put forward by MaxAstro is more relevant.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

	WinWait, ahk_class Notepad
	WinGet, ID, ID, % ahkid:="ahk_id " . WInExist() ;  ; if all parameters are omitted, WinExist returns the ID the Last Found Window if it still exist; WInWait set the Last Found Window

    Gui -Caption +ToolWindow +Owner%ID%
	/*
		+ToolWindow > no taskbar button
		+Owner%owner% > make the GUI owned by the window whose ID is the one stored in the variable 'ID'
	*/
	Gui, Margin, 0, 0
    Gui Add, Button, Section gDuplicate x-1 y-1 w60 h25, Duplicate
    Gui Add, Button, gDelete ys wp hp, Delete
    Gui Add, Button, gBackwards ys w25 hp, ←
    Gui Add, Button, gForwards ys wp hp, →
	WinWaitActive % ahkid
	Gui, Show, xCenter y35 NoActivate AutoSize
    return

    Duplicate:
		Gosub, _isWindowStillExist
        SendInput {Ctrl Down}c{Ctrl Up}
        SendInput {Ctrl Down}v{Ctrl Up}
    return
    Delete:
		Gosub, _isWindowStillExist
        SendInput {Delete}
    return

    Backwards:
		Gosub, _isWindowStillExist
        SendInput {Ctrl Down}z{Ctrl Up}
    return
    Forwards:
		Gosub, _isWindowStillExist
        SendInput {Ctrl Down}y{Ctrl Up}
    return
	_isWindowStillExist:
	if not WinExist("ahk_class Notepad")
		Exit
			WinActivate ; use the last found window
			WinWaitActive ; use the last found window
	return
	
[EDIT]
Maybe something like the following could suit your purpose:

Code: Select all

#NoEnv
#Warn
#SingleInstance force

i := 0
Gui Add, Button, vDuplicate w200 h80, Duplicate
Gui, Show
return

#If label:=MouseIsOver("Duplicate")
~LButton::
	while (GetKeyState("LButton", "P")) { ; && IsLabel(label)
		Gosub % label
		sleep, 300
	}
return
#If

Duplicate:
ToolTip % A_ThisLabel . " > " . ++i
return

MouseIsOver(_control) {
    MouseGetPos,,,, _ctrl, 3
	GuiControlGet, _ctrl, Name, % _ctrl
return (_control = _ctrl) ? _ctrl : false
}
Last edited by A_AhkUser on 01 May 2018, 12:09, edited 1 time in total.
my scripts
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: Execute Subroutine on press and not on release

01 May 2018, 11:47

Code: Select all

#SingleInstance force

Gui +LastFound -Caption +ToolWindow +Border + AlwaysOnTop
Gui Add, Button, HwndhDublicate x-1 y-1 w60 h25, Dublicate
Gui, Show

~LButton::
	MouseGetPos, , , , ctrl, 2
	if (ctrl == hDublicate){
		gosub, Dublicate
	}
	return

Dublicate:
	WinActivate  ?- OneNote
	SendInput ^c
	Sleep 50
	SendInput ^v
	return
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Execute Subroutine on press and not on release

01 May 2018, 12:16

Maybe this other GUI option can help you achieve your goal (see also my edit on my previous post):

Code: Select all

#NoEnv
#SingleInstance force

Gui, +AlwaysOnTop +E0x08000000 ; WS_EX_NOACTIVATE
Gui Add, Button, gtest,
Gui Show, AutoSize NoActivate
return

test() {
ToolTip % A_ThisFunc
}
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, peter_ahk, Rauvagol, Spawnova, wineguy and 284 guests