Are there labels for the default icon menu items? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Are there labels for the default icon menu items?

03 Jul 2021, 20:48

Hi everyone,
i want to write a script that will notify me when i suspend the script with the "Suspend Hotkeys" item on the default menu. Is there a label for that item?
Last edited by necomerx on 03 Jul 2021, 21:34, edited 1 time in total.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

03 Jul 2021, 21:01

It's a variable, not a label, but A_IsSuspended tells you if the script is suspended. So you can accomplish what you described like this:

Code: Select all

#Persistent

Notified := 0
SetTimer, SuspendCheck, 300
return

SuspendCheck:
	if A_IsSuspended {
		if !Notified {
			Notified := 1
			MsgBox, Script is suspended
		}
	}
	else
		Notified := 0
return
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: Are there labels for the default icon menu items?

03 Jul 2021, 21:15

Thanks you boiler for clarifying and your help!
i'm gonna check that now!

Edit:
As i though It is not working unfortunately with my script witch has a loop, but it is working for a new script. So the default menu don't have variables to acces its different item, only possible for custom menu i guess?
https://autohotkey.com/board/topic/3955-menu-tray-default-help/
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Are there labels for the default icon menu items?  Topic is solved

03 Jul 2021, 22:01

All menu items work by sending a WM_COMMAND message to one of the script's windows (usually the main window). User-created menu items have an auto-assigned ID that depends on the order in which items are created across all menus, so it is usually best to just stick with assigning labels/functions via the Menu command in v1.x or Menu object in v2.

The IDs of the standard menu items are defined in the source code. Tray menu items are defined by the CommandIDs enumeration, with ID_TRAY_SUSPEND being 65305. The main window's File menu items are defined in resource.h, with ID_FILE_SUSPEND being 65404.

To detect when the user selects a standard menu item, you can monitor the WM_COMMAND message (0x111).

Code: Select all

#Requires AutoHotkey v1.1
OnMessage(0x111, Func("Receive_WM_COMMAND"))
Receive_WM_COMMAND(wParam) {
    if (wParam = 65305) { ; Tray, &Suspend Hotkeys
        about_to_suspend := !A_IsSuspended
        MsgBox % "Click OK to " (about_to_suspend ? "" : "un") "suspend hotkeys."
    }
}
The default action of Suspend Hotkeys is performed after the function returns, so A_IsSuspended is true if hotkeys were previously suspended but are about to be unsuspended. Alternatively, you can override (and in this case replicate) the default action:

Code: Select all

#Requires AutoHotkey v1.1
OnMessage(0x111, Func("Receive_WM_COMMAND"))
Receive_WM_COMMAND(wParam) {
    if (wParam = 65305) { ; Tray, &Suspend Hotkeys
        Suspend
        MsgBox % "Hotkeys " (A_IsSuspended ? "" : "un") "suspended."
        return 0
    }
}
With AutoHotkey v2.0, you can directly modify the standard menu items:

Code: Select all

#Requires AutoHotkey v2.0-a138
A_TrayMenu.Add "&Suspend Hotkeys", TraySuspend
TraySuspend(*) {
    Suspend
    MsgBox "Hotkeys " (A_IsSuspended ? "" : "un") "suspended."
}
Persistent



The correct term for "the default icon menu items" is "the standard menu items". They can be added to any menu with Menu MenuName, Standard or removed from the tray menu with Menu Tray, NoStandard. You can do the latter and then add your own custom menu items which look like the standard items but do whatever you want (and this also allows you to add icons with Menu Tray, Icon, ...).
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

03 Jul 2021, 22:53

necomerx wrote: As i though It is not working unfortunately with my script witch has a loop, but it is working for a new script.
The approach lexikos showed is better, of course, but I'm not sure what you're saying about your script having a loop causing the approach I showed not to work. The timer continues regardless of whether your script is looping. For example:

Code: Select all

#Persistent

CoordMode, ToolTip, Screen
Notified := 0
SetTimer, SuspendCheck, 300
loop {
	ToolTip, % "`n`tLoop count : " A_Index "`t`t `n ", A_ScreenWidth/2-100, A_ScreenHeight/2
	Sleep, 100
}
return

SuspendCheck:
	if A_IsSuspended {
		if !Notified {
			Notified := 1
			TrayTip, Suspended, Script is suspended
		}
	}
	else
		Notified := 0
return
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: Are there labels for the default icon menu items?

04 Jul 2021, 04:05

Thanks you lexikos also for clarifying and your great help! I was wondering for quite some times how it will be possible to interact with the standard menu items, and your detailed reply answer many of my questions! Your codes work well, also sorry boiler your code work fine in the auto-execute section of my script before the loop. I needed to specify that the codes will be integrated on a script i already have: it's a script with a loop to close some windows when they appears in the auto execute section then come some hotkeys. I do the mistake of putting your script after the auto execute section then when i put it after the loop it was obviously not working then i put it before and it works but my loop didn't work anymore, so i put it inside the loop and the two work fine. I already have a hotkey that create a gui to notify me when i suspend the script and i wanted to replicate the same thing when i click on "Suspend Hotkeys" on the menu. I make ajustment with your codes and lexikos's ones but the only problem i have is that when i unsuspend the script the gui is not destroyed (even thought it is destroyed with my hotkey), but i think that will be not hard to figure out,thanks. And by the way the tooltip gui with the shadow on your last script is gorgeous, didn't know that it was possible to have that kind of gui.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

04 Jul 2021, 05:01

necomerx wrote: also sorry boiler your code work fine in the auto-execute section of my script before the loop. I needed to specify that the codes will be integrated on a script i already have: it's a script with a loop to close some windows when they appears in the auto execute section then come some hotkeys. I do the mistake of putting your script after the auto execute section then when i put it after the loop it was obviously not working then i put it before and it works but my loop didn't work anymore, so i put it inside the loop and the two work fine.
It sounds like you are not implementing it correctly in any of the scenarios you mentioned. It’s not correct to put it inside the loop, and both your script and this one would work if you put the correct part (not the entire script) before your loop. You apparently are putting the entire script in the auto-execute section. That is not correct.

This is the only part you put in the auto-execute section, before the loop you already have in your script:

Code: Select all

Notified := 0
SetTimer, SuspendCheck, 300

This rest goes below your auto-execute section. To be safe, put this at the very bottom of your script:

Code: Select all

SuspendCheck:
	if A_IsSuspended {
		if !Notified {
			Notified := 1
			MsgBox, Script is suspended
		}
	}
	else
		Notified := 0
return

Again, it’s better to use lexikos’ approach. I’m just trying to show you how to properly implement a SetTimer routine into your scripts.
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: Are there labels for the default icon menu items?

04 Jul 2021, 06:07

Yes i was integrating it wrong first when i put it after the auto execute section (i think i was so tired!). But when i put your code in the loop, i think i removed the Settimer, the label and the return lines, so it was working fine, but yes how you explain it is the proper way to do it and all is working fine. And yes lexikos' codes are high level, i just need some times to understand how i can integrate it with my lines of codes.
Last edited by necomerx on 04 Jul 2021, 06:23, edited 1 time in total.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

04 Jul 2021, 06:21

The code from lexikos is just as easy to implement. The only line that goes in the auto-execute section (before your loop) is the OnMessage line. The rest is a function that goes below the auto-execute section. Again, just put it at the very bottom of your script. (You don’t need the #Requires line, but if you keep it, put it at the very top of the script.)
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: Are there labels for the default icon menu items?

04 Jul 2021, 07:13

Yes thanks, the lexikos' code is integrated as you suggest and is working fine but with my gui, it is not working properly: when i suspend the script, the gui shows up fine but when i unsuspend it, it is not destroyed, here is the code :

Code: Select all

OnMessage(0x111, Func("Receive_WM_COMMAND"))
Receive_WM_COMMAND(wParam) {
    if (wParam = 65305) { ; Tray, &Suspend Hotkeys
		Suspend
		gui, +AlwaysOnTop +Caption -MinimizeBox
		gui, font, s10 , UD Digi Kyokasho NP-B
		gui, add, text,, SUSPENDED
		gui, Show
		Loop
		{
			SetTimer, label2, 300000
			label2:
			gui, Flash
			return
		}
			2GuiClose:
			gui, Show
			return
		}
			else
		{
			Gui, destroy
		}
		return
}
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

04 Jul 2021, 09:24

The Suspend menu item being selected again (to un-suspend) would not trigger the else part of your code. It’s the same menu item, so it would trigger the if part again.
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: Are there labels for the default icon menu items?

04 Jul 2021, 10:42

Yep right and that's why i'm confused because i don't understand for the moment how i can make the code to work with my gui.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

04 Jul 2021, 12:43

You would check the value of A_IsSuspended to know if that occurrence of the menu item selection was to suspend or un-suspend hotkeys, as lexikos demonstrated.
necomerx
Posts: 57
Joined: 01 Jan 2021, 09:09

Re: Are there labels for the default icon menu items?

04 Jul 2021, 15:43

After many hours of trials, i finally manage to make it work properly, it can seems easy but this one was really tough for me. I could not keep your script boiler because of the timer, the gui was keeping the focus so it was difficult to work on other windows when the script was suspended. I still need to fully understand lexikos' code but i manage to make it work how i wanted. Here is the final code:

Code: Select all

OnMessage(0x111, Func("Receive_WM_COMMAND"))
Receive_WM_COMMAND(wParam) {

    if (wParam = 65305) { ; Tray, &Suspend Hotkeys

     	gui, +AlwaysOnTop +Caption -MinimizeBox
		gui, font, s10 , UD Digi Kyokasho NP-B
		gui, add, text,, SUSPENDED
if A_isSuspended
		{
		gui, destroy		
		return
	 	}
		Suspend
		
		gui, Show,

Loop
{
	SetTimer, label2, 300000
	label2:
	gui, Flash
	return 0

}
	2GuiClose:
	gui, Show
	return
}
}
Thanks you very much for your helps boiler, lexikos. You rock !
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Are there labels for the default icon menu items?

05 Jul 2021, 04:19

boiler wrote:
04 Jul 2021, 06:21
The rest is a function that goes below the auto-execute section. Again, just put it at the very bottom of your script.
One benefit of using functions is that they can be safely interspersed with other auto-execute code. There is no need to split the parts of my example; they can all be placed at the top of the script. Replacing your timer subroutine with a function would give it the same benefit. If someone then tries to place it inside another function with AutoHotkey v1, they will get a very strong hint that it isn't the right place. With AutoHotkey v2 it might work even there.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Are there labels for the default icon menu items?

05 Jul 2021, 05:44

That’s a good point. It would make adding posted code to their scripts more foolproof. Thanks for the suggestion.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, Spawnova and 360 guests