July 3rd, 2009 03:23 AM
#1
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?
July 3rd, 2009 05:13 AM
#2
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.
July 3rd, 2009 04:42 PM
#3
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?
July 4th, 2009 03:11 PM
#4
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;
}
July 4th, 2009 04:04 PM
#5
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
Last edited by miksayer; July 5th, 2009 at 03:23 AM .
July 5th, 2009 03:26 AM
#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?
July 5th, 2009 06:33 AM
#7
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".
July 5th, 2009 02:09 PM
#8
Re: Get Symbolic Name Of Key Combination Code
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks