October 27, 2020

Global Hotkey Launcher

Most of the PC applicaitons provide hotkeys but they can only be accessed if the program window is activated. (no global hotkey)
Moreover hotkeys are sometimes hard to remember.
I present here an approach using AutoHotkey and a launcher app to easily launch program-specific features even when the program is not activated and using an easy to remember keyword syntax.
I illustrate this approach for Microsoft Teams and Microsoft Outlook.

Main idea

Launcher Keyword -> Command

AutoHotkey Implementation

Wrapping the AHK script for command line call

The AHK script will redirect the parameters to the main function.
See the use of the A_Args argument.
Outlooky(A_Args[1])
ExitApp

Outlooky(sInput){
...    
}

Parsing the input parameters

The argument is then parsed with a switch / case structured.

Outlooky(sInput){
    
If (!sInput) { ; empty
    WinId := GetMainWindow()
    WinActivate, ahk_id %WinId%
    return
}

FoundPos := InStr(sInput," ")  
If FoundPos {
    sKeyword := SubStr(sInput,1,FoundPos-1)
    sInput := SubStr(sInput,FoundPos+1)
} Else {
    sKeyword := sInput
    sInput =
}

Switch sKeyword
{
Case "w": ; Web App
    Switch sInput
    {
    Case "m": ; mail
        Run, https://outlook.office.com/mail/
  ...
    return
}

WinId := GetMainWindow()
WinActivate, ahk_id %WinId%

Switch sKeyword
{
Case "n": ; new
    Switch sInput

You can extend the plugin to accept additional arguments as a single keyword.
For example teamsy w cal: will open the Teams calendar in the browser/web app.

Get Program Main Window

You have to be careful in case the program can have multiple windows, how to identify in which window the hotkey shall be run.

For runtime optimization, I store the main Window Id in the registry

Microsoft Teams Example

For Microsoft Teams an hotkey may only run in the main program window while another only in the current meeting window. See this separate post for details how I have handle this issue. 

Running the Teams.exe will activate the main Teams window (single instance)

Microsoft Outlook Example

For Microsoft Outlook, you can identify the main Outlook window by its window title ending with " - Outlook".

Note that contrary to Teams, Outlook supports multiple instances: running outlook.exe will not activate the current main window but open another one.

Such an implementation is illustrated in this Outlooky.ahk file - SubFunction GetMainWindow

Mapping the hotkeys to keyword-based command

Example: Outlook - Open Calendar

The Hotkey for this is Ctrl+2.
The script below is a bit more advanced: it will not run the hotkey if the Calendar view is already active.

You can do also hotkeys combination for example to click on some ribbon buttons via activating them with the Alt key.

Example below shows how to create a Teams meeting:
Press Alt+H to activate the Home ribbon and the corresponding letter (Y1) for the New Teams Meeting button.


Switch sKeyword
{
Case "c","cal":
    WinGetTitle Title, A
    If ! RegExMatch(Title,"^Calendar.* - Outlook$") {
        SendInput ^2 ; open calendar
    }
    return
Case "t": ; new Teams meeting
    WinGetTitle Title, A
    If ! RegExMatch(Title,"^Calendar.* - Outlook$") {
            SendInput ^2 ; open calendar
            While !RegExMatch(Title,"^Calendar.* - Outlook$") {
                WinGetTitle Title, A
                Sleep 500
            }
    }
    SendInput !h ; Home Tab
    Sleep 500
    SendInput y1
    return
} ; End Switch

Example: Teams Mute or Share Desktop

Switch sKeyword
{
..
Case "sh","share":  
    WinId := Teams_GetMeetingWindow()
    If !WinId ; empty
        return
    WinActivate, ahk_id %WinId%
    SendInput ^+e ; ctrl+shift+e 
    sleep, 1000
    SendInput {Tab}{Enter} 
    return
Case "mu","mute":  
    WinId := Teams_GetMainWindow()
    If !WinId ; empty
        return
    WinActivate, ahk_id %WinId%
    SendInput ^+m ; ctrl+shift+m 
    return
The keyword mu (for mute) or sh or share is much easier for me to remember than the native program hotkey. And I don't have to activate the meeting window.

Plugin Delivery

You can compile and deliver the launcher plugin in a standalone Exe file using ahk2exe.
Users won't need to have AutoHotkey setup to run it.

Plugin Setup

You requires of course to use a Launcher. 
I personally recommends Executor.

Then you shall add a Keyword as shown below:


As command choose the .exe or .ahk file.
You shall pass the rest of the command as parameters ("$P$").

See also

Teamsy: Launcher Plugin for Microsoft Teams

Outlooky: Launcher Plugin for Microsoft Outlook (in work)

How to get Teams main or meeting window?

Executor: my preferred App Launcher


No comments:

Post a Comment