Remapping Emacs' Meta to Windows keys on Kubuntu

2

After quite a few hours of searching now, I still can't work out how to do this.

Setup:

  • French Windows Comfort Curve Keyboard 2000 (which is why I want to remap)
  • HP Pavilion dm4 laptop running Kubuntu 12.04.
  • Emacs 24.3.1 cassou-emacs-precise

I simply want the ⊞ Win keys (+ left Alt if it's easier) to map to the ◆ Meta key in Emacs. I have tried everything I can think of - xev tells me that the keys are mapped to META_L and META_R from KDE's point of view - why on earth am I having so much trouble?

I've seen lots of stuff about remapping "mac-..." and "w32-..." stuff but I'm running neither a Mac, nor Windows. Why is Alt no good for my ◆ Meta? Alt Gr (right Alt) is used for lots of letters on a French keyboard and it's not practical to change that and I want a ◆ Meta key on the right side of the keyboard.

So I have done further research: Using Konsole and emacs -nw does not work but if I change this either with KDE's keyboard management app or xmodmap then it does work with Emacs in GUI mode. Win_R-w then C-h l gives:

M-w C-h l

for Emacs in GUI mode and

ESC [ > 0 ; 1 1 5 ; 0 c w C-h l

in emacs -nw in Konsole.

AntonOfTheWoods

Posted 2013-08-23T13:56:51.447

Reputation: 33

Could you please explain what you have tried, and how it fails. Your last paragraph seems to imply that you have solved it. – terdon – 2013-08-23T14:00:15.373

@terdon Only for Emacs frames on the X server, though. If it still doesn't work in Konsole, despite being xmodmapped to what you want it to be, the implication is that Konsole is doing something with the keystroke before passing it to Emacs; I'd be interested to see the output of M-x view-lossage (C-h l) after a chord involving the problematic key. – Aaron Miller – 2013-08-23T14:17:52.440

@AaronMiller that's right. I get:

ESC [ > 0 ; 1 1 5 ; 0 c w C-h l

After trying Win_L and Win_R with the character 'w' and then C-h l. – AntonOfTheWoods – 2013-08-23T14:24:59.327

@Antonovich Your comment ends with "I get:"; perhaps it'd work better to edit the lossage into your question? -- oh, there it is. – Aaron Miller – 2013-08-23T14:29:11.697

@AaronMiller - It's my first time posting so it's taking me a while to get the formatting right! Thanks for your patience... – AntonOfTheWoods – 2013-08-23T14:30:49.323

@Antonovich No worries, and welcome to Super User! We're glad to have you with us. OK, so Konsole is translating the Meta keypress into an ANSI escape code (ESC [ &c.) I see the same behavior in Konsole, and it seems plausible there'd be a way to convince Emacs it should recognize such sequences. I don't know yet how that might be done, but let me see what I can come up with. – Aaron Miller – 2013-08-23T14:36:44.003

@Antonovich Well, that didn't take long: apparently the problem is with Konsole mishandling the key, not with Emacs misunderstanding the input, and to solve it requires patching the source and rebuilding. Seems like it might just be easier to have Emacs talk to your X server...

– Aaron Miller – 2013-08-23T14:41:25.490

@AaronMiller - or using gnome-terminal... Wow that's annoying! You've answered my question - how do I mark you comment as an answer? (and how do I delete this comment? :-)) – AntonOfTheWoods – 2013-08-23T14:55:09.077

@Antonovich Comments can't be marked as answers, but answers can, so I've just added one. If you mouse over a comment, it should reveal a 'circled X' after your name, which can be clicked to delete the comment. (Also, Gnome Terminal seems to have the same problem Konsole does, judging by the lossage...) – Aaron Miller – 2013-08-23T15:00:25.650

@AaronMiller Thanks and done. Gnome Terminal works fine for me so I think I'll go with that for the meanwhile. – AntonOfTheWoods – 2013-08-23T15:06:49.630

Answers

1

This appears to result from the way in which Konsole handles the Meta key, and requires a source patch to fix. (In the following, 'a' is the unpatched version, and 'b' is the patched.)

--- a/konsole/konsole/TEmuVt102.cpp
+++ b/konsole/konsole/TEmuVt102.cpp
@@ -945,7 +945,8 @@  void TEmuVt102::onKeyPress( TQKeyEvent* ev )
                                      encodeMode(MODE_AppScreen     , BITS_AppScreen ) + // VT100 stuff
                                      encodeStat(TQt::ControlButton , BITS_Control   ) +
                                      encodeStat(TQt::ShiftButton   , BITS_Shift     ) +
-                                     encodeStat(TQt::AltButton     , BITS_Alt       ),
+                                     encodeStat(TQt::AltButton     , BITS_Alt       ) +
+                                     encodeStat(TQt::MetaButton        , BITS_Alt   ),
                           &cmd, &txt, &len, &metaspecified ))
 //printf("cmd: %d, %s, %d\n",cmd,txt,len);
   if (connected)
@@ -977,7 +978,7 @@  void TEmuVt102::onKeyPress( TQKeyEvent* ev )
     scr->setHistCursor(scr->getHistLines());

   if (cmd==CMD_send) {
-    if ((ev->state() & TQt::AltButton) && !metaspecified ) sendString("\033");
+    if (((ev->state() & TQt::AltButton) || (ev->state() & TQt::MetaButton)) && !metaspecified ) sendString("\033");
     emit sndBlock(txt,len);
     return;
   }

Aaron Miller

Posted 2013-08-23T13:56:51.447

Reputation: 8 849