⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hid.cpp

📁 很牛的GUI源码wxWidgets-2.8.0.zip 可在多种平台下运行.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ----------------------------------------------------------------------------// wxHIDDevice::HasElement//// Returns true if the element in the internal cookie array exists// ----------------------------------------------------------------------------bool wxHIDDevice::HasElement(int nIndex){    return m_pCookies[nIndex] != NULL;}// ----------------------------------------------------------------------------// wxHIDDevice Destructor//// Frees all memory and objects from the structure// ----------------------------------------------------------------------------wxHIDDevice::~wxHIDDevice(){    if (m_ppDevice != NULL)    {        if (m_ppQueue != NULL)        {            (*m_ppQueue)->stop(m_ppQueue);            (*m_ppQueue)->dispose(m_ppQueue);            (*m_ppQueue)->Release(m_ppQueue);        }        (*m_ppDevice)->close(m_ppDevice);        (*m_ppDevice)->Release(m_ppDevice);        mach_port_deallocate(mach_task_self(), m_pPort);    }    if (m_pCookies != NULL)    {        delete [] m_pCookies;    }}// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//// wxHIDKeyboard//// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//There are no right shift, alt etc. in the wx headers yet so just sort//of "define our own" for nowenum{    WXK_RSHIFT = 400,    WXK_RALT,    WXK_RCONTROL,    WXK_RMENU};// ----------------------------------------------------------------------------// wxHIDKeyboard::GetCount [static]//// Get number of HID keyboards available// ----------------------------------------------------------------------------int wxHIDKeyboard::GetCount(){    return wxHIDDevice::GetCount(kHIDPage_GenericDesktop,                               kHIDUsage_GD_Keyboard);}// ----------------------------------------------------------------------------// wxHIDKeyboard::Create//// Create the HID Keyboard// ----------------------------------------------------------------------------bool wxHIDKeyboard::Create(int nDev /* = 1*/){    return wxHIDDevice::Create(kHIDPage_GenericDesktop,                               kHIDUsage_GD_Keyboard,                               nDev);}// ----------------------------------------------------------------------------// wxHIDKeyboard::AddCookie//// Overloaded version of wxHIDDevice::AddCookie that simply does not// add a cookie if a duplicate is found// ----------------------------------------------------------------------------void wxHIDKeyboard::AddCookie(CFTypeRef Data, int i){    if(!HasElement(i))        wxHIDDevice::AddCookie(Data, i);}// ----------------------------------------------------------------------------// wxHIDKeyboard::BuildCookies//// Callback from Create() to build the HID cookies for the internal cookie// array// ----------------------------------------------------------------------------void wxHIDKeyboard::BuildCookies(CFArrayRef Array){    //Create internal cookie array    InitCookies(500);    //Begin recursing in array    DoBuildCookies(Array);}void wxHIDKeyboard::DoBuildCookies(CFArrayRef Array){    //Now go through each possible cookie    int i,        nUsage;//    bool bEOTriggered = false;    for (i = 0; i < CFArrayGetCount(Array); ++i)    {        const void* ref = CFDictionaryGetValue(                (CFDictionaryRef)CFArrayGetValueAtIndex(Array, i),                CFSTR(kIOHIDElementKey)                                              );        if (ref != NULL)        {            DoBuildCookies((CFArrayRef) ref);        }        else    {            //            // Get the usage #            //        CFNumberGetValue(                (CFNumberRef)                    CFDictionaryGetValue((CFDictionaryRef)                        CFArrayGetValueAtIndex(Array, i),                        CFSTR(kIOHIDElementUsageKey)                                        ),                              kCFNumberLongType,                              &nUsage);            //            // Now translate the usage # into a wx keycode            //        //        // OK, this is strange - basically this kind of strange -        // Starting from 0xEO these elements (like shift) appear twice in        // the array!  The ones at the end are bogus I guess - the funny part        // is that besides the fact that the ones at the front have a Unit        // and UnitExponent key with a value of 0 and a different cookie value,        // there is no discernable difference between the two...        //        // Will the real shift please stand up?        //        // Something to spend a support request on, if I had one, LOL.        //        //if(nUsage == 0xE0)        //{        //    if(bEOTriggered)        //       break;        //    bEOTriggered = true;        //}        //Instead of that though we now just don't add duplicate keys        if (nUsage >= kHIDUsage_KeyboardA && nUsage <= kHIDUsage_KeyboardZ)            AddCookie(CFArrayGetValueAtIndex(Array, i), 'A' + (nUsage - kHIDUsage_KeyboardA) );        else if (nUsage >= kHIDUsage_Keyboard1 && nUsage <= kHIDUsage_Keyboard9)            AddCookie(CFArrayGetValueAtIndex(Array, i), '1' + (nUsage - kHIDUsage_Keyboard1) );        else if (nUsage >= kHIDUsage_KeyboardF1 && nUsage <= kHIDUsage_KeyboardF12)            AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_F1 + (nUsage - kHIDUsage_KeyboardF1) );        else if (nUsage >= kHIDUsage_KeyboardF13 && nUsage <= kHIDUsage_KeyboardF24)            AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_F13 + (nUsage - kHIDUsage_KeyboardF13) );        else if (nUsage >= kHIDUsage_Keypad1 && nUsage <= kHIDUsage_Keypad9)            AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_NUMPAD1 + (nUsage - kHIDUsage_Keypad1) );        else switch (nUsage)        {            //0's (wx & ascii go 0-9, but HID goes 1-0)            case kHIDUsage_Keyboard0:                AddCookie(CFArrayGetValueAtIndex(Array, i), '0');                break;            case kHIDUsage_Keypad0:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_NUMPAD0);                break;            //Basic            case kHIDUsage_KeyboardReturnOrEnter:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_RETURN);                break;            case kHIDUsage_KeyboardEscape:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_ESCAPE);                break;            case kHIDUsage_KeyboardDeleteOrBackspace:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_BACK);                break;            case kHIDUsage_KeyboardTab:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_TAB);                break;            case kHIDUsage_KeyboardSpacebar:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_SPACE);                break;            case kHIDUsage_KeyboardPageUp:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_PAGEUP);                break;            case kHIDUsage_KeyboardEnd:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_END);                break;            case kHIDUsage_KeyboardPageDown:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_PAGEDOWN);                break;            case kHIDUsage_KeyboardRightArrow:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_RIGHT);                break;            case kHIDUsage_KeyboardLeftArrow:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_LEFT);                break;            case kHIDUsage_KeyboardDownArrow:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_DOWN);                break;            case kHIDUsage_KeyboardUpArrow:                AddCookie(CFArrayGetValueAtIndex(Array, i), WXK_UP);                break;            //LEDS            case kHIDUsage_KeyboardCapsLock:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_CAPITAL);                break;            case kHIDUsage_KeypadNumLock:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_NUMLOCK);                break;            case kHIDUsage_KeyboardScrollLock:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_SCROLL);                break;            //Menu keys, Shift, other specials            case kHIDUsage_KeyboardLeftControl:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_CONTROL);                break;            case kHIDUsage_KeyboardLeftShift:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_SHIFT);                break;            case kHIDUsage_KeyboardLeftAlt:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_ALT);                break;            case kHIDUsage_KeyboardLeftGUI:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_MENU);                break;            case kHIDUsage_KeyboardRightControl:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_RCONTROL);                break;            case kHIDUsage_KeyboardRightShift:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_RSHIFT);                break;            case kHIDUsage_KeyboardRightAlt:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_RALT);                break;            case kHIDUsage_KeyboardRightGUI:                AddCookie(CFArrayGetValueAtIndex(Array, i),WXK_RMENU);                break;            //Default            default:            //not in wx keycodes - do nothing....            break;            } //end mightly long switch        } //end if the current element is not an array...    } //end for loop for Array}//end buildcookies// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//// wxHIDModule//// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++class wxHIDModule : public wxModule{    DECLARE_DYNAMIC_CLASS(wxHIDModule)    public:        static wxArrayPtrVoid sm_keyboards;        virtual bool OnInit()        {            return true;        }        virtual void OnExit()        {            for(size_t i = 0; i < sm_keyboards.GetCount(); ++i)                delete (wxHIDKeyboard*) sm_keyboards[i];        }};IMPLEMENT_DYNAMIC_CLASS(wxHIDModule, wxModule)wxArrayPtrVoid wxHIDModule::sm_keyboards;// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//// wxGetKeyState()//// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++bool wxGetKeyState (wxKeyCode key){    wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=        WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));    if (wxHIDModule::sm_keyboards.GetCount() == 0)    {        int nKeyboards = wxHIDKeyboard::GetCount();        for(int i = 1; i <= nKeyboards; ++i)        {            wxHIDKeyboard* keyboard = new wxHIDKeyboard();            if(keyboard->Create(i))            {                wxHIDModule::sm_keyboards.Add(keyboard);            }            else            {                delete keyboard;                break;            }        }        wxASSERT_MSG(wxHIDModule::sm_keyboards.GetCount() != 0,                     wxT("No keyboards found!"));    }    for(size_t i = 0; i < wxHIDModule::sm_keyboards.GetCount(); ++i)    {        wxHIDKeyboard* keyboard = (wxHIDKeyboard*)                                wxHIDModule::sm_keyboards[i];    switch(key)    {    case WXK_SHIFT:            if( keyboard->IsActive(WXK_SHIFT) ||                   keyboard->IsActive(WXK_RSHIFT) )            {                return true;            }        break;    case WXK_ALT:            if( keyboard->IsActive(WXK_ALT) ||                   keyboard->IsActive(WXK_RALT) )            {                return true;            }        break;    case WXK_CONTROL:            if( keyboard->IsActive(WXK_CONTROL) ||                   keyboard->IsActive(WXK_RCONTROL) )            {                return true;            }        break;    case WXK_MENU:            if( keyboard->IsActive(WXK_MENU) ||                   keyboard->IsActive(WXK_RMENU) )            {                return true;            }        break;    default:            if( keyboard->IsActive(key) )            {                return true;            }        break;    }    }    return false; //not down/error}#endif //__DARWIN__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -