0
votes

In most cases, I can use autohotkey to bind hotkeys for a function/feature/button to applications in Windows.

If a software does not have hotkeys for a button and does not provide features to custom-map keys to that button, I used to use image recognition or location of the button. Both are somewhat easy to break.

Does anyone know if there is a better way to target buttons?

for example, "Bold" in Excel has a hotkey, while the Fill Cell Color does not:

-

enter image description here

-

thanks!

3

3 Answers

0
votes

You could look into control clicking a button on Excel. May or may not work nicely and easily.

For an actually good solution, I'd recommend looking into VBA.
Or if you want to use AHK look into using the Excel ComObject.

Here's a quick little AHK code snippet to turn the currently active cell red:

XL := ComObjActive("Excel.Application") ;reference our open Excel document
XL.ActiveCell.Interior.ColorIndex := 3 

For VBA you'll find loads of tutorials etc with a quick Google search.
For AHK you'll find some as well, but maybe not as many.
Here's one for AHK that seems to cover a good amount of stuff, however, seems like you'll want to use his library for all that he does. I guess be easy if you just want to get the job done, but not very good if you actually want to learn how to do it:
https://www.autohotkey.com/boards/viewtopic.php?t=63032

EDIT:
Just realized Excel was the example, instead of the fill color button. So I'll add some more about control clicking and control sending.
Use some way to try to get the class, or even more preferably the handle, of the control you wish to click or input to. The controlclick/send AHK documentation pages give some example ways for this.
A possibly easy way could be to use WindowSpy.ahk (an ahk script that with the ahk installation, you'll find it in your install folder). Might also be named something like AU3_Spy.exe.
If you can't get a class or hwnd, you can use coordinates.
The documentation also shows this and has examples.

0
votes

One approach you could take is using the Quick Access Toolbar:

  1. Add Fill Cell Color to your Quick Access Toolbar, and
  2. Define a hotkey that navigates to the toolbar option. In my case shown further below, this would be a script that sends the keys Alt,6 in sequence.

You can then select a fill color using arrow and enter keys.

This tested okay for me using the below:

; Go to Fill Cell Color using Alt+Shift+f
#IfWinActive, ahk_class XLMAIN
!+f::
Send !6
Return

The benefit is negligible in this example as you could simply enter Alt,6 manually, but using the Toolbar with AHK is useful in more complex examples.

The downside of this approach is that the script would need to customised for use on a different machine, as the Quick Access Toolbar numbering may vary.


Add to Quick Access Toolbar

Shortcut provided by Excel

-1
votes

there is a method called PerformClick() on a button. Please check is this resolves your issue.