unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] On the nasty "ghost key" problem on NS
@ 2022-11-03 18:07 Kai Ma
  2022-11-04  0:32 ` Po Lu
  0 siblings, 1 reply; 20+ messages in thread
From: Kai Ma @ 2022-11-03 18:07 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1360 bytes --]

Hi all,

Attached is a one-liner patch to the “ghost key” problem on NS. (The name is made up by me.) I didn’t have luck finding a bug report (but it did happen to many to-be-liberated Mac users [1-3]), so I decided to directly send it here.

On certain occasions (*), the key event buffer is not cleared to be empty even if the events have been processed. This results in a nasty problem: when you press a key, say “y”, a key that is pressed earlier will be prepended to the current key. There’s no way to reset the event buffer.

This problem is highly unpredictable, making it very hard to reproduce. A probably quicker way: hack Emacs’ C codebase with corfu-auto and citre-mode.

(*) The root cause is still not clear to me. It’s observed that sometimes, the [interpretKeyEvents] call seems not to return. (Blocked inside Cocoa?) And the supposedly matched “remove” is never called. Now “nsEvArray” is left with an earlier keyDown event, and since “nsEvArray” is static, you have the problem as described above. I guess it may be some Cocoa internal changes that caused this.

This patch is just a workaround, but it improves the current user experience a bit.

Regards
Kai

[1] https://emacs-china.org/t/emacs-rime-e/20286
[2] https://emacs-china.org/t/emacs/15430
[3] https://emacs-china.org/t/topic/13207


[-- Attachment #2: trace.org --]
[-- Type: application/octet-stream, Size: 4897 bytes --]

* trace1
#+begin_src fundamental

     ,*1 #nsEvArray = 0
  keyDown: code =6d	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:27:47.272 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:47.273 emacs[46756:2752417] firstRectForCharRange request
  2022-11-04 00:27:47.273 emacs[46756:2752417] insertText 'm'	len = 1
     event removed...
     ,*3 #nsEvArray = 0
     ,*1 #nsEvArray = 0
  keyDown: code =61	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:27:47.297 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:47.297 emacs[46756:2752417] firstRectForCharRange request
  2022-11-04 00:27:47.298 emacs[46756:2752417] insertText 'a'	len = 1
     event removed...
     ,*3 #nsEvArray = 0
     ,*1 #nsEvArray = 0
  keyDown: code =63	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:27:47.727 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:47.727 emacs[46756:2752417] firstRectForCharRange request
  2022-11-04 00:27:47.729 emacs[46756:2752417] insertText 'c'	len = 1
     event removed...
     ,*3 #nsEvArray = 0
     ,*1 #nsEvArray = 0
  keyDown: code =73	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:27:47.732 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:47.732 emacs[46756:2752417] firstRectForCharRange request
  2022-11-04 00:27:48.424 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:48.424 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:48.424 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:48.424 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:48.424 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:48.424 emacs[46756:2752417] selectedRange request
     ,*1 #nsEvArray = 1
     event added...
     ,*2 #nsEvArray = 2
  2022-11-04 00:27:50.028 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:50.028 emacs[46756:2752417] firstRectForCharRange request
  2022-11-04 00:27:50.029 emacs[46756:2752417] insertText 's'	len = 1
  2022-11-04 00:27:50.030 emacs[46756:2752417] selectedRange request
  2022-11-04 00:27:50.030 emacs[46756:2752417] firstRectForCharRange request
  2022-11-04 00:27:50.030 emacs[46756:2752417] insertText 's'	len = 1
     event removed...
     ,*3 #nsEvArray = 1
#+end_src
* trace2
#+begin_src fundamental
     ,*1 #nsEvArray = 0
  keyDown: code =73	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:08:38.627 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:38.627 emacs[43626:2728428] firstRectForCharRange request
  2022-11-04 00:08:38.628 emacs[43626:2728428] insertText 's'	len = 1
     event removed...
     ,*3 #nsEvArray = 0
     ,*1 #nsEvArray = 0
  keyDown: code =65	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:08:38.973 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:38.973 emacs[43626:2728428] firstRectForCharRange request
  2022-11-04 00:08:38.973 emacs[43626:2728428] insertText 'e'	len = 1
     event removed...
     ,*3 #nsEvArray = 0
     ,*1 #nsEvArray = 0
  keyDown: code =6c	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 1
  2022-11-04 00:08:38.976 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:38.976 emacs[43626:2728428] firstRectForCharRange request
     ,*1 #nsEvArray = 1
     event added...
     ,*2 #nsEvArray = 2
  2022-11-04 00:08:39.639 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:39.639 emacs[43626:2728428] firstRectForCharRange request
  2022-11-04 00:08:39.640 emacs[43626:2728428] insertText 'l'	len = 1
  2022-11-04 00:08:39.641 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:39.641 emacs[43626:2728428] firstRectForCharRange request
  2022-11-04 00:08:39.641 emacs[43626:2728428] insertText 'f'	len = 1
     event removed...
     ,*3 #nsEvArray = 1
     ,*1 #nsEvArray = 1
  keyDown: code =20	fnKey =0	flags = 100	mods = 0
  keyDown: Begin compose sequence.
     event added...
     ,*2 #nsEvArray = 2
  2022-11-04 00:08:40.776 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:40.776 emacs[43626:2728428] firstRectForCharRange request
  2022-11-04 00:08:40.776 emacs[43626:2728428] insertText 'l'	len = 1
  2022-11-04 00:08:40.777 emacs[43626:2728428] selectedRange request
  2022-11-04 00:08:40.777 emacs[43626:2728428] firstRectForCharRange request
  2022-11-04 00:08:40.778 emacs[43626:2728428] insertText ' '	len = 1
     event removed...
     ,*3 #nsEvArray = 1
#+end_src

[-- Attachment #3: fix-ghost-key.patch --]
[-- Type: application/octet-stream, Size: 412 bytes --]

diff --git a/src/nsterm.m b/src/nsterm.m
index 17f40dc7e3..0a90e85e15 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -6904,7 +6904,7 @@ In that case we use UCKeyTranslate (ns_get_shifted_character)
   /* FIXME: Use [NSArray arrayWithObject:theEvent]?  */
   [nsEvArray addObject: theEvent];
   [self interpretKeyEvents: nsEvArray];
-  [nsEvArray removeObject: theEvent];
+  [nsEvArray removeAllObjects];
 }
 
 

^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-11-12 17:49 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-03 18:07 [PATCH] On the nasty "ghost key" problem on NS Kai Ma
2022-11-04  0:32 ` Po Lu
2022-11-04  6:28   ` Kai Ma
2022-11-04  7:16     ` Po Lu
2022-11-04  8:53       ` Kai Ma
2022-11-04  9:29         ` Po Lu
2022-11-04 11:04           ` Kai Ma
2022-11-04 11:27             ` Po Lu
2022-11-04 12:09               ` Kai Ma
2022-11-04 12:17                 ` Po Lu
2022-11-04 15:09                   ` Stefan Monnier
2022-11-05  0:43                     ` Po Lu
2022-11-05 14:40                       ` Stefan Monnier
2022-11-05 15:26                         ` Kai Ma
2022-11-05 15:47                           ` Stefan Monnier
2022-11-06  0:25                         ` Po Lu
2022-11-10 11:59                     ` Kai Ma
2022-11-10 12:25                       ` Po Lu
2022-11-12 17:49                         ` Stefan Monnier
2022-11-04 11:40         ` Eli Zaretskii

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).