* bug#72555: 30.0.60; [PATCH] NS: support passing key events to the system
@ 2024-08-10 11:47 Kai Ma
2024-08-10 19:46 ` Kai Ma
0 siblings, 1 reply; 3+ messages in thread
From: Kai Ma @ 2024-08-10 11:47 UTC (permalink / raw)
To: 72555
[-- Attachment #1: Type: text/plain, Size: 1316 bytes --]
The attached patch adds ns-pass-keys-to-system and makes Emacs to send
the key events to the system input manager when it's set. This can
correct a number of problems that are hard to work around due to the
current key handling logic of Emacs NSport.
(1) The keydown event of Shift+Enter is never sent to the system input
method. This causes an annoying problem for system input methods: if
you press Shift+Enter in Emacs (that is three events: Shift down,
Shift+Enter down, Shift up), the input method only sees Shift down and
up. However, many input methods interprets a click of Shift differently
(mostly toggling ASCII mode), and this causes unwanted behavior. (This
was reported as #38293 in 2019.)
This can only be fixed by Emacs, because to input methods, Shift+Enter
is indistinguishable from a single Shift.
(2) The input method may define some special keys and Emacs cannot make
use of them.
(3) Some system level shortcuts never work in Emacs. For example,
Rectangle (think of it as a third-party window manager in macOS) will
not receive any requests from Emacs windows.
The fix is mostly modeled after Mitsuharu's Mac port, with a few
adjustments for NS port. I've been using it for some time and there are
no more defects known to me, so I decide it's time to send it to you.
Regards,
Kai
[-- Attachment #2: 0001-NS-Pass-key-events-to-system-first.patch --]
[-- Type: application/octet-stream, Size: 4850 bytes --]
From 6e0a8ac127ce5da7fb50aca2fe63819719d2740f Mon Sep 17 00:00:00 2001
From: Kai Ma <justksqsf@gmail.com>
Date: Sun, 11 Jun 2023 14:29:25 +0800
Subject: [PATCH] NS: Pass key events to system first.
* src/nsterm.h (@interface EmacsView): Add keyEventsInterpreted and
rawKeyEvent.
* src/nsterm.m (syms_of_nsterm): Add ns-pass-keys-to-system.
([EmacsView keyDown]): Interpret key events first if
ns-pass-keys-to-system is set.
([EmacsView insertText]): Adjust.
([EmacsView doCommandBySelector]): Adjust.
([EmacsView markedRange]): Adjust.
([EmacsView selectedRange]): Adjust.
---
src/nsterm.h | 2 ++
src/nsterm.m | 44 ++++++++++++++++++++++++++++++++++++++++----
2 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/src/nsterm.h b/src/nsterm.h
index 480f9febe5d..0ee01696f2c 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -481,6 +481,8 @@ #define NSTRACE_UNSILENCE()
int maximized_width, maximized_height;
EmacsWindow *nonfs_window;
BOOL fs_is_native;
+ BOOL keyEventsInterpreted;
+ NSEvent *rawKeyEvent;
@public
struct frame *emacsframe;
int scrollbarsNeedingUpdate;
diff --git a/src/nsterm.m b/src/nsterm.m
index 9978ff6433e..e3c78822bb4 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -6993,6 +6993,8 @@ - (void)keyDown: (NSEvent *)theEvent
NSTRACE ("[EmacsView keyDown:]");
+ rawKeyEvent = theEvent;
+
/* Rhapsody and macOS give up and down events for the arrow keys. */
if ([theEvent type] != NSEventTypeKeyDown)
return;
@@ -7096,12 +7098,23 @@ In that case we use UCKeyTranslate (ns_get_shifted_character)
code, fnKeysym, flags, emacs_event->modifiers);
#endif
- /* If it was a function key or had control-like modifiers, pass
- it directly to Emacs. */
+ /* It was a function key or had control-like modifiers. */
if (fnKeysym || (emacs_event->modifiers
&& (emacs_event->modifiers != shift_modifier)
&& [[theEvent charactersIgnoringModifiers] length] > 0))
{
+ /* Before passing it to Emacs, check if there is a
+ system-level interpretation. */
+ if (ns_pass_keys_to_system)
+ {
+ keyEventsInterpreted = YES;
+ [self interpretKeyEvents: @[theEvent]];
+ if (keyEventsInterpreted)
+ return;
+ }
+
+ /* Pass it directly to Emacs. */
+
emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
/* FIXME: What are the next four lines supposed to do? */
if (code < 0x20)
@@ -7172,6 +7185,15 @@ In that case we use UCKeyTranslate (ns_get_shifted_character)
- (void) insertText: (id) string
replacementRange: (NSRange) replacementRange
{
+ /* Don't interpret modified events (except Shift and NumericPad). */
+ NSEventModifierFlags flags = rawKeyEvent.modifierFlags
+ & NSEventModifierFlagDeviceIndependentFlagsMask;
+ if (flags & ~(NSEventModifierFlagShift | NSEventModifierFlagNumericPad))
+ {
+ keyEventsInterpreted = NO;
+ return;
+ }
+
if ([string isKindOfClass:[NSAttributedString class]])
string = [string string];
[self unmarkText];
@@ -7337,6 +7359,7 @@ - (NSRange)markedRange
? NSMakeRange (0, [workingText length]) : NSMakeRange (NSNotFound, 0);
if (NS_KEYLOG)
NSLog (@"markedRange request");
+ keyEventsInterpreted = NO;
return rng;
}
@@ -7440,9 +7463,17 @@ - (void)doCommandBySelector: (SEL)aSelector
if (NS_KEYLOG)
NSLog (@"doCommandBySelector: %@", NSStringFromSelector (aSelector));
- processingCompose = NO;
- if (aSelector == @selector (deleteBackward:))
+ /* Either this is a function key, or is a delete event when
+ composition is active. */
+
+ if (processingCompose == NO)
{
+ /* The key event was not interpreted by interpretKeyEvents:. */
+ keyEventsInterpreted = NO;
+ }
+ else if (aSelector == @selector (deleteBackward:))
+ {
+ processingCompose = NO;
/* Happens when user backspaces over an ongoing composition:
throw a 'delete' into the event queue. */
if (!emacs_event)
@@ -11351,6 +11382,11 @@ Convert an X font name (XLFD) to an NS font name.
If `none', the key is ignored by Emacs and retains its standard meaning. */);
ns_function_modifier = Qnone;
+ DEFVAR_BOOL ("ns-pass-keys-to-system", ns_pass_keys_to_system,
+ doc: /* If non-nil, pass all key events to the system input manager
+before they are passed to Emacs. */);
+ ns_pass_keys_to_system = YES;
+
DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
doc: /* Non-nil (the default) means to render text antialiased. */);
ns_antialias_text = Qt;
--
2.39.3 (Apple Git-146)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* bug#72555: 30.0.60; [PATCH] NS: support passing key events to the system
2024-08-10 11:47 bug#72555: 30.0.60; [PATCH] NS: support passing key events to the system Kai Ma
@ 2024-08-10 19:46 ` Kai Ma
2024-10-05 1:12 ` Stefan Kangas
0 siblings, 1 reply; 3+ messages in thread
From: Kai Ma @ 2024-08-10 19:46 UTC (permalink / raw)
To: 72555
Do NOT test the patch. I just found the handling of certain keys incorrect.
(e.g. Control-/ seems to unconditionally triggers the alert beep. I’d appreciate it if some experts can share their insights.)
I will post a new patch once I find a solution.
^ permalink raw reply [flat|nested] 3+ messages in thread
* bug#72555: 30.0.60; [PATCH] NS: support passing key events to the system
2024-08-10 19:46 ` Kai Ma
@ 2024-10-05 1:12 ` Stefan Kangas
0 siblings, 0 replies; 3+ messages in thread
From: Stefan Kangas @ 2024-10-05 1:12 UTC (permalink / raw)
To: Kai Ma; +Cc: 72555
tags 72555 - patch
thanks
Kai Ma <justksqsf@gmail.com> writes:
> Do NOT test the patch. I just found the handling of certain keys incorrect.
>
> (e.g. Control-/ seems to unconditionally triggers the alert beep. I’d appreciate it if some experts can share their insights.)
>
> I will post a new patch once I find a solution.
Thanks. Any updates here?
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-10-05 1:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-10 11:47 bug#72555: 30.0.60; [PATCH] NS: support passing key events to the system Kai Ma
2024-08-10 19:46 ` Kai Ma
2024-10-05 1:12 ` Stefan Kangas
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).