I'm new to AHK, but I've searched through the forums, help and StackOverflow, but couldn't find the solution.
I'm trying to write a script that will locate the scrollbar of the topmost window's control under the mouse.
I have some applications, like PyCharm, that have really, really narrow scrollbars that take forever to grab because I keep overshooting. I would like to press a hotkey and have AHK move the cursor precisely on top of the scrollbar and hold it there until a mouse down event is triggered, in which case control is handed back to the application and I am on the scrollbar.
I'm mostly concerned with the application scrollbar, as in Chrome, but many apps (like PyCharm) have multiple layouts with multiple scrollbars. It seems I need to grab the nearest scrollbar control to the right of the mouse.
I used AHK spy to view the controls for both Chrome and PyCharm. For Chrome, hovering over the window scrollbar yields:
ClassNN: Chrome_RenderWidgetHostHWND1
Text: Chrome Legacy Window
and hovering over any window in PyCharm yields nothing.
Is this possible? Is there a better way?
Grabbing scrollbar control or location
-
- Posts: 27
- Joined: 07 Jun 2017, 13:50
Re: Grabbing scrollbar control or location
I would recommend using AccViewer to investigate the window:
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
Unfortunately I couldn't identify the scrollbar in Google Chrome, using this method.
If that fails other ideas are to use PixelSearch/ImageSearch, or if the scrollbar coordinates tend to be consistent relative to the top-left corner of the window/a control, use that information, and the window XYWH coordinates (use WinGetPos or ControlGetPos).
Btw sometimes scrollbars are separate controls, sometimes they are part of windows/controls.
This works for Notepad (it requires the Acc library, see link above):
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
Unfortunately I couldn't identify the scrollbar in Google Chrome, using this method.
If that fails other ideas are to use PixelSearch/ImageSearch, or if the scrollbar coordinates tend to be consistent relative to the top-left corner of the window/a control, use that information, and the window XYWH coordinates (use WinGetPos or ControlGetPos).
Btw sometimes scrollbars are separate controls, sometimes they are part of windows/controls.
This works for Notepad (it requires the Acc library, see link above):
Code: Select all
q:: ;notepad - move cursor to scrollbar
ControlGet, hCtl, Hwnd,, Edit1, ahk_class Notepad
oAcc := Acc_Get("Object", "5", 0, "ahk_id " hCtl)
oRect := Acc_Location(oAcc)
CoordMode, Mouse, Screen
vMouseX := oRect.x + oRect.w/2
vMouseY := oRect.y + oRect.h/2
MouseMove, % vMouseX, % vMouseY
oAcc := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
-
- Posts: 27
- Joined: 07 Jun 2017, 13:50
Re: Grabbing scrollbar control or location
Thank you for the info. Unfortunately AccViewer can't seem to grab anything for PyCharm either. I was hoping to try this for any widget that has a scrollbar, including text fields, so, yes, they're separate controls.
I'm not sure about image search. That would require me capturing images for every app in which I would use this. Text edits in web pages would be hard too since web pages change all the time. And, it would only work for web pages that I choose to do image search on.
Your little app did work in Notepad for me as well, but notepad has a decent sized scrollbar. It's the really thin ones I hate (and I think are stupid).
I had thought about using an offset relative to the edge of the window, but that will only work for scrollbars for the whole window.
Do you have any other suggestions?
How do I locate the right edge of the window?
Thanks for the help!
I'm not sure about image search. That would require me capturing images for every app in which I would use this. Text edits in web pages would be hard too since web pages change all the time. And, it would only work for web pages that I choose to do image search on.
Your little app did work in Notepad for me as well, but notepad has a decent sized scrollbar. It's the really thin ones I hate (and I think are stupid).
I had thought about using an offset relative to the edge of the window, but that will only work for scrollbars for the whole window.
Do you have any other suggestions?
How do I locate the right edge of the window?
Thanks for the help!
Re: Grabbing scrollbar control or location
To get the right edge: WinGetPos/ControlGetPos, then do X plus W.
For image search, there might be a little square at the top/bottom of the scrollbar, and it would be fast to search for it, or to search for more than one, and compute the coordinates of the one you want relative to the cursor position.
[repeated ImageSearch][ImageSearch: click image every time it's found]
My first project: How hard/easy it is to develop a script for this? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=28023
One possibility is to add your own GUI elements on top of the window, which when clicked send key presses e.g PgUp/PgDn to invoke scrolling.
COM/Acc can be used on Internet Explorer. Acc (and hence AccViewer) I think can be used on Mozilla. That might be helpful. One possibility for interaction with Chrome is Selenium, but not for an existing window, otherwise maybe some Chrome add-on. I only use IE day-to-day.
I don't know if a utility that uses UI Automation would provide any further information, I thought I'd mention it just in case.
For image search, there might be a little square at the top/bottom of the scrollbar, and it would be fast to search for it, or to search for more than one, and compute the coordinates of the one you want relative to the cursor position.
[repeated ImageSearch][ImageSearch: click image every time it's found]
My first project: How hard/easy it is to develop a script for this? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=28023
One possibility is to add your own GUI elements on top of the window, which when clicked send key presses e.g PgUp/PgDn to invoke scrolling.
COM/Acc can be used on Internet Explorer. Acc (and hence AccViewer) I think can be used on Mozilla. That might be helpful. One possibility for interaction with Chrome is Selenium, but not for an existing window, otherwise maybe some Chrome add-on. I only use IE day-to-day.
I don't know if a utility that uses UI Automation would provide any further information, I thought I'd mention it just in case.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
-
- Posts: 27
- Joined: 07 Jun 2017, 13:50
Re: Grabbing scrollbar control or location
This is useful information, thank you. I did as you suggested and made an image out of the little triangle at the bottom of the scrollbar. Image search seems to find the image on some of the apps, but it is slow compared simply grabbing a control's coordinates.
It's curious that in AccViewer, when I click on 'Acc Structure' that the chrome and PyCharm scrollbars do show up, but it's unable to get any coordinates for them. It can get the position for the title bar, but rarely any other elements.
One thing page up/down does is move the text insertion point. I often want to scroll for reference, but keep the text insertion unmoved. Otherwise I would just just the page up/down buttons.
Selenium looks interesting and I'll have to check it out, but one new scripting language at a time for me
Thank you for the help and I'll follow your posts for other useful info!
It's curious that in AccViewer, when I click on 'Acc Structure' that the chrome and PyCharm scrollbars do show up, but it's unable to get any coordinates for them. It can get the position for the title bar, but rarely any other elements.
One thing page up/down does is move the text insertion point. I often want to scroll for reference, but keep the text insertion unmoved. Otherwise I would just just the page up/down buttons.
Selenium looks interesting and I'll have to check it out, but one new scripting language at a time for me
Thank you for the help and I'll follow your posts for other useful info!
Re: Grabbing scrollbar control or location
It's quite simple to use this, and you can use it from within AHK, but I haven't tested it much, and you can't use it on an existing Chrome browser window, you have to create it via Selenium.
Using Selenium with AutoHotkey- Cross browser automation! - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32323
With Acc, if you can't get the coordinates for an element, you could try getting the coordinates for its parent instead. By navigating up the treeview.
With ImageSearch you can restrict the search area, which might be faster.
You could also preload the image, although I'm having issues with this. I.e. load the image from a file once, and then keep the image as an hBitmap or hIcon, instead of loading it from a file each time.
ImageSearch and LoadPicture (transparency and lost hIcon) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=32928
You could do something like WinGetPos and MouseGetPos, and check if the mouse is in the left half of the window. Btw do check the CoordMode command.
Using Selenium with AutoHotkey- Cross browser automation! - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32323
With Acc, if you can't get the coordinates for an element, you could try getting the coordinates for its parent instead. By navigating up the treeview.
With ImageSearch you can restrict the search area, which might be faster.
You could also preload the image, although I'm having issues with this. I.e. load the image from a file once, and then keep the image as an hBitmap or hIcon, instead of loading it from a file each time.
ImageSearch and LoadPicture (transparency and lost hIcon) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=32928
You could do something like WinGetPos and MouseGetPos, and check if the mouse is in the left half of the window. Btw do check the CoordMode command.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA