Carbon: get Keyboard event

Posted on Updated on

  1. create event with a window where the keyboard event to be catched.
  2. const EventTypeSpec kLoginWindowEvents[] =
    {
    {kEventClassKeyboard, kEventRawKeyDown}
    } ;

    InstallWindowEventHandler( myWindow, GetWindowEventHandlerUPP(),
    GetEventTypeCount( kLoginWindowEvents ), kLoginWindowEvents,
    myWindow, NULL );

    myWindow is the window reference(WindowRef) that you create from NIB or…

  3. create an event handler
  4. DEFINE_ONE_SHOT_HANDLER_GETTER( WindowEventHandler ) ; // this macro defined in CarbonEvent.h
    OSStatus WindowEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon )
    {
    OSStatus err = eventNotHandledErr;
    UInt32 keyPressed ;
    UInt32 kMyCommandID = ‘comd’ ;
    switch ( GetEventClass( inEvent ) )
    {
    case kEventClassKeyboard:
    {
    switch ( GetEventKind(inEvent) )
    {
    case kEventRawKeyDown :
    GetEventParameter(inEvent,kEventParamKeyCode,typeUInt32,NULL,
    sizeof(UInt32),NULL,&keyPressed) ;
    if ( keyPressed == 36 || keyPressed == 76) // 36: return; 76: enter
    mySendCommand( kMyCommandID ) ;

    break;

    case kEventRawKeyRepeat:
    case kEventRawKeyUp:
    case kEventRawKeyModifiersChanged:
    case kEventHotKeyPressed:
    case kEventHotKeyReleased:
    break;
    }
    }
    break;
    }
    }

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s