Trigger an AutoHotkey script when Windows orb (menu button) is clicked - autohotkey

I have an AutoHotkey script that I would like to run whenever the Windows 7 start menu orb is clicked (the button on the bottom left that brings up the menu).
I've been using MouseGetPos previously to determine when the cursor is hovering over the region, but this causes the script to be activated even when running fullscreen applications. Is there another way I can use to detect when the start menu orb is clicked? Thanks.

MouseGetPos,,, id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
if (title = "Start" || (class = "Shell_TrayWnd" && control = "")) {
;Do script
}
title = "Start" covers most of the orb, except for a few pixels at the corners.
class = "Shell_TrayWnd" refers to the entire taskbar, but only the few pixels around the orb do not have a value for control.
This should cover the entire region where normally clicking would bring out the Windows menu.

Related

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

AHK shortcuts for Alt+Space in Windows

running AHK in Windows 10, and I'm trying to create shortcuts for the Alt+Space menu. Specifically, I often use this sequence:
Press keys Alt+Space (bring up the window menu)
Press key S (select Size to resize)
Press key Left (now moving the mouse is resizing your window horizontally)
Press key Up (now moving the mouse is resizing your window in all directions)
Once this sequence is pressed, you can move your mouse to resize the active window as if you click-dragged the top-right corner, without having to hunt for that corner. (to finalize the resizing you can press Enter of LeftMouseKey, or Esc to abort). I'd like to get to this state with a single shortcut.
Here is my current script, binding to Winkey+Ctrl+Shift+1
;resize window from Top-Left
#^+1::
SendInput !{Space}
Sleep 100
SendInput s
Sleep 100
SendInput {Left}+{Up}
Return
It works mostly, but sometimes the active window will consume the s {Left} {Up} commands, rather than the popup menu. Thus, sometimes this shortcut will result in the active window like VSCode having the "s" character and the cursor 1 line up from before (as if typing s {Left} {Up}), and a visible Alt+Space menu.
I initially used Sleep 10 and thought Sleep 100 would fix this, but it didn't. The shortcut already feels slow with 2x Sleep 100 built in.
I'd like to test if the Alt+Space menu is open before SendInput s and preferably make sure I'm sending to the menu rather than the main application.
I was unable to reproduce the problem using your method. Perhaps try using Send, SendEvent, SendPlay, SendRaw, #InstallKeybdHook, #UseHook
Alternatively, use Autohotkey's WinMove statement:
This resizes the active window such that the upper left hand corner is at the current mouse position
#^+1::
CoordMode Mouse, screen
id := WinExist("A")
WinGetPos x, y, width, height, ahk_id %id%
MouseGetPos mx, my
neww := width + x - mx
newh := height + y - my
WinMove % "ahk_id" id,, mx, my, neww, newh
return
The menu itself is ahk_class #32768, so waiting for it to exist seems to work for me.
#^+1::
Send , !{space}
WinWait , ahk_class #32768 ,, 1 ; Waits 1s for menu to exist
If !ErrorLevel ; ErrorLevel is 0 if menu exists
Send , s{left}+{up}
Return
Jim U's alternative solution is a more reliable way of doing what you're trying to achieve, but this will make what you currently have work.

autohotkey type in another window with gui

Autohotkey Example Needed
Need some help, please. I've searched and can't seem to find an example of what I need.
What I want to do is create a ahk dialog box with a button (I can do this part), and when I click on it, it will type some text into another window. Basically, I want want to offload the "shortcut" to a "mouse click". But, without mapping a shortcut.
Something like this:
When user clicks BOX1, "text" is stored. Then, when user clicks elsewhere, vBOX1 is typed into the cursor location of the window activated by that click
I hope I'm explaining this succinctly. Any help would be appreciated.
Here are two possible alternatives:
First alternative expands #scso's suggestion:
~LButton::
sleep, 200 ;give the window below the cursor some time to get activated
Send, %vBOX1%
return
Now this may seem fine but what it actually does is type the text EVERY time you click the mouse in ANY window. Let's put an additional check so if vBOX1 is empty it doesn't type anything.
~LButton::
sleep, 200 ;give the window below the cursor some time to get activated
If (vBOX1 != "")
{
Send, %vBOX1%
vBOX1 := "" ; clears the contents of vBOX1
}
return
Second alternative:
You use the mouse clicks normally and the text gets typed only when you do Control + Click.
So in order to type the text you need to click once to select the window and then control+click to do the actual typing
~^LButton::
Send, %vBOX1%
vBOX1 := "" ; clears the contents of vBOX1
return
You can expand both alternatives by adding commands for detecting the active window and then typing the text or changing the mouse click combination to something else.

Right controlClick on ToolbarWindow321 autohotkey

I'm quite new in autohotkey and I'm currently facing a misunderstanding about the ControlClick.
My aim is to right click on an icon on the toolbarWindow321 with a ControlClick to display the contextual menu. I do not want to use a Click or a Send as the action could be done when the session is locked on.
I search for a while in the web and tried several things.
ControlClick,,ahk_class Shell_TrayWnd,,R,NA x1500 y22
This thing works fine if I wanted to have the dropdown menu of the toolbar. That's not the case.
I tried something like this:
ControlClick,ToolbarWindow321,ahk_class Shell_TrayWnd,,Right,1,NA x1500 y22
But nothing is shown. I tried several coordinates, and used AutoIt3 spy to determined the position of my icon.
I'm certain I've done something wrong (of course or it would be working fine :))
Does somebody have an idea of what I must do to make it work?
I wrote some code to click on the Desktop button in Notepad's Save As window.
On the Windows XP version of Notepad this was a ToolbarWindow32 control.
The code uses functions from the Acc library, which you can put in your script's Lib folder.
Acc Library [AHK_L] (updated 09/27/2012) - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/77303-acc-library-ahk-l-updated-09272012/
^q::
ControlGet, hCtl, Hwnd, , ToolbarWindow322, A
if !hCtl
Return
oAcc := Acc_Get("Object", "tool_bar", 0, "ahk_id " hCtl)
Loop, % oAcc.accChildCount
if (oAcc.accName(A_Index) = "Desktop")
if (1, oAcc.accDoDefaultAction(A_Index))
break
Return
EDIT: In your particular case, accDoDefaultAction might not correspond to right-click.
For reference, my script that works on both Windows XP and 7:
^q:: ;notepad (save as) - click Desktop button
^d:: ;notepad (save as) - click Desktop button
WinGet, hWnd, ID, A
hCtl := ""
if !hCtl ;check for treeview e.g. Win 7
{
ControlGet, hCtl, Hwnd, , SysTreeView321, ahk_id %hWnd%
if hCtl
oAcc := Acc_Get("Object", "outline", 0, "ahk_id " hCtl)
}
if !hCtl ;check for toolbar e.g. Win XP
{
ControlGet, hCtl, Hwnd, , ToolbarWindow322, ahk_id %hWnd%
if hCtl
oAcc := Acc_Get("Object", "tool_bar", 0, "ahk_id " hCtl)
}
Loop, % oAcc.accChildCount
if (oAcc.accName(A_Index) = "Desktop")
if (1, oAcc.accDoDefaultAction(A_Index))
break
Return
This issue may seem like a relatively trivial thing, a 'read the manual' question, but I remember having problems with it myself.
Arguably the method at the bottom should work like the one above, but doesn't.
;notepad save as (windows xp version) left-click Desktop button)
ControlClick, ToolbarWindow322, A, , , , NA x40 y100
;taskbar (windows 7) right-click taskbar button
ControlClick, x260 y20, ahk_class Shell_TrayWnd, , R
ControlClick, x260 y20, ahk_class Shell_TrayWnd, , R, NA
;taskbar (windows 7) right-click taskbar button (DIDN'T WORK)
;(clicked the wrong part of the taskbar, at the far right I believe)
ControlClick, , ahk_class Shell_TrayWnd, , R, x260 y20
ControlClick, , ahk_class Shell_TrayWnd, , R, NA x260 y20
Notes on your code:
this is of the form that didn't work for me either:
ControlClick,,ahk_class Shell_TrayWnd,,R,NA x1500 y22
I believe this:
ControlClick,ToolbarWindow321,ahk_class Shell_TrayWnd,,Right,1,NA x1500 y22
should be this:
ControlClick,ToolbarWindow321,ahk_class Shell_TrayWnd,,Right,NA x1500 y22
with the '1' removed
Further note:
You may be able to use Acc to identify the coordinates of a button, and then use ControlClick to right-click it.

How to obtain textual contents from a window

I have a window that displays a book. In two smaller boxes below, there is page number and volume information of the book that is open. I can get that information easily as follows:
ControlGetText, volume, ThunderRT6TextBox3
ControlGetText, page, ThunderRT6TextBox2
Then my script makes my mouse pointer move to a button. It clicks it, and a new window pops open. In that window, there is more textual information related to the book, such as publisher, name author, edition etc. I want to retrieve that information. But when I try the same strategy it is not working, eg:
ControlGetText, data, RichTextWndClass3
The only difference between the two cases, is that those two small boxes are editable, you can enter text whereas this window is static.
I tried many other options such as:
SendEvent ^a
Which is equivalent to control + a, which should select everything. I tried putting pauses but it would never select. I tried the script to manually double click on that window. It works, and one word gets select like that. Even then SendEvent ^a doesn't do anything.
However, if I do SendEvent ^{insert}, then the selected word gets copied to my clipboard.
I experimented with more combinations:
ControlSend ahk_class ThunderRT6FormDC, ^a
ControlSend ClassNN RichTextWndClass3, ^a
and
ControlSend ThunderRT6FormDC, ^a
ControlSend RichTextWndClass3, ^a
None of them work. All text selection does not manifest itself in that window.
The only alternative remaining for me is to make the script do a manual selection of the entire text. However, this is slow and very ridiculous. Moreover, in Window Spy under the section: Visible Window Text, the text is all there. I tried many other possibilities and I am at the end of my wits. How am I to harvest that text directly?
EDIT--
The text of the window shows in Window Spy under the heading: TitleMatchMode=slow Visible Text, NOT the heading: Visible Window Text
EDIT--
I spoke to you about two windows. The first one in which i get volume and page number. The second one which needs to pop up by pressing a button.
Both these windows have the same class-name:
ahk_class ThunderRT6MDIForm
Is that problematic in any way?
EDIT--
The conclusion is that it is impossible for me to get that text from the second window directly. As such, I opted for the lame, boring manual method. I send out a {shift down} to the active window and then do a click at the bottom of the window. Then I save the selection to my clipboard. It works, but it is just stupid. Please help me find a more elegant solution than this one.
This is the code I used:
ControlGetText, volume, ThunderRT6TextBox3
ControlGetText, page, ThunderRT6TextBox2
Click, 110, 70
sleep 1000
SendInput {shift down}
click 29, 490
SendInput {shift up}
sleep 1000
SendInput, ^{ins}
sleep 100
It is funny to note that real keyboard keys, such as a b c are not possible. But I am able to send a ctrl, shift and an ins. As I noted above, ^c was also giving issues just like ^a was giving issues.
This routine will do the job of getting and returning from the active window the following text sections:
- EdtWindowTextFastVisible
- EdtWindowTextSlowVisible
- EdtWindowTextFastHidden
- EdtWindowTextSlowHidden
MyGetWindowText(ByRef EdtWindowTextFastVisible, ByRef EdtWindowTextSlowVisible, ByRef EdtWindowTextFastHidden,ByRef EdtWindowTextSlowHidden)
{
; Source: https://code.google.com/p/autohotkey-cn/source/browse/trunk/Source/AHK_Window_Info/AHK_Window_Info_v1.7.ahk?r=6
EdtWindowTextFastVisible =
EdtWindowTextSlowVisible =
EdtWindowTextFastHidden =
EdtWindowTextSlowHidden =
WindowControlTextSize = 32767
VarSetCapacity(WindowControlText, WindowControlTextSize)
WinGet, WindowUniqueID, ID, A
;Suggested by Chris
WinGet, ListOfControlHandles, ControlListHwnd, ahk_id %WindowUniqueID% ; Requires v1.0.43.06+.
Loop, Parse, ListOfControlHandles, `n
{
text_is_fast := true
If not DllCall("GetWindowText", "uint", A_LoopField, "str", WindowControlText, "int", WindowControlTextSize)
{
text_is_fast := false
SendMessage, 0xD, WindowControlTextSize, &WindowControlText,, ahk_id %A_LoopField% ; 0xD is WM_GETTEXT
}
If (WindowControlText <> ""){
ControlGet, WindowControlStyle, Style,,, ahk_id %A_LoopField%
If (WindowControlStyle & 0x10000000)
{ ; Control is visible vs. hidden (WS_VISIBLE).
If text_is_fast
EdtWindowTextFastVisible = %EdtWindowTextFastVisible%%WindowControlText%`r`n
Else
EdtWindowTextSlowVisible = %EdtWindowTextSlowVisible%%WindowControlText%`r`n
} Else
{ ; Hidden text.
If text_is_fast
EdtWindowTextFastHidden = %EdtWindowTextFastHidden%%WindowControlText%`r`n
Else
EdtWindowTextSlowHidden = %EdtWindowTextSlowHidden%%WindowControlText%`r`n
}
}
}
;EdtWindowTextFastVisibleFull := ShowOnlyAPartInGui("EdtWindowTextFastVisible", EdtWindowTextFastVisible, 400)
;EdtWindowTextSlowVisibleFull := ShowOnlyAPartInGui("EdtWindowTextSlowVisible", EdtWindowTextSlowVisible, 400)
;EdtWindowTextFastHiddenFull := ShowOnlyAPartInGui("EdtWindowTextFastHidden", EdtWindowTextFastHidden, 400)
;EdtWindowTextSlowHiddenFull := ShowOnlyAPartInGui("EdtWindowTextSlowHidden", EdtWindowTextSlowHidden, 400)
Return
}
There is an autohotkey script that emulates most of the window spy logic. It is called AHK_Window_Info_v1.7.ahk. The nice thing is... you can run it to see if your second window text if visible to this script and if so... the logic needed to pull the information is available inside the script. Here is a link to the webpage and the script is available through SKANs dropbox link on that page. http://www.autohotkey.com/board/topic/8204-ahk-window-info-17/