Get Symbolic Name Of Key Combination Code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Get Symbolic Name Of Key Combination Code

  1. #1
    Join Date
    Jul 2009
    Posts
    6

    Get Symbolic Name Of Key Combination Code

    I've got such source code from MFC:
    Code:
    CString CHotKeyCtrl::GetKeyName(UINT vk, BOOL fExtended)
    {
        LONG lScan = MapVirtualKey(vk, 0) << 16;
    
        // if it's an extended key, add the extended flag
        if (fExtended)
            lScan |= 0x01000000L;
    
        CString str;
        int nBufferLen = 64;
        int nLen;
        do
        {
            nBufferLen *= 2;
            LPTSTR psz = str.GetBufferSetLength(nBufferLen);
            nLen = ::GetKeyNameText(lScan, psz, nBufferLen + 1);
            str.ReleaseBuffer(nLen);
        }
        while (nLen == nBufferLen);
        return str;
    }
    
    CString CHotKeyCtrl::GetHotKeyName() const
    {
        ASSERT(::IsWindow(m_hWnd));
    
        CString strKeyName;
        WORD wCode;
        WORD wModifiers;
    
        GetHotKey(wCode, wModifiers);
        if (wCode != 0 || wModifiers != 0)
        {
            if (wModifiers & HOTKEYF_CONTROL)
            {
                strKeyName += GetKeyName(VK_CONTROL, FALSE);
                strKeyName += szPlus;
            }
    
            if (wModifiers & HOTKEYF_SHIFT)
            {
                strKeyName += GetKeyName(VK_SHIFT, FALSE);
                strKeyName += szPlus;
            }
    
            if (wModifiers & HOTKEYF_ALT)
            {
                strKeyName += GetKeyName(VK_MENU, FALSE);
                strKeyName += szPlus;
            }
    
            strKeyName += GetKeyName(wCode, wModifiers & HOTKEYF_EXT);
        }
    
        return strKeyName;
    }
    i want to rewrite it using std::string(or char []), but i don't know std::string prototypes for CString::GetBufferSetLength and CString::ReleaseBuffer. Can you help me?

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Get Symbolic Name Of Key Combination Code

    You could dynamically allocate a TCHAR array and then deallocate it after use.

    TCHAR* psz = new TCHAR[nBufferLen + 1];
    .
    .
    .
    delete [] psz;

    char and string don't work with unicode data.
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Jul 2009
    Posts
    6

    Re: Get Symbolic Name Of Key Combination Code

    I hve code like this(i rewrite this code for your recommendations):
    Code:
    std::wstring getKeyName(UINT vk, BOOL fExtended)
    	{
    		LONG lScan = MapVirtualKey(vk, 0) << 16;
    
    		// if it's an extended key, add the extended flag
    		if (fExtended)
    			lScan |= 0x01000000L;
    		int nBufferLen = 64;
    		wchar_t* str;
    		int nLen;
    		do
    		{
    			nBufferLen *= 2;
    			str = new wchar_t[nBufferLen + 1];
    			nLen = ::GetKeyNameText(lScan, (LPWSTR)str, nBufferLen + 1);
    			delete [] str; 
    		}
    		while (nLen == nBufferLen);
    		return str;
    	}
    
    	std::wstring getHotkeyName(Hotkey hk)
    	{
    
    		std::wstring strKeyName;
    		WORD wCode = LOBYTE(hk);
    		WORD wModifiers = HIBYTE(hk);
    
    		if (wCode != 0 || wModifiers != 0)
    		{
    			if (wModifiers & HOTKEYF_CONTROL)
    			{
    				strKeyName += getKeyName(VK_CONTROL, FALSE);
    				strKeyName += L"+";
    			}
    
    			if (wModifiers & HOTKEYF_SHIFT)
    			{
    				strKeyName += getKeyName(VK_SHIFT, FALSE);
    				strKeyName += L"+";
    			}
    
    			if (wModifiers & HOTKEYF_ALT)
    			{
    				strKeyName += getKeyName(VK_MENU, FALSE);
    				strKeyName += L"+";
    			}
    
    			strKeyName += getKeyName(wCode, FALSE);
    		}
    
    		return strKeyName;
    	}
    but i've got error in wcslen.c. What i do unduly?

  4. #4
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: Get Symbolic Name Of Key Combination Code

    You are trying to return some garbage ('str' is deleted before).
    To replace CString with std::wstring, use the next code
    Code:
       // ... same as above
       std::wstring str;
       int nLen;
       do
       {
          nBufferLen *= 2;
          str.resize(nBufferLen);
          nLen = ::GetKeyNameTextW(lScan, &str[0], nBufferLen);
       }
       while (nLen == nBufferLen);
       return str;
    }
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  5. #5
    Join Date
    Jul 2009
    Posts
    6

    Re: Get Symbolic Name Of Key Combination Code

    2 ovidiucucu
    but i all the same have garbage(see attache)
    my code:
    Code:
    	std::wstring getKeyName(UINT vk, BOOL fExtended)
    	{
    		LONG lScan = MapVirtualKey(vk, 0) << 16;
    
    		// if it's an extended key, add the extended flag
    		if (fExtended)
    			lScan |= 0x01000000L;
    		int nBufferLen = 64;
    		std::wstring str;
    		int nLen;
    		do
    		{
    			nBufferLen *= 2;
    			str.resize(nBufferLen);
    			nLen = ::GetKeyNameTextW(lScan, &str[0], nBufferLen);
    		}
    		while (nLen == nBufferLen);
    		return str;
    	}
    Attached Images Attached Images  
    Last edited by miksayer; July 5th, 2009 at 03:23 AM.

  6. #6
    Join Date
    Jul 2009
    Posts
    6

    Re: Get Symbolic Name Of Key Combination Code

    problem located in this function
    Code:
    std::wstring getHotkeyName(Hotkey hk)
    	{
    
    		std::wstring strKeyName;
    		WORD wCode = LOBYTE(hk);
    		WORD wModifiers = HIBYTE(hk);
    
    		if (wCode != 0 || wModifiers != 0)
    		{
    			if (wModifiers & HOTKEYF_CONTROL)
    			{
    				strKeyName += getKeyName(VK_CONTROL, FALSE);
    				strKeyName += L"+";
    			}
    
    			if (wModifiers & HOTKEYF_SHIFT)
    			{
    				strKeyName += getKeyName(VK_SHIFT, FALSE);
    				strKeyName += L"+";
    			}
    
    			if (wModifiers & HOTKEYF_ALT)
    			{
    				strKeyName += getKeyName(VK_MENU, FALSE);
    				strKeyName += L"+";
    			}
    
    			strKeyName += getKeyName(wCode, FALSE);
    		}
    
    		return strKeyName;
    	}
    because getKeyName is working normally(i understood it by debugging). What i do unduly?

  7. #7
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: Get Symbolic Name Of Key Combination Code

    Ah, forgot a little detail
    Code:
    std::wstring getKeyName(UINT vk, BOOL fExtended)
    {
       // same as in my previous post
       return str.c_str();
    }
    I hope now is "unduly".
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  8. #8
    Join Date
    Jul 2009
    Posts
    6

    Re: Get Symbolic Name Of Key Combination Code

    ovidiucucu, big thx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center