Subject: Re: bug#44973: Add a macOS global hotkey function

From: [email-masked]
Date: Sun, 3 Jan 2021 21:13:47 -0800

Hi Eli, Alan,

Here's a new patch incorporating all of Alan's feedback, with two notes:
1. This implementation is unable to differentiate between left and right
   modifier keys, because it depends upon NSEventModifierFlags, which
doesn't
   differentiate between left and right.
2. hotkey_ids is a global variable, so it should not be released.

To address Eli's feedback, this patch is inspired by
https://www.mattduck.com/emacs-fuzzy-launcher.html , where Matt uses emacs
to
create a launcher like Spotlight or Alfred. Since he's using GNU/Linux and
i3 wm, he
simply assigned a global hotkey in his window manager config to run
emacsclient
and run the launcher. I believe this is also easy to do in Windows.

In macOS this is much more difficult, because assigning a global hotkey can
only
be done in code. So a user would have to rely on a utility like skhd or
Hammerspoon.

My proposal is to add this patch to emacs, so that a user can assign a
global
hotkey in emacs without relying on a 3rd-party program.

I used w32-register-hot-key as an example of another instance where emacs
added
an OS-specific feature in order to provide a consistent user experience
across
platforms. Also, even though the behavior is totally different, the actual
implementation of that routine and mine are very similar.

Thanks for all of your time and feedback.

On Wed, Dec 30, 2020 at 9:08 AM Eli Zaretskii <[email-masked]> wrote:

> > From: [email-masked]
> > Date: Tue, 29 Dec 2020 20:10:00 -0800
> > Cc: [email-masked]
> >
> > Here is the patch to bind a global hotkey in mac. As long as Emacs is
> set as
> > "trusted" in macOS preferences, the user can bind a two-key hotkey of the
> > form [modifier-key] or a single-key modifier [function key], for example
> > (mac-bind-global-hotkey [f1] 'tetris). Binding a three-key
> > combo is left to a future patch.
> >
> > The code is copied from w32-register-hot-key as much as possible.
>
> Hmm...  w32-register-hot-key is not for binding Emacs commands to
> platform-specific keys, it is so that the OS doesn't catch some key
> combinations ahead of Emacs.  That is, by using w32-register-hot-key
> you make the key combination available for binding to a command using
> global-set-key and the likes.
>
> By contrast, it sounds like your patch is for letting users bind
> platform-specific key sequences to Emacs commands, which is something
> quite different.  What is the rationale for adding this functionality
> to Emacs?
>