--- event-carbon.c.old	2005-08-30 12:02:11.000000000 -0600
+++ event-carbon.c	2005-09-24 13:18:22.000000000 -0600
@@ -209,44 +209,12 @@
 {
   AEEventHandlerUPP od_handler = NewAEEventHandlerUPP (open_documents_ae_handler);
   
-  if (AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, od_handler, NULL, false) != noErr)
+  if (AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, od_handler, 0L, false) != noErr)
     invalid_operation ("Cannot install open documents Apple event handler", Qnil);
   
   DisposeAEEventHandlerUPP (od_handler);
 }
 
-/* Table for translating Mac keycode to keysym strings.  Adapted from macterm.c in Emacs.  */
-static char *keycode_to_keysymstr_table[] = {
-  /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  /*0x20*/ 0, 0, 0, 0, "return", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  
-  /*0x30*/ "tab", "space", 0, "backspace",
-  /*0x34*/ 0, "escape", 0, 0,
-  /*0x38*/ 0, 0, 0, 0,
-  /*0x3C*/ 0, 0, 0, 0,
-  
-  /*0x40*/ 0, "kp-decimal", 0, "kp-multiply",
-  /*0x44*/ 0, "kp-add", 0, "clear",
-  /*0x48*/ 0, 0, 0, "kp-divide",
-  /*0x4C*/ "kp-enter", 0, "kp-subtract", 0,
-  
-  /*0x50*/ 0, "kp-equal", "kp-0", "kp-1",
-  /*0x54*/ "kp-2", "kp-3", "kp-4", "kp-5",
-  /*0x58*/ "kp-6", "kp-7", 0, "kp-8",
-  /*0x5C*/ "kp-9", 0, 0, 0,
-  
-  /*0x60*/ "f5", "f6", "f7", "f3",
-  /*0x64*/ "f8", "f9", 0, "f11",
-  /*0x68*/ 0, "f13", 0, "f14",
-  /*0x6C*/ 0, "f10", 0, "f12",
-  
-  /*0x70*/ 0, "f15", "insert", "home",
-  /*0x74*/ "prior", "delete", "f4", "end",
-  /*0x78*/ "f2", "next", "f1", "left",
-  /*0x7C*/ "right", "down", "up", 0
-};
-
 /* Used in frame-carbon.c.  */
 int carbon_modifiers_to_emacs_modifiers (UInt32);
 int
@@ -347,6 +315,59 @@
     }
 }
 
+static char *ascii_to_keysymstr_table[] = {
+  /*0x00*/ 0, "home", 0, "kp-enter", "end", "help", 0, 0,
+  /*0x08*/ "backspace", "tab", 0, "prior", "next", "return", 0, 0,
+  /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "left", "right", "up", "down",
+  /*0x20*/ "space", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
+  /*0x30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "delete"
+};
+
+static char ascii_needs_keycode_lookup[] = {
+  /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x10*/ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
+  /*0x20*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
+  /*0x30*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
+  /*0x40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+static char keycode_needs_retranslate[] = {
+  /*0x00*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x10*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x20*/ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x30*/ 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x40*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x50*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x60*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  /*0x70*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
+};
+
+static char *keycode_to_keysymstr_table[] = {
+  /*0x00*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x10*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /*0x20*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
+  /*0x30*/ 0, 0, 0, 0, 0, "escape", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
+  
+  /*0x40*/ 0, "kp-decimal", 0, "kp-multiply", 0, "kp-add", 0, "clear",
+  /*0x48*/ 0, 0, 0, "kp-divide", "kp-enter", 0, "kp-subtract", 0,
+  
+  /*0x50*/ 0, "kp-equal", "kp-0", "kp-1", "kp-2", "kp-3", "kp-4", "kp-5",
+  /*0x58*/ "kp-6", "kp-7", 0, "kp-8", "kp-9", 0, 0, 0,
+  
+  /*0x60*/ "f5", "f6", "f7", "f3", "f8", "f9", 0, "f11",
+  /*0x68*/ 0, "f13", 0, "f14", 0, "f10", 0, "f12",
+  
+  /*0x70*/ 0, "f15", 0, 0, 0, 0, "f4", 0,
+  /*0x78*/ "f2", 0, "f1", 0, 0, 0, 0, 0
+};
+
 static OSStatus
 text_input_event_handler (EventHandlerCallRef UNUSED (next_handler), EventRef event, void * UNUSED (data))
 {
@@ -361,32 +382,43 @@
   UInt32 keycode;
   if (GetEventParameter (keyboard_event, kEventParamKeyCode, typeUInt32, NULL, sizeof (typeUInt32), NULL, &keycode) != noErr)
     invalid_operation ("Can't get key code", Qunbound);
+  keycode &= 0x7f;
   
-  char *keysymstr = keycode_to_keysymstr_table[keycode & 0x7f];
-  if (keysymstr)
-    {
-      enqueue_one_input_event (KEYSYM (keysymstr), carbon_modifiers_to_emacs_modifiers (modifiers));
-    }
-  else
+  UInt32 text_size;
+  if (GetEventParameter (event, kEventParamTextInputSendText, typeUnicodeText, NULL, 0, &text_size, NULL) != noErr || text_size == 0)
+    invalid_operation ("Can't get input text size", Qunbound);
+    
+  UniChar *text = (UniChar *)alloca_extbytes (text_size);
+  if (GetEventParameter (event, kEventParamTextInputSendText, typeUnicodeText, NULL, text_size, NULL, text) != noErr)
+    invalid_operation ("Can't get input text", Qunbound);
+  
+  if (text_size == 2 && text[0] <= 127)
     {
-      if (modifiers & (controlKey | cmdKey))
-	{
-	  retranslate_keycode (keyboard_event, modifiers);
-	}
+      if ((modifiers & (controlKey | cmdKey)) && keycode_needs_retranslate[keycode])
+	retranslate_keycode (keyboard_event, modifiers);
       else
 	{
-	  UInt32 text_size;
-	  if (GetEventParameter (event, kEventParamTextInputSendText, typeUnicodeText, NULL, 0, &text_size, NULL) != noErr)
-	    invalid_operation ("Can't get input text size", Qunbound);
-    
-	  Extbyte *text = alloca_extbytes (text_size);
-	  if (GetEventParameter (event, kEventParamTextInputSendText, typeUnicodeText, NULL, text_size, NULL, text) != noErr)
-	    invalid_operation ("Can't get input text", Qunbound);
-	  
-	  enqueue_input ((UniChar *)text, text_size / 2, modifiers);
+	  char *keysymstr = ascii_to_keysymstr_table[text[0]];
+	  if (keysymstr)
+	    enqueue_one_input_event (KEYSYM (keysymstr), carbon_modifiers_to_emacs_modifiers (modifiers));
+	  else
+	    {
+	      if (ascii_needs_keycode_lookup[text[0]])
+		{
+		  char *keysymstr = keycode_to_keysymstr_table[keycode];
+		  if (keysymstr)
+		    enqueue_one_input_event (KEYSYM (keysymstr), carbon_modifiers_to_emacs_modifiers (modifiers));
+		  else
+		    enqueue_input (text, text_size / 2, modifiers);
+		}
+	      else
+		enqueue_input (text, text_size / 2, modifiers);
+	    }
 	}
     }
-
+  else
+    enqueue_input (text, text_size / 2, modifiers);
+ 
   return noErr;
 }
 
