I can’t uncomment the code… Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

I can’t uncomment the code…

Post by FredOoo » 22 May 2022, 12:39

After a long work, I can’t remove the display commands I wrote for dev time.
The problem on 3 places, where I add ★★★
The problem if I uncomment this code: the oneClick is reconized like a dblClick.
Now I don’t know what to do…

(The goal of this <tf> Lib is to just include it to quick write code for testing.
One click on trayIcon reload the script.
But il’s possible to simply add menu, of other functions to trigger if clicks on trayIcon)
­

Code: Select all

;#Requires AutoHotkey 2.0-beta.3
#NoTrayIcon
#SingleInstance force
Persistent

/*
	NameSpace:
		CTrayMenu → global TrayMenu
		CTrayIcon → global TrayIcon
		CConsole  → global Console
		<ƒ_>
	Hotkeys:
		TrayIcon.reload →
		Console.show    → <#C
	Usage:
		OneClick → reload
		DblClick → Menu.default (if def)
	  or
	  	OneClick → TrayIcon_onClick
		DblClick → TrayIcon_onDblClick
*/

;#Include <ƒ>
ƒ_ScriptName( ){
	splitPath a_ScriptFullPath,,,, &out_NameNoExt
	return out_NameNoExt
}

#Include ..\..\AutoHotkey\Lib\CConsole_f.ahk
global Console := CConsole()
Console.hotkey := "<#C"
Console.activate
 <#V::Console.log "`n"

;global Console := CConsole()  ; ★★★ if I use this → BUG
;class CConsole {
;	log(*){
;	}
;	clear(){
;	}
;}


global TrayMenu := CTrayMenu()
global TrayIcon := CTrayIcon()

class CTrayMenu extends Menu
{
	__new(){
		;; Menu
		a_AllowMainWindow := false
		this.Cmds := Map()
		this.delete()
		smnAhk := Menu()
		smnAhk.addStandard()
		smnAhk.default := "&Reload Script"
		this.add("AutoHotkey", smnAhk)
		this.add
		this.setIcon "AutoHotkey", "AutoHotkey64.exe", 1, 16
		;this.clickCount := 2 (default)
	}
	add( cmdName:=unset, cmdFunc:=unset ){
		if !isSet(cmdName)
			super.add
		else {
			super.add( cmdName, cmdFunc )
			this.Cmds[ cmdName ] := cmdFunc
		}
	}
}

class CTrayIcon
{
	__new(){
		this.set "AutoHotkey64.exe", 2
		this.tip := ƒ_ScriptName()
		onMessage 0x404, objBindMethod(this, "AHK_NOTIFYICON")
		a_IconHidden := false
		this.Cmds := Map()
		this.devTime := true  ; ★★★ if I false this → BUG
		} ;
	set( fileName, iconNumber:=1, freeze:=0 ){
		traySetIcon(fileName, iconNumber, freeze)
		} ;
	number {
		get => a_IconNumber
		set {
			this.set( this.file, value )
		}
		} ;
	file {
		get => a_IconFile
		} ;
	tip {
		get => a_IconTip
		set => a_IconTip := value
		} ;
	reloadKey {
		set {
			try {
				hotkey value, objBindMethod( this, "reload" )
			} catch Error as Err {
				msgBox( Err.message, this.tip )
			}
		}
		} ;
	reload(*){
		reload
		} ;
	onClick(){
		if true or this.devTime {
			Console.log "➽ " a_ThisFunc
			soundbeep 3000,30
		}
		defautName := TrayMenu.default
		if isSet(TrayIcon_onClick) and TrayIcon_onClick.hasBase(Func.prototype) {
			TrayIcon_onClick()
		} else if defautName and TrayMenu.clickCount==1 {
			funcDefault := TrayMenu.Cmds[defautName]
			funcDefault()
		} else {
			r := msgBox("Reload?", a_IconTip, 0x1)
			if r=='OK'
				this.reload()
		}
	}
	onDblClick(from:=''){
		if true or this.devTime {
			Console.log "➽ " a_ThisFunc "  " from
			soundbeep 1000,30
			soundbeep 1000,30
		}
		defautName := TrayMenu.default
		if isSet(TrayIcon_onDblClick) and TrayIcon_onDblClick.hasBase(Func.prototype) {
			TrayIcon_onDblClick()
		} else if defautName and TrayMenu.clickCount==2 {
			funcDefault := TrayMenu.Cmds[defautName]
			funcDefault()
		}
	}
	AHK_NOTIFYICON( wParam, lParam, * ){
		static this_onClick := objBindMethod(this, "onClick")
		static dblClicTime  := dllCall("User32.dll\GetDoubleClickTime", 'Uint')
		wm_display(lParam, plus:=''){
			; for dev time
			if this.devTime {
				static wm := Map(
					0x200,  "WM_MOUSE_MOVE",
					0x201,  "WM_L BUTTON_DOWN",
					0x202,  "WM_L BUTTON_UP",
					0x203,  "WM_L BUTTON_DBLCLK",
					0x204,  "WM_R_BUTTON_DOWN",
					0x205,  "WM_R_BUTTON_UP",
					0x02A3, "WM_MOUSE_LEAVE", ; never apppend
				) ;
				if wm.has(lParam)
					Console.log format("0x{1:04X}", lParam) " ●" wm[lParam] "● ", plus
				else
					Console.log format("0x{1:04X}", lParam) " ●?●", plus
				}
			} ;
		/* if lParam==0x200  ; WM_MOUSEMOVE
			wm_display(lParam)
			*/
		if lParam==0x201 { ; WM_LBUTTONDOWN
			setTimer this_onClick, -dblClicTime
			wm_display(lParam)                     ; ★★★ if I comment this → BUG
			return 0
		}
		if lParam==0x202 { ; WM_LBUTTONUP
			setTimer this_onClick, 0
			;wm_display(lParam)
 			this.onDblClick("WM_LBUTTONUP")
			return 0
		}
		if lParam==0x203 { ; WM_LBUTTONDBLCLK
			setTimer this_onClick, 0
			;wm_display(lParam)
			; microsoft say WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK et WM_LBUTTONUP.
			return 0
		}
		if lParam==0x204 { ; WM_RBUTTONDOWN
			wm_display(lParam)
			if TrayMenu.default {
				if ( (TrayMenu.clickCount==2
   			       and isSet(TrayIcon_onDblClick) and TrayIcon_onDblClick.hasBase(Func.prototype))
   			    or (TrayMenu.clickCount==1
   			       and isSet(TrayIcon_onClick) and TrayIcon_onClick.hasBase(Func.prototype)) )
				TrayMenu.default := ''
			}
			return 0
			}
		if lParam==0x205 { ; WM_RBUTTONUP
			;wm_display(lParam)
			TrayMenu.show
			return 1
		}
	}
}
Here is an exemple to use this Lib

Code: Select all

#Requires AutoHotkey 2.0-beta.3
#Include <tf>


/*
	If Click on TrayIcon → reload
	If DblClick on TrayIcon → DoNothing, but bdlClick is detected
*/
TrayMenu.add "Effacer console", clear
clear(*){
	Console.clear
}
TrayMenu.add("Bip!", bip)
bip(*){
	soundBeep 1500,150
	Console.log "➜ TrayMenu.default =Bip! "
}


/*
	If a default menu is defined (on 1 or 2 clicks)
	it will be executed before defaut
*/
;TrayMenu.default := "Bip!"
;TrayMenu.clickCount := 1


/*
	If you define TrayIcon_onClick() or TrayIcon_onDblClick()
	it will be executed before anything else
*/
;TrayIcon_onClick(){
;	Console.log "➜ " a_ThisFunc
;}
;TrayIcon_onDblClick(){
;	Console.log "➜ " a_ThisFunc
;}
Here is thce Console I use on dev time

Code: Select all

;#Requires AutoHotkey 2.0-beta.3


class CConsole {

	__new( title := "Console" ) {
		HWND := winExist( title " ahk_class Notepad" )
		if ( HWND ) {
			PID := WinGetPID( "ahk_id " HWND )
			this.ahkPID  := "ahk_pid " PID
			this.ahkHWND := "ahk_id  " HWND
			this.clear
		} else {
			detectHiddenWindows 1
			try run( "notepad.exe",,"Hide", &PID )
			this.ahkPID := "ahk_pid " PID
			HWND := winWait(this.ahkPID)
			if HWND==0
				return
			this.ahkHWND := "ahk_id  " HWND
			winMove(0, 0, a_ScreenWidth/4, a_ScreenHeight, this.ahkHWND)
			winSetTitle( title, this.ahkHWND )
			winShow( this.ahkHWND )
		}
    }

	hotkey {
		set {
			try {
				hotkey value, objBindMethod( this, "show" )
			} catch Error as Err {
				a := 2
				msgBox( Err.message, "AHK Console" )
			}
		}
	}

	log( variables* ) {
		if ( !winExist( this.ahkHWND ) )
			return
		ControlSend( "{Ctrl down}{End}", "Edit1", this.ahkHWND )
		lastIndex := variables.length
		if lastIndex == 0
			EditPaste( "`r`n", "Edit1", this.ahkHWND )
		for index, var in variables {
			if (isObject(var)) {
				EditPaste( "{`r`n", "Edit1", this.ahkHWND )
				for key, value in var {
					EditPaste( "    " key ": " value "`r`n", "Edit1", this.ahkHWND )
				}
				EditPaste( "}`r`n", "Edit1", this.ahkHWND )
			} else {
				sep := (index==lastIndex? "`r`n" : " ")
				EditPaste( var sep, "Edit1", this.ahkHWND )
			}
		}
	}

	show( v* ) {
		if ( !winExist( this.ahkHWND ) ) {
			msgBox( "La Console est fermée." )
			return
		}
		winSetAlwaysOnTop( true,  this.ahkHWND )
		winSetAlwaysOnTop( false, this.ahkHWND )
	}

	activate() {
		WinActivate( this.ahkHWND )
		;
	}

	clear() {
		ControlSetText( "", "Edit1", this.ahkHWND )
		;
	}

	test() {
		this.log( "Notice that `r`n is a Carriage Return." )
		this.log( "New line", "…plus." )
	}

}
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: I can’t uncomment the code…

Post by FredOoo » 24 May 2022, 15:37

If I change CConsole code to this, it doesn’t works.

Code: Select all

	log( variables* ) {
		if ( !winExist( this.ahkHWND ) )
			return
		ControlSend( "{Ctrl down}{End}", "Edit1", this.ahkHWND )
		lastIndex := variables.length
		if lastIndex == 0
			this.editPaste( "`r`n", "Edit1", this.ahkHWND )
		for index, var in variables {
			if (isObject(var)) {
				this.editPaste( "{`r`n", "Edit1", this.ahkHWND )
				for key, value in var {
					this.editPaste( "    " key ": " value "`r`n", "Edit1", this.ahkHWND )
				}
				this.editPaste( "}`r`n", "Edit1", this.ahkHWND )
			} else {
				sep := (index==lastIndex? "`r`n" : " ")
				this.editPaste( var sep, "Edit1", this.ahkHWND )
			}
		}
	}

	editPaste(*){
		; Do Nothing
	}
How can EditPast change mouse triggers ?
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: I can’t uncomment the code…

Post by FredOoo » 25 May 2022, 01:59

If I use EditPast during a WM_LBUTTONDOWN message, SimpleClick and DoubleClick will be reconized.
If I don’t use it, I will receve DoubleClick in any case.

Can you explain that and how to work with that ?

Code: Select all

		if lParam==0x201 { ; WM_LBUTTONDOWN
			setTimer this_onClick, -dblClicTime
			
			;wm_display(lParam)  ;★★★ if I comment this → BUG
			
			if HWND := winExist( "Console ahk_class Notepad" )
				editPaste( "❖", "Edit1", "ahk_id " HWND )

			
			return 0
		}
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: I can’t uncomment the code…  Topic is solved

Post by FredOoo » 25 May 2022, 10:34

Now it's sure. If you EditPast between ClickDown and ClickUp, it’s a mess.
A also understand now why I didn’t get the sequence WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK and WM_LBUTTONUP a DblClik, as Microsoft say.

Now, the scripts below works fine:

Code: Select all

#Requires AutoHotkey 2.0-beta.3
#Include <t>

;TrayIcon.number := 1
;TrayIcon.set("shell32.dll", 55)  ; (default="AutoHotkey64.exe", TrayIcon.number=2)
TrayIcon.reloadKey := "^!R"  ; (default=none)
TrayIcon.soundClicks := true  ; (default=false)
TrayIcon.confirmReload := false  ; (default=false)

TrayMenu.add "Clear console", clear
TrayMenu.add("Bip!", bip)

TrayMenu.default := "Bip!"
;TrayMenu.clickCount := 1

clear(*){
	msgBox "> Console.clear"
}

bip(*){
	soundBeep 1500,150
}


;If you define whose functions, they will be executed first.
;TrayIcon_onClick(){
;	msgBox "> " a_ThisFunc
;}

;TrayIcon_onDblClick(){
;	msgBox "> " a_ThisFunc
;}
The Lib for quick tests:

Code: Select all

;#Requires AutoHotkey 2.0-beta.3
#NoTrayIcon
#SingleInstance force
Persistent

/*
	Environnement de développement pour les tests
	avec TrayIcon et TrayMenu
	///////////////////////////////////////////////////////////

	NameSpace:
		CTrayMenu → global TrayMenu
		CTrayIcon → global TrayIcon
		CConsole  → global Console
		<ƒ>

	Hotkeys:
		TrayIcon.reload →
		Console.show    → <#C

	Usage:
		OneClick → reload
		DblClick → Menu.default (if defined)
		  or
	  	OneClick → TrayIcon_onClick
		DblClick → TrayIcon_onDblClick

		TrayIcon.reloadKey := none by default
		TrayIcon.soundClicks := false (default)
		TrayIcon.confirmReload := false (default)
		TrayIcon.set("shell32.dll", 55)
		TrayIcon.number := integer, change ico in same file
		TrayIcon.tip  ; a_ScriptName with no ext
*/

;#Include <ƒ>
;ƒ_TonBeep("Si")
ƒ_ScriptName( ){
	; Anciennement NameNoExt
	splitPath a_ScriptFullPath,,,, &out_NameNoExt
	return out_NameNoExt
}


;#Include ..\..\AutoHotkey\Lib\CConsole_f.ahk
;#Include ..\..\AutoHotkey\Lib\CConsoleMini_v2b3.ahk
;global Console := CConsole()
;Console.hotkey := "<#C"


global TrayMenu := CTrayMenu()
global TrayIcon := CTrayIcon()


class CTrayMenu extends Menu
{
	__new(){
		;; Menu
		a_AllowMainWindow := false
		this.Cmds := Map()
		this.delete()
		smnAhk := Menu()
		smnAhk.add("&Open", objBindMethod(this,"openAHKWin") )
		smnAhk.addStandard()
		smnAhk.default := "&Reload Script"
		this.add("AutoHotkey", smnAhk)
		this.add
		this.setIcon "AutoHotkey", "AutoHotkey64.exe", 1, 16
		;this.clickCount := 2 (default)
	}
	add( cmdName:=unset, cmdFunc:=unset ){
		if !isSet(cmdName)
			super.add ; separator
		else {
			super.add( cmdName, cmdFunc )
			this.Cmds[ cmdName ] := cmdFunc ; to memorize cmd list
		}
	}
	openAHKWin(*){
		;? can't use: smnAhk.add("&Open", keyHistory )
		keyHistory
	}
}


class CTrayIcon
{
	__new(){
		this.set "AutoHotkey64.exe", 2
		a_IconHidden := false
		this.tip := ƒ_ScriptName()
		onMessage 0x404, objBindMethod(this, "AHK_NOTIFYICON")
		this.Cmds := Map()
		this._soundClicks   := false
		this._confirmReload := false
		} ;
	set( fileName, iconNumber:=1, freeze:=0 ){
		traySetIcon(fileName, iconNumber, freeze)
		} ;
	number {
		get => a_IconNumber
		set {
			this.set( this.file, value )
		}
		} ;
	soundClicks {
		get => this._soundClicks
		set => this._soundClicks := value
		} ;
	confirmReload {
		get => this._confirmReload
		set => this._confirmReload := value
		} ;
	file {
		get => a_IconFile
		} ;
	tip {
		get => a_IconTip
		set => a_IconTip := value
		} ;
	reloadKey {
		set {
			try {
				hotkey value, objBindMethod( this, "reload" )
			} catch Error as Err {
				msgBox( Err.message, this.tip )
			}
		}
		} ;
	reload(*){
		reload
		} ;
	onClick(){
		if this.soundClicks
			soundbeep 1000,30
		defautName := TrayMenu.default
		if isSet(TrayIcon_onClick) and TrayIcon_onClick.hasBase(Func.prototype) {
			TrayIcon_onClick()
		} else if defautName and TrayMenu.clickCount==1 {
			funcDefault := TrayMenu.Cmds[defautName]
			funcDefault()
		} else if this.confirmReload {
			if 'OK'=msgBox("Reload?", a_IconTip, 0x1)
				this.reload()
		} else
			this.reload()
	}
	onDblClick(from:=''){
		if this.soundClicks {
			soundbeep 1000,30
			soundbeep 1000,30
		}
		defautName := TrayMenu.default
		if isSet(TrayIcon_onDblClick) and TrayIcon_onDblClick.hasBase(Func.prototype) {
			TrayIcon_onDblClick()
		} else if defautName and TrayMenu.clickCount==2 {
			funcDefault := TrayMenu.Cmds[defautName]
			funcDefault()
		}
	}
	AHK_NOTIFYICON( wParam, lParam, * ){
		static this_onClick := objBindMethod(this, "onClick")
		static dblClicTime  := dllCall("User32.dll\GetDoubleClickTime", 'Uint')
		if lParam==0x201 { ; WM_LBUTTONDOWN
			setTimer this_onClick, -dblClicTime
			return 0
		}
		if lParam==0x203 { ; WM_LBUTTONDBLCLK
			setTimer this_onClick, 0
 			this.onDblClick()
			return 0
		}
		if lParam==0x204 { ; WM_RBUTTONDOWN
			if TrayMenu.default {
				if ( (TrayMenu.clickCount==2
   			       and isSet(TrayIcon_onDblClick) and TrayIcon_onDblClick.hasBase(Func.prototype))
   			       or (TrayMenu.clickCount==1
   			       and isSet(TrayIcon_onClick) and TrayIcon_onClick.hasBase(Func.prototype)) )
				TrayMenu.default := ''
			}
			return 0
			}
		if lParam==0x205 { ; WM_RBUTTONUP
			TrayMenu.show
			return 1
		}
	}
}
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

Post Reply

Return to “Ask for Help (v2)”