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.
Related
I'm pretty new to ahk.
Say I have two separate vlc windows open and playing videos.
If one of the vlc windows is active, then I want to be able to hold a key to make all keypresses go to the other vlc window.
ie if I press Left, then it sends Left to the active vlc window, but if I hold ALT+Left, then it sends Left to the other vlc window.
If no vlc window is active, then I don't want autohotkey to do anything.
This is so I can control two vlc windows without having to click and choose which one is active.
I looked up GroupAdd hoping I could use a group that includes both vlc windows, but couldn't find a way to target specific vlc windows from the group to send keys to it.
edit: I got a very basic version working, but I feel it's pretty ugly. I would like a way to send any key that's pressed if you hold Shift to the other vlc window. Also this seems a bit unreliable in switching focus if you use it a few times quickly in succession.
GroupAdd, vlcgroup, ahk_exe vlc.exe
return
#IfWinActive ahk_exe vlc.exe
+Left:: ; shift-left
GroupActivate, vlcgroup, r
Send {Left}
GroupActivate, vlcgroup, r
return
A simple approach could be done like this:
#IfWinActive, ahk_exe vlc.exe
+Left::
WinGet, WindowList, List, ahk_exe vlc.exe
BottomMostVlcHwnd := WindowList%WindowList%
ControlSend, , {Left}, % "ahk_id " BottomMostVlcHwnd
return
^+Left::
WinGet, WindowList, List, ahk_exe vlc.exe
BottomMostVlcHwnd := WindowList%WindowList%
ControlSend, , ^{Left}, % "ahk_id " BottomMostVlcHwnd
return
...
#IfWinActive
You'd write out each hotkey.
WinGet, , List(docs) returns a legacy pseudo-array(docs) of hwnds to Vlc windows.
The last element in that array will be the bottom most window.
You can get the last element of a pseudo array via a dynamic variable trick WindowList%WindowList%.
Essentially you're accessing a variable named WindowListN, where N is the number for the last element in the pseudo-array.
Then ControlSend(docs) is used to send keys to the background window without the need to activate it.
And you refer to the background window by a window name like ahk_id 0x1234567(docs).
Of course writing out the hotkey for each key is pretty repetitive, so we can do something much better:
#IfWinActive, ahk_exe vlc.exe
+Left::
^+Left::
+Right::
^+Right::
+PgUp::
+PgDn::
WinGet, WindowList, List, ahk_exe vlc.exe
ControlSend, , % StrReplace(A_ThisHotkey, "+", "{") "}", % "ahk_id " WindowList%WindowList%
return
#IfWinActive
The hotkey definitions are just stacked on top of each other.
(If there were even more definitions, a loop with the Hotkey(docs) command could be nicer)
A_ThisHotkey(docs) will contain the hotkey that was used, and StrReplace(docs) is used to automatically replace the + with a {.
And finally the closing brace } is appended to the end.
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.
I am trying to use ControlGet, List, to get the contents of a listview in AutoHotKey. This listview is in a simple VB6 app that I wrote. I am using:
ControlGet, List, List,, ListViewWndClass1, WindowTitle
ListViewWndClass1 is the class name I got from ActiveWindowInfo.
When I look at the return value for List, it is blank. Also ErrorLevel is 1.
I also tried:
ControlGet, List, List, Selected, SysListView321, Downloads
to get the contents of an Explorer window (Downloads) and this returned an ErrorLevel 1.
Any ideas what I am doing wrong here?
I am happy to use PostMessage / SendMessage or DllCall( "SendMessage", to get the contents of the list control.
Has anyone successfully gotten the contents of a listview control using AHK?
Some current controls look like listviews, but are different from the old standard listview controls.
For example, Windows XP folders did use standard listviews, but Windows 7 does not.
Presuming you are using an up-to-date version of AutoHotkey, on a standard listview control,
it should be able to retrieve the text, unless it belongs to a program that deliberately prevents
retrieval of the text (although you should still be able to get a count of the listview items).
If AutoHotkey's AccViewer can retrieve the text of a listview/listview-like control,
then this means that you should be able to retrieve it via Acc.ahk and its functions.
If a control is a standard listview, you have to get the ControlGet parameters exactly right or the text retrieval won't work:
;get window by title
ControlGet, vText, List, , SysListView321, Media Player Classic Home Cinema
;get window by class
ControlGet, vText, List, , SysListView321, ahk_class MediaPlayerClassicW
;get active window
ControlGet, vText, List, , SysListView321, A
;display the text
MsgBox %vText%
;put the text on the clipboard
Clipboard := vText
;e.g. get text via the Acc library
;note: requires Acc.ahk library in AutoHotkey\Lib folder
;https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
;on right of screen right-click Raw, Save target as...
^q::
ControlGet, hCtl, Hwnd, , SysListView321, A
if !hCtl
Return
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hCtl)
Loop, % oAcc.accChildCount
if (Acc_Role(oAcc, A_Index) = "list item")
vText .= oAcc.accName(A_Index) "`r`n"
Clipboard := vText
MsgBox %vText%
Return
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/
The following AHK script automatically brings up the menu that you would normally get when you right-click on the uTorrent icon in the system task-bar tray:
DetectHiddenWindows, On
Process, Exist, utorrent.exe
WinGet, W, List, ahk_pid %ErrorLevel%
Loop %W%
{
WinGetClass, Class, % "ahk_id" W%A_Index%
If InStr( Class, "µTorrent" ) {
hWnd := W%A_Index%
Break
}}
PostMessage, 0x8001, 0,0x204,, ahk_id %hWnd% ; Right Click down
PostMessage, 0x8001, 0,0x205,, ahk_id %hWnd% ; Right Click Up
I understand most of it except the last two lines. 0x8001 is WM_APP+1. Why is a message in the WM_APP range used, and what exactly happens when these two messages are issued? Many thanks for the explanation.
According to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx#system_defined)
If your application is marked version 4.0, you can use message-identifier values in the range 0x8000 (WM_APP) through 0xBFFF for private messages.
So this would indicate that 0x8001 is an identifier that utorrent has chosen for a message. The 0x204 and 0x205 values are for the Right-Click Down and Up events respectively. My guess is that this code is intended to simulate a Right-Click on utorrent's icon in the Windows tray.
Also, if you're using Autohotkey_L the code can probably be simplified to three lines and perform the same:
DetectHiddenWindows, On
PostMessage, 0x8001, 0,0x204,, ahk_exe utorrent.exe ahk_class µTorrent ; Right Click down
PostMessage, 0x8001, 0,0x205,, ahk_exe utorrent.exe ahk_class µTorrent ; Right Click Up
When you right click on something, the context menu is shown. The context menu can also be shown by pressing the appskey button on the keyboard. When you press it, Windows sends the application a message, i.e., the WM_APP message. Most applications respond by showing a context menu. In the case of the tray icon, the response is the same as the response to a right-click.
Seems that the WM_APP+1 message number is a choice made in the development of the application, in this case utorrent. Could have been another message #, nothing magic. I believe the code I presented (which is from the AHK forums) figured out the message to send through reverse engineering.