Key capture using OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) method
-
See more: VC++ Dear All, I am working MFC Application, this application is compiled using VS2008. To capture the key pressed OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) method is used. The strange thing happening is when i press the key "a" without caps lock its gives the nChar value as "65" which is suppose to be "97". Even if the caps lock is "ON" the value i get is "65" when i press "A". The thing is either the caps lock is on or off, i get the same value as if the caps lock is on. (this is the case with all the keys) as the OnKeyUp() is as MFC method, I am not sure how to solve this issue. Kindly help me and let me know if you require any more information. Below is the code i am using, because of this what happens is, when i set a text using SetDlgItemText(), it is always going as Upper case letter or word. void CMyProg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { CString SBCommand; CString Temp; char *s = NULL; char strnChar[10]; /* Alphabets and Numerals passed to the command box */ if((nChar >= 65 && nChar <= 90) || (nChar >= 48 && nChar <= 57)) { pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); Temp.Format("%c",nChar); SBCommand += Temp; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand); /*pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); SBCommand += (char*)nChar; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand);*/ } }
-
See more: VC++ Dear All, I am working MFC Application, this application is compiled using VS2008. To capture the key pressed OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) method is used. The strange thing happening is when i press the key "a" without caps lock its gives the nChar value as "65" which is suppose to be "97". Even if the caps lock is "ON" the value i get is "65" when i press "A". The thing is either the caps lock is on or off, i get the same value as if the caps lock is on. (this is the case with all the keys) as the OnKeyUp() is as MFC method, I am not sure how to solve this issue. Kindly help me and let me know if you require any more information. Below is the code i am using, because of this what happens is, when i set a text using SetDlgItemText(), it is always going as Upper case letter or word. void CMyProg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { CString SBCommand; CString Temp; char *s = NULL; char strnChar[10]; /* Alphabets and Numerals passed to the command box */ if((nChar >= 65 && nChar <= 90) || (nChar >= 48 && nChar <= 57)) { pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); Temp.Format("%c",nChar); SBCommand += Temp; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand); /*pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); SBCommand += (char*)nChar; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand);*/ } }
OnKeyDown: The key is Kept Down OnKeyUp: The Key is released The above two together form OnKeyPress All three will give the state of the Keys. OnChar is the best event for your expectation.
-
See more: VC++ Dear All, I am working MFC Application, this application is compiled using VS2008. To capture the key pressed OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) method is used. The strange thing happening is when i press the key "a" without caps lock its gives the nChar value as "65" which is suppose to be "97". Even if the caps lock is "ON" the value i get is "65" when i press "A". The thing is either the caps lock is on or off, i get the same value as if the caps lock is on. (this is the case with all the keys) as the OnKeyUp() is as MFC method, I am not sure how to solve this issue. Kindly help me and let me know if you require any more information. Below is the code i am using, because of this what happens is, when i set a text using SetDlgItemText(), it is always going as Upper case letter or word. void CMyProg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { CString SBCommand; CString Temp; char *s = NULL; char strnChar[10]; /* Alphabets and Numerals passed to the command box */ if((nChar >= 65 && nChar <= 90) || (nChar >= 48 && nChar <= 57)) { pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); Temp.Format("%c",nChar); SBCommand += Temp; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand); /*pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); SBCommand += (char*)nChar; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand);*/ } }
The character value is a virtual-key[^] code. You can use
GetKeyState(VK_SHIFT)
to check if any shift key is pressed or not. Depending on your requirements, it may be better to handleWM_CHAR
notifications (CWnd::OnChar()
) rather than using key up/down notifications. Then you will get the character code of the key that was pressed. -
See more: VC++ Dear All, I am working MFC Application, this application is compiled using VS2008. To capture the key pressed OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) method is used. The strange thing happening is when i press the key "a" without caps lock its gives the nChar value as "65" which is suppose to be "97". Even if the caps lock is "ON" the value i get is "65" when i press "A". The thing is either the caps lock is on or off, i get the same value as if the caps lock is on. (this is the case with all the keys) as the OnKeyUp() is as MFC method, I am not sure how to solve this issue. Kindly help me and let me know if you require any more information. Below is the code i am using, because of this what happens is, when i set a text using SetDlgItemText(), it is always going as Upper case letter or word. void CMyProg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { CString SBCommand; CString Temp; char *s = NULL; char strnChar[10]; /* Alphabets and Numerals passed to the command box */ if((nChar >= 65 && nChar <= 90) || (nChar >= 48 && nChar <= 57)) { pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); Temp.Format("%c",nChar); SBCommand += Temp; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand); /*pFrame->m_wndStatusBar.GetDlgItemText(IDC_SB_POINT,SBCommand); SBCommand += (char*)nChar; pFrame->m_wndStatusBar.SetDlgItemText(IDC_SB_POINT,SBCommand);*/ } }
In addition to
VK_SHIFT
, you may also want to check the state withVK_CAPITAL
.«_Superman_» _I love work. It gives me something to do between weekends.