+#if TARGET_CARBON
+
+EventHandlerUPP wxMacWindowEventHandlerUPP = NULL ;
+
+extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
+
+pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ EventRecord rec ;
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassTextInput :
+ if ( wxMacConvertEventToRecord( event , &rec ) )
+ {
+ short keycode ;
+ short keychar ;
+ keychar = short(rec.message & charCodeMask);
+ keycode = short(rec.message & keyCodeMask) >> 8 ;
+ long keyval = wxMacTranslateKey(keychar, keycode) ;
+ wxWindow* focus = wxWindow::FindFocus() ;
+
+ if ( wxTheApp->MacSendKeyDownEvent( focus , keyval , rec.modifiers , rec.when , rec.where.h , rec.where.v ) )
+ {
+ // was handled internally
+ result = noErr ;
+ }
+ }
+ break ;
+ default :
+ break ;
+ }
+ return result ;
+}
+
+#endif
+
+void wxTopLevelWindowMac::MacInstallEventHandler()
+{
+#if TARGET_CARBON
+ if ( wxMacWindowEventHandlerUPP == NULL )
+ {
+ wxMacWindowEventHandlerUPP = NewEventHandlerUPP( wxMacWindowEventHandler ) ;
+ }
+
+ static const EventTypeSpec eventList[] =
+ {
+ { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent }
+ } ;
+ if ( m_macEventHandler )
+ {
+ ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
+ m_macEventHandler = NULL ;
+ }
+ InstallWindowEventHandler(MAC_WXHWND(m_macWindow), wxMacWindowEventHandlerUPP, WXSIZEOF(eventList), eventList, this, &((EventHandlerRef)m_macEventHandler));
+#endif
+}
+