How to capture "[" and "]" keys on OnKeyDown()?
-
Hi, Hope somebody can help me with this one. I want to capture pressing "[" and "]" on OnKeyDown() event. For controls like shift if(VK_SHIFT) For capturing letters, I use example pressing s. if(nChar=="s") but for other keys like "[", "]" and numerics it's not applicable. How can i capture other keys?
-
Hi, Hope somebody can help me with this one. I want to capture pressing "[" and "]" on OnKeyDown() event. For controls like shift if(VK_SHIFT) For capturing letters, I use example pressing s. if(nChar=="s") but for other keys like "[", "]" and numerics it's not applicable. How can i capture other keys?
TooShy2Talk wrote:
For capturing letters, I use example pressing s. if(nChar=="s")
you mean
if (nChar == 'S')
but you should always use Virtual Key codes, because nChar is the virtual key code, capital letter is identified by the Shift/caps key status. you may refer the list of key codes [Virtual-Key Codes ^] to use in your code.TooShy2Talk wrote:
but for other keys like "[", "]" and numerics
for numbers you may use '0'(30) - '9'(39) or VK_NUMPAD(0 - 9) because number can be entered in two different keys num pad and other above characters. "[" can still be identified by VK_OEM_4 by Virtual-Key Codes but depends on keyboard layout. so it is better to use [VkKeyScan(Ex)^] to convert character to virtual key code and compare,
SHORT iSqrBracketOpen = VkKeyScan(_T('[')); // before hand once
void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == iSqrBracketOpen)
{
MessageBox(_T("Open Square Bracket"));
}
...
}or use [ToAscii(Ex)^] or ToUnicode(Ex) to convert virtual key code to character and compare.
-
TooShy2Talk wrote:
For capturing letters, I use example pressing s. if(nChar=="s")
you mean
if (nChar == 'S')
but you should always use Virtual Key codes, because nChar is the virtual key code, capital letter is identified by the Shift/caps key status. you may refer the list of key codes [Virtual-Key Codes ^] to use in your code.TooShy2Talk wrote:
but for other keys like "[", "]" and numerics
for numbers you may use '0'(30) - '9'(39) or VK_NUMPAD(0 - 9) because number can be entered in two different keys num pad and other above characters. "[" can still be identified by VK_OEM_4 by Virtual-Key Codes but depends on keyboard layout. so it is better to use [VkKeyScan(Ex)^] to convert character to virtual key code and compare,
SHORT iSqrBracketOpen = VkKeyScan(_T('[')); // before hand once
void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == iSqrBracketOpen)
{
MessageBox(_T("Open Square Bracket"));
}
...
}or use [ToAscii(Ex)^] or ToUnicode(Ex) to convert virtual key code to character and compare.
Thanks for the reply. It's a big help