unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Cecilio Pardo <cpardo@imayhem.com>
Cc: 74423@debbugs.gnu.org
Subject: bug#74423: Low level key events
Date: Tue, 19 Nov 2024 11:43:08 -0500	[thread overview]
Message-ID: <jwv4j43jisn.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <d5c72818-e907-43f5-853a-89ec01264157@imayhem.com> (Cecilio Pardo's message of "Mon, 18 Nov 2024 21:35:40 +0100")

> It provides events for press/release of keys, independently of the normal
> keyboard events. These events are bound int the special-event-map. Some lisp
> is included to implement detection of multiple tapping on keys, and running
> commands or simulating modifiers.

I hadn't followed the discussion over at emacs-devel, but this is cool.
I used to have a local patch which generated extra events for all key
presses/releases.  I never wrote any useful Lisp-level code for it
(all I had were bindings to ignore those events so Emacs was still
usable 🙂), but I really think it would make for fun new hacks.

I haven't had time to look at your whole patch, but here are some comments.

> +  (let ((key (cl-third last-input-event)))

Please try and use `event-*` functions.  If none serve (as will often be
the case), define local "replacements" until they can be promoted to
`subr.el`.

> +         ((functionp func) (funcall func))
> +         ((eq 'hyper func)
> +          (message "H-...")
> +          (let ((r (read-event)))
> +            (setq unread-command-events
> +                  (list (event-apply-modifier
> +                         r 'hyper 24 "H-"))))))))))

Move that code to a function, so you can get rid of this `hyper`
special case.  BTW, any reason why you couldn't use
`event-apply-hyper-modifier`?

[ BTW, this becomes more ... interesting ... when you want to be able to
  cumulative that for several modifiers, in which case your `read-event`
  might return an event which is not the one to which you want to add
  `H-`.  ]

> +  switch (keysym)
> +    {
> +    case GDK_KEY_Shift_L: key = Qlshift; break;
> +    case GDK_KEY_Shift_R: key = Qrshift; break;
> +    case GDK_KEY_Control_L: key = Qlctrl; break;
> +    case GDK_KEY_Control_R: key = Qrctrl; break;
> +    case GDK_KEY_Alt_L: key = Qlalt; break;
> +    case GDK_KEY_Alt_R: key = Qralt; break;
> +    default:
> +      key = Qnil;
> +    }
> +
> +   switch (keysym)
> +    {
> +    case GDK_KEY_Shift_L:
> +    case GDK_KEY_Shift_R:
> +      modifier = Qshift;
> +      break;
> +    case GDK_KEY_Control_L:
> +    case GDK_KEY_Control_R:
> +      modifier = Vx_ctrl_keysym;
> +      if (NILP (modifier))
> +	modifier = Qctrl;
> +      break;
> +    case GDK_KEY_Alt_L:
> +    case GDK_KEY_Alt_R:
> +      modifier = Vx_meta_keysym;
> +      if (NILP (modifier))
> +	modifier = Qalt;
> +      break;
> +    case GDK_KEY_Meta_L:
> +    case GDK_KEY_Meta_R:
> +      modifier = Vx_meta_keysym;
> +      if (NILP (modifier))
> +	modifier = Qmeta;
> +      break;
> +    case GDK_KEY_Hyper_L:
> +    case GDK_KEY_Hyper_R:
> +      modifier = Vx_hyper_keysym;
> +      if (NILP (modifier))
> +	modifier = Qhyper;
> +      break;
> +    case GDK_KEY_Super_L:
> +    case GDK_KEY_Super_R:
> +      modifier = Vx_super_keysym;
> +      if (NILP (modifier))
> +	modifier = Qsuper;
> +      break;
> +    default:
> +      modifier = Qnil;
> +    }

I think the list of low-level keys handled here should not be hard-coded.
IOW, maybe `enable-low-level-keys` should not be a boolean but
a list/map/table indicating which keys to handle.

> +  if (!NILP (key))
> +    {
> +      EVENT_INIT (inev.ie);
> +      XSETFRAME (inev.ie.frame_or_window, f);
> +      inev.ie.kind = LOW_LEVEL_KEY_EVENT;
> +      inev.ie.timestamp = xkey.time;
> +      inev.ie.arg = list2 (is_press ? Qt : Qnil, key);
> +      kbd_buffer_store_buffered_event (&inev, &xg_pending_quit_event);
> +    }
> +
> +  if (!NILP (modifier))
> +    {
> +      EVENT_INIT (inev.ie);
> +      XSETFRAME (inev.ie.frame_or_window, f);
> +      inev.ie.kind = LOW_LEVEL_MODIFIER_KEY_EVENT;
> +      inev.ie.timestamp = xkey.time;
> +      inev.ie.arg = list2 (is_press ? Qt : Qnil, modifier);
> +      kbd_buffer_store_buffered_event (&inev, &xg_pending_quit_event);
> +    }
> +}

So, IIUC you might generate 2 low-level events for a single key press?
Why?

Other note: in the distant past (back around Emacs-21) I seem to
remember Gerd making changes to the event structure so as to avoid
allocating Lisp objects for this code.  I think it was related to
problems due to running this low-level event-handler code from within
a C signal handler, which is a practice was have since stopped, luckily,
but maybe there are still good reasons to try and avoid involving
allocating objects into the Lisp heap in this low-level code.


        Stefan






  parent reply	other threads:[~2024-11-19 16:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <fb50ec11-7aec-481e-8a3a-ecdcf22eb7c0@imayhem.com>
     [not found] ` <31bdc55d-8c13-4de0-9cef-bd6cc4fb033f@imayhem.com>
     [not found]   ` <s1r8qtzsvbe.fsf@yahoo.com>
2024-11-18 20:35     ` bug#74423: Low level key events Cecilio Pardo
2024-11-18 23:49       ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-11-19 15:29       ` Eli Zaretskii
2024-11-19 16:43       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-11-19 20:05         ` Cecilio Pardo
2024-11-20  4:21           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwv4j43jisn.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=74423@debbugs.gnu.org \
    --cc=cpardo@imayhem.com \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).