From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: j@mremus.net Newsgroups: gmane.emacs.bugs Subject: bug#44973: Add a macOS global hotkey function Date: Sun, 20 Dec 2020 16:40:37 -0800 Message-ID: References: <874kkw9gmq.fsf@gnus.org> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000936d6105b6eeb5a5" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8752"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 44973@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 21 02:18:21 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kr9q8-0002B2-Kj for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 21 Dec 2020 02:18:20 +0100 Original-Received: from localhost ([::1]:49732 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kr9q7-0003Pm-Lg for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 20 Dec 2020 20:18:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44494) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kr9pq-0003Pe-RW for bug-gnu-emacs@gnu.org; Sun, 20 Dec 2020 20:18:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:34299) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kr9pq-0005Fh-EK for bug-gnu-emacs@gnu.org; Sun, 20 Dec 2020 20:18:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kr9pq-0002H9-9m for bug-gnu-emacs@gnu.org; Sun, 20 Dec 2020 20:18:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: j@mremus.net Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 21 Dec 2020 01:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44973 X-GNU-PR-Package: emacs Original-Received: via spool by 44973-submit@debbugs.gnu.org id=B44973.16085134428701 (code B ref 44973); Mon, 21 Dec 2020 01:18:02 +0000 Original-Received: (at 44973) by debbugs.gnu.org; 21 Dec 2020 01:17:22 +0000 Original-Received: from localhost ([127.0.0.1]:45845 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kr9pB-0002GG-Om for submit@debbugs.gnu.org; Sun, 20 Dec 2020 20:17:22 -0500 Original-Received: from mail-ot1-f48.google.com ([209.85.210.48]:37193) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kr9Fu-0001Pk-3S for 44973@debbugs.gnu.org; Sun, 20 Dec 2020 19:40:54 -0500 Original-Received: by mail-ot1-f48.google.com with SMTP id o11so7448345ote.4 for <44973@debbugs.gnu.org>; Sun, 20 Dec 2020 16:40:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mremus.net; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=r5SBEhyOs0kGJ3E2ie4iSFTfRcSa1371ZPUrDrAiIhc=; b=RxR4aLl6l1Vh9iQOKrvIn6/zc000ZamJClBL9LXKg3aoW5Wyt1X0ZTQkv8jc88WBd+ Uobn3k9Ct2ROVAl/Mo0F9KUg/oARGNInhTs0DGAwlrHf5+3NgUT69E0hn4sBltqajH9Z oEMbytQtpMRePkNy4Fovk0KySlyd41oRSDucOPhrmcZPzcJV0nyG2CFjlLWH7pz2FHb0 UOE4aWGlcyu7Fq9E4mbJG4deFWnTD1qNPlAugRk3liEC1FCqbmyOZ8rrOYu5hLhuCVnO vZajwMeJdeJ+4CnmOvyelvEN7rrcht3Uitl9kPAwOzsYNvJLiueDgIVaL9T89VfXI8+F P1Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=r5SBEhyOs0kGJ3E2ie4iSFTfRcSa1371ZPUrDrAiIhc=; b=HxThWl6gkt4wUcMd9voZ9JoCyWOi2f66QhAxpz3Wky/zH/6zqlP+azvMcFEl43Capo izCbspyhLN7KeFpmlVElG2KbgMPlx9rNfMhxPB9uX+kcBhD81sajKORD+M5T5HXam4TF byFfHtrhFLM510YCjC9bsIh3oXo7zgvkSFLTbjC+kKNL2RPOrWLJy+t730hn+KtJYybH fc7iFgfEy+f62garoWfdFzKcQMxv079apgAiEVxooCmdLC9gy5HCviqUyHjWSqWzl2kE uS3Zyc7IyF40YswSnnm+cSfbSvFdxNJuFjrnZbjo4zNYgO7/5BRE1SO+w1hROloBoAWE PHCA== X-Gm-Message-State: AOAM530qS5viOGWfrh27uqHpG7IMSGovrirhmpp+QA9WeQvVxG9hyffP qZubYX2R6u6Gfb2huRRidv3KQb/cMM9DLqrZpJj74w== X-Google-Smtp-Source: ABdhPJxdf9qn86YYw5Y1QIh1kUDyiZjekXGinXTsFOqACksAxlNjVRobsBgJec3izgODheop95TVmyhZz34oCwzF79c= X-Received: by 2002:a9d:4b03:: with SMTP id q3mr10270954otf.88.1608511248358; Sun, 20 Dec 2020 16:40:48 -0800 (PST) In-Reply-To: <874kkw9gmq.fsf@gnus.org> X-Mailman-Approved-At: Sun, 20 Dec 2020 20:17:20 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:196513 Archived-At: --000000000000936d6105b6eeb5a5 Content-Type: text/plain; charset="UTF-8" Hi Lars, I'm pretty close to having a patch ready, but I'm stuck in one key spot. I don't know if anyone on the list could help? After the user registers a hotkey, and when they press the hotkey, MacOS will run the code below. On the line "RUN_SOME_ELISP_FUNCTION", I would expect some elisp function to be run (e.g. 'emacs-version'). But no matter what I do, it always crashes the program. I think my first problem is not knowing how to call elisp (run_hooks, safe_call, etc?) correctly, but second, I suspect if this is crashing due to a threading issue. handler = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSEventMaskKeyDown) handler:^(NSEvent *event){ if (event.modifierFlags & modifier) if([event.charactersIgnoringModifiers characterAtIndex:0] == vkey) { RUN_SOME_ELISP_FUNCTION [[NSApp mainWindow] makeKeyAndOrderFront:NSApp]; } } }]; Is there anyone who might know how to fill in this piece? Thanks On Tue, Dec 8, 2020 at 12:39 PM Lars Ingebrigtsen wrote: > j@mremus.net writes: > > > MacOS makes it very difficult to assign global hotkeys to focus > > Emacs. For example, if I'm in another program, and I want to > > launch/focus Emacs with "Alt-SPC", it can't just be set in the system > > preferences; I'd need to use a 3rd-party program like skhd. > > > > Would it be possible to add a function or hook to Emacs which registers > one or > > more global hotkeys (using addGlobalMonitorForEventsMatchingMask), and > then > > when the hotkey is pressed, run an elisp function? > > [...] > > > Even if it's rare for Emacs to implement an OS-specific feature, there > > is some precedent on Windows. If you look at w32-register-hot-keys, > > it's similar to, but different from my request. > > Sure, I think that makes sense. Would it be possible for you to work up > a patch that adds this functionality? > > -- > (domestic pets only, the antidote for overdose, milk.) > bloggy blog: http://lars.ingebrigtsen.no > --000000000000936d6105b6eeb5a5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Lars= ,

I'm pretty close to having a patch ready, but I= 9;m stuck in one key spot. I don't know if anyone on the list could hel= p?

After the user registers a hotkey, and when the= y press the hotkey, MacOS will run the code below. On the line "RUN_SO= ME_ELISP_FUNCTION", I would expect some elisp function to be run (e.g.= 'emacs-version').=C2=A0

But no matter what = I do, it always crashes the program. I think my first problem is not knowin= g how to call elisp (run_hooks, safe_call, etc?) correctly, but second, I s= uspect if this is crashing due to a threading issue.

<= /div>
=C2=A0 =C2=A0 handler =3D [NSEven= t addGlobalMonitorForEventsMatchingMask:(NSEventMaskKeyDown) handler:^(NSEv= ent *event){
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 if (event.modifierFlags & modifier)=C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if([event.charactersIgnoring= Modifiers characterAtIndex:0] =3D=3D vkey) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0RUN_SOME_ELISP_FUNCTION
=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 [[NSApp mainWindow] makeKeyAndOrderFront:NSApp];
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 }];

Is there anyone who might = know how to fill in this piece?

Thanks

On Tue, Dec 8, 2020 at 12:39 PM Lars= Ingebrigtsen <larsi@gnus.org> = wrote:
j@mremus.net writes:

> MacOS makes it very difficult to assign global hotkeys to focus
> Emacs. For example, if I'm in another program, and I want to
> launch/focus Emacs with "Alt-SPC", it can't just be set = in the system
> preferences; I'd need to use a 3rd-party program like skhd.
>
> Would it be possible to add a function or hook to Emacs which register= s one or
> more global hotkeys (using addGlobalMonitorForEventsMatchingMask), and= then
> when the hotkey is pressed, run an elisp function?

[...]

> Even if it's rare for Emacs to implement an OS-specific feature, t= here
> is some precedent on Windows. If you look at w32-register-hot-keys, > it's similar to, but different from my request.

Sure, I think that makes sense.=C2=A0 Would it be possible for you to work = up
a patch that adds this functionality?

--
(domestic pets only, the antidote for overdose, milk.)
=C2=A0 =C2=A0bloggy blog: http://lars.ingebrigtsen.no
--000000000000936d6105b6eeb5a5--