Original (and messy) text was sent to the sawfish ML. Here much improved.
This gives little control in case the invoked command decides to grab actively; it may be too late, because other key events have already been distributed to the focused window.
the grab ends with the release of the grabbed key. That means that if we grab H-i, this sequence (which might occur if i type quickly) Hyper-press, i-press, Hyper-Release, e-Press, i-Release will deliver the "e-press" to the grabber. This is undesired! But it can occur by mistake (wrong synchronization of fingers).
I think, that this is a non-intended feature, but i don't know of any proposal for different behaviour.
If the lisp code has a possibility to grab-keyboard, it seems plausible, that the C code should not disregard it, and should not run `allow-events' `ungrab-keyboard' without precise rules.
C might take care about skipping over modifier events, key-releases. C is told what to do through keymaps, 2 variables eval-key-release-events and eval_modifier_events and the unbound-key-hook.
My reasoning is this: If lisp requests more key events, and eval-key-release-events or eval-modifier-events indicate that lisp is not interested in an arrived event (because it is a modifier, or a keyRelease), the C code should request the next event.
note: there is currently no way to grab keyboard and prevent C from calling any XAllowEvent. There is code for it, and once I used it (to freeze the keyboard for some time), but since then i changed the lisp--C synchronization and didn't update this feature.
As i explained above, we have to ungrab explicitely, instead of using "passive" key grab. Also, ungrabbing has to be synchronized with change of focused window: we ungrab "into" a certain window. We don't want any following key-events to be delivered to the previously focused window, or any other random window.
So the (possible) XSetInputFocus must be issued before XUngrabKeyboard. And since C postpones the XSetInputFocus, the lisp code must delegate the C part for (postponed) ungrabbing as well.
ungrab_into_focused_window
All these function are ready to be used from repl.
Please note, that when entering (recursive-edit) you have to (allow-events 'sync-keyboard) before, from lisp! The usual C part does not follow! This needs fix.
http://www.std.org/~msm/common/protocol.pdf
new feature to have a look on key events before C processes them.