gkeenley
gkeenley

Reputation: 7398

Can you make a keyboard shortcut to select a button on a website?

This question is mostly just out of curiosity at what's possible. I'm interested to know any ideas at all.

If I'm on a website, is it possible to make a keyboard shortcut for clicking a button on that site? For example, if I'm on google.com, is it possible to write a script somewhere on my own system that will detect that I'm on google.com and make it so that if I press ctrl-g while that site is in focus, it will click Google Search?

If you know for sure that this isn't possible and have an explanation for why, I'd be interested to know that too.

Upvotes: 1

Views: 1238

Answers (1)

Kangaxx
Kangaxx

Reputation: 81

Yes, it's possible.

I'll briefly describe how to do it in C# and Chrome, but it's not language-dependent and could be used in Edge or IE either.

  1. Get the currently focused window by calling GetForegroundWindow API.
  2. Get the process id by calling the GetWindowThreadProcessId API.
  3. Get the process name by process id. Proceed if it's Chrome.
  4. Get access to UI Automation COM interface. You will need to consume the COM interface in your language, but in C# it's quite easy: you'll need to add a reference to COM -> UIAutomationClient to your project. This will generate a wrapper for you, so the COM interface will be accessible in the code in a usual OO-style.
  5. In Chrome, the Accessibility options must be enabled first to get access to UIA. It could be done manually by navigating to chrome://accessibility/, but it's possible to do it programmatically as well.
  6. Use UI Automation to detect the active URL. You will need to find the Address bar in the UI tree using UIA by its Name or ClassName or another unique identifier. You could first retrieve the UI Automation element for the Chrome window and then use the FindAll method to find the Address bar in the subtree to get its Value property which should contain the current URL.
    Inspect tool will help you to investigate the Chrome UIA element tree and element properties (like Name or ClassName).
  7. Do the same steps to find the Google Search button in the UI tree using UIA. Call GetClickablePoint method to get its clickable point.
  8. Click on the button programmatically using Win API.
  9. To do this on a global shortcut, you could place a keyboard hook by calling SetWindowsHookEx API. Or use a library. Here is another way.

That's it, easier than it looks.

Upvotes: 1

Related Questions