A dialog with button controls does not have focus OnKeyDown
-
Hello. At the first time, I made a dialog with OnKeyDown function and there was no control on the dialog. My OnKeyDown function worked well. I pressed a key(spacebar) and OnKeyDown function was called. :) Next, I made some CButton controls on the dialog. And I soon noticed something wrong, OnKeyDown had no effect. I found the reason. One of the button controls had focus automatically, and when I pressed spacebar that button was clicked. :omg: (If there was no control on the dialog, that action would call OnKeyDown function.) So I added the code "AfxGetApp()->m_pMainWnd->SetFocus();" in OnInitDialog, yet that seems ineffective. I want to make the dialog grab 'keydown' focus like the first time, not the button control. I need ideas and advice. Thank you for reading this.
May the sky bring you a full measure of health and prosperity.
-
Hello. At the first time, I made a dialog with OnKeyDown function and there was no control on the dialog. My OnKeyDown function worked well. I pressed a key(spacebar) and OnKeyDown function was called. :) Next, I made some CButton controls on the dialog. And I soon noticed something wrong, OnKeyDown had no effect. I found the reason. One of the button controls had focus automatically, and when I pressed spacebar that button was clicked. :omg: (If there was no control on the dialog, that action would call OnKeyDown function.) So I added the code "AfxGetApp()->m_pMainWnd->SetFocus();" in OnInitDialog, yet that seems ineffective. I want to make the dialog grab 'keydown' focus like the first time, not the button control. I need ideas and advice. Thank you for reading this.
May the sky bring you a full measure of health and prosperity.
If you read the documentation of CDialog::OnInitDialog[^], you find this:
Return Value Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.
So return FALSE from OnInitDialog. Does that help?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
-
If you read the documentation of CDialog::OnInitDialog[^], you find this:
Return Value Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.
So return FALSE from OnInitDialog. Does that help?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
I tried and the button control seemed losing its 'focus', but when I pressed spacebar, the button was still clicked. (The problem is that my action(pressing the key) didn't call the OnKeyDown function because of the buttons. Once I removed the buttons for test, the program worked well. Yet I have to add buttons.) Anyway, thanks for your answer. If you have any ideas, please let me know. :)
May the sky bring you a full measure of health and prosperity.
-
I tried and the button control seemed losing its 'focus', but when I pressed spacebar, the button was still clicked. (The problem is that my action(pressing the key) didn't call the OnKeyDown function because of the buttons. Once I removed the buttons for test, the program worked well. Yet I have to add buttons.) Anyway, thanks for your answer. If you have any ideas, please let me know. :)
May the sky bring you a full measure of health and prosperity.
I'm not sure but you could try catching the key press in the PreTranslateMessage[^] method of the CDialog...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
-
I'm not sure but you could try catching the key press in the PreTranslateMessage[^] method of the CDialog...
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
Your advice greatly helped me. I think you were busy, nevertheless, you helped me. Thank you very much. :) I am using the PreTranslateMessage method like this :
#define SPACEBAR 32
...
BOOL C~~Dlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN) {
if(pMsg->wParam == SPACEBAR)
OnKeyDown(SPACEBAR, 1, 57);And when I press down spacebar, my OnKeyDown function is called. :-D Yet there remains a little problem that the button is still being clicked. I am trying to find the solution now. If I find good ideas, I will post it on this post. Have a nice day! :)
May the sky bring you a full measure of health and prosperity.
-
Your advice greatly helped me. I think you were busy, nevertheless, you helped me. Thank you very much. :) I am using the PreTranslateMessage method like this :
#define SPACEBAR 32
...
BOOL C~~Dlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN) {
if(pMsg->wParam == SPACEBAR)
OnKeyDown(SPACEBAR, 1, 57);And when I press down spacebar, my OnKeyDown function is called. :-D Yet there remains a little problem that the button is still being clicked. I am trying to find the solution now. If I find good ideas, I will post it on this post. Have a nice day! :)
May the sky bring you a full measure of health and prosperity.
Yourwelcome. :) Try returning TRUE from PreTranslateMessage after you called your OnKeyDown handler, this should stop the button from handling the keyhit too and get pressed. Good luck. :)
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <
-
Hello. At the first time, I made a dialog with OnKeyDown function and there was no control on the dialog. My OnKeyDown function worked well. I pressed a key(spacebar) and OnKeyDown function was called. :) Next, I made some CButton controls on the dialog. And I soon noticed something wrong, OnKeyDown had no effect. I found the reason. One of the button controls had focus automatically, and when I pressed spacebar that button was clicked. :omg: (If there was no control on the dialog, that action would call OnKeyDown function.) So I added the code "AfxGetApp()->m_pMainWnd->SetFocus();" in OnInitDialog, yet that seems ineffective. I want to make the dialog grab 'keydown' focus like the first time, not the button control. I need ideas and advice. Thank you for reading this.
May the sky bring you a full measure of health and prosperity.
hi,get the key message use PreTranslateMessage() function. you can try..