Subject: nsterm.m does not distinguish key on the key pad

From: Michael Marchionna <[email-masked]>
Date: Tue, 17 May 2011 10:34:01 -0700

In addition to the clear/NumLock key not being mapped the rest of the keypad keys do not seem to be
translated  as well, and hence can not be distinguished from keys on the main keyboard.  What follows
is a patch that remaps the keypad keys. at least on a aluminum apple keyboard, to the appropriate X11
virtual key codes.   Note the Clear/NumLock key is hardcoded to  XK_Clear.   There should probably be
a way to make it possible to configure it so that it translates to XK_Num_Lock.

diff --git a/src/nsterm.m b/src/nsterm.m
index 91f0cbb..322c9d9 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -87,6 +87,7 @@ static unsigned convert_ns_to_X_keysym[] =
   NSBeginFunctionKey,           0x58,
   NSSelectFunctionKey,          0x60,
   NSPrintFunctionKey,           0x61,
+  NSClearLineFunctionKey,       0x0B,
   NSExecuteFunctionKey,         0x62,
   NSInsertFunctionKey,          0x63,
   NSUndoFunctionKey,            0x65,
@@ -131,7 +132,24 @@ static unsigned convert_ns_to_X_keysym[] =
   NSNewlineCharacter,          0x0D,
   NSEnterCharacter,            0x8D,
 
-  0x1B,                                0x1B   /* escape */
+  0x1B,                                0x1B,  /* escape */
+
+  0x41,                         0xAE,  /* KP_Decimal */
+  0x43,                         0xAA,  /* KP_Multiply */
+  0x45,                         0xAB,  /* KP_Add */
+  0x4B,                         0xAF,  /* KP_Divide */
+  0x4E,                         0xAD,  /* KP_Subtract */
+  0x51,                         0xBD,  /* KP_Equal */
+  0x52,                         0xB0,  /* KP_0 */
+  0x53,                         0xB1,  /* KP_1 */
+  0x54,                         0xB2,  /* KP_2 */
+  0x55,                         0xB3,  /* KP_3 */
+  0x56,                         0xB4,  /* KP_4 */
+  0x57,                         0xB5,  /* KP_5 */
+  0x58,                         0xB6,  /* KP_6 */
+  0x59,                         0xB7,  /* KP_7 */
+  0x5B,                         0xB8,  /* KP_8 */
+  0x5C,                         0xB9   /* KP_9 */
 };

@@ -4503,10 +4521,10 @@ ns_term_shutdown (int sig)
   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
   int code;
   unsigned fnKeysym = 0;
-  int flags;
   static NSMutableArray *nsEvArray;
   static BOOL firstTime = YES;
   int left_is_none;
+  unsigned int flags = [theEvent modifierFlags];
 
   NSTRACE (keyDown);
 
@@ -4550,9 +4568,13 @@ ns_term_shutdown (int sig)
       code = ([[theEvent charactersIgnoringModifiers] length] == 0) ?
         0 : [[theEvent charactersIgnoringModifiers] characterAtIndex: 0];
       /* (Carbon way: [theEvent keyCode]) */
+      
 
       /* is it a "function key"? */
-      fnKeysym = ns_convert_key (code);
+      if ( code < 0x00ff && (flags&NSNumericPadKeyMask) )
+       fnKeysym = ns_convert_key([theEvent keyCode]);
+      else
+        fnKeysym = ns_convert_key (code);
       if (fnKeysym)
         {
           /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
@@ -4565,7 +4587,6 @@ ns_term_shutdown (int sig)
 
       /* are there modifiers? */
       emacs_event->modifiers = 0;
-      flags = [theEvent modifierFlags];
 
       if (flags & NSHelpKeyMask)
           emacs_event->modifiers |= hyper_modifier;