-
February 18th, 2009, 06:06 AM
#1
Focus Problem in MFC Dialog Project
Hi everyone,
I've created a dialog project to implement a simple calculator. I'm trying to set the focus to be on my dialog so I will be able to use the WM_KEYDOWN to capture keyboard input. I've enabled the ON_WM_SETFOCUS() and ON_WM_KILLFOCUS() options so I will know when my dialog gets the focus, and when then focus is taken away from it.
I've tried setting the focus using SetFocus() in the following way:
BOOL CMyCalculatorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CWnd::SetFocus();
return FALSE; // return TRUE unless you set the focus to a control
}
However, when debugging the program I see that the focus is indeed given to the dialog at first ( ON_WM_SETFOCUS() is activated), but then the focus is immediately taken away from it ( ON_WM_KILLFOCUS() is activated ), and because of this the dialog doesn't respond to key pressings.
What am I doing wrong?
BTW: I'm using visual studio 2005 with an MFC project.
Thanks alot!
-
February 18th, 2009, 06:29 AM
#2
Re: Focus Problem in MFC Dialog Project
Maybe this?
return FALSE; // return TRUE unless you set the focus to a control
Put it back to TRUE and see what is happening
-
February 18th, 2009, 07:01 AM
#3
Re: Focus Problem in MFC Dialog Project
No, it was TRUE to begin with, the behavior stays the same.
I understood that returning FALSE tells the dialog manager (I'm not sure that's the name of it) that the focus was set to the current dialog, but I'm not sure.
-
February 18th, 2009, 10:10 AM
#4
Re: Focus Problem in MFC Dialog Project
Look, If you have made the dialog and select it that dialog have a focus, then press some key on the keyboard, message will be dispatched to your dialog.
You need to have OnKeyDown event handled:
OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
then you just handle nChar-s to call appropriate functions:
if( nChar == '1' ) {
Pressed(1);
}
if( nChar == VK_RETURN ) {
Calculate();
}
etc...
You don't need to bother with focus not focus problem.
-
February 18th, 2009, 02:10 PM
#5
Re: Focus Problem in MFC Dialog Project
I've already added the use of OnKeyDown. I press the keyboard upon startup of the dialog but this function is never entered. That what made me suspect the focus issue from the first place.
Then I added the OnSetFocus and OnKillFocus to try and take a deeper look into this issue, and I saw that the dialog does get the focus on startup (OnSetFocus is entered), but is then stripped away from it (OnKillFocus is entered).
-
February 18th, 2009, 02:23 PM
#6
Re: Focus Problem in MFC Dialog Project
Originally Posted by Ido_I
I've already added the use of OnKeyDown. I press the keyboard upon startup of the dialog but this function is never entered. That what made me suspect the focus issue from the first place.
Then I added the OnSetFocus and OnKillFocus to try and take a deeper look into this issue, and I saw that the dialog does get the focus on startup (OnSetFocus is entered), but is then stripped away from it (OnKillFocus is entered).
we need to understand what is the exact behavior that you are after and what part of that is not working.
-
February 18th, 2009, 11:48 PM
#7
Re: Focus Problem in MFC Dialog Project
Originally Posted by Ido_I
I've enabled the ON_WM_SETFOCUS() and ON_WM_KILLFOCUS() options so
Correction: you did not enable anything. You have inserted WM_SETFOCUS and WM_KILLFOCUS handlers.
[QUOTE= I'm trying to set the focus to be on my dialog so I will be able to use the WM_KEYDOWN to capture keyboard input. . .
What am I doing wrong?
BTW: I'm using visual studio 2005 with an MFC project.[/QUOTE]
Originally Posted by Ido_I
I would like the dialog box to control the keyboard input from one main place. The way I understand it, if I use OnKeyDown in the main dialog file (XXDlg.cpp), then every input of the keyboard should direct me to the OnKeyDown routine,
Originally Posted by Ido_I
I know I'm doing something wrong, as my knowledge of the MFC is very poor,
It does not matter what version of VS you are using. Also, this is not MFC. It is a dialog default behavior that you are struggling against. By design, dialog that has at least one control, never has a keyboard focus.
Originally Posted by Ido_I
I know I'm doing something wrong, as my knowledge of the MFC is very poor,
Proper way is to insert handlers for each control. Alternatively, you can use OnNotify or OnCommand if controls generate command or notification messages.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
February 19th, 2009, 05:28 AM
#8
Re: Focus Problem in MFC Dialog Project
I'm getting a little confused now - I'm trying all these advises but none is working. Let's start from the top:
Let's leave the calculator aside. Suppose I've just created a totaly new MFC Application project called "Test". The project is created with the "OK" and "Cancel" buttons. I didn't touch anything yet.
The question is simple - what are the steps I have to do (after creating the "Test" project from scratch) in-order to capture the keyboard input, when I start from this point?
From all the comments I understand it should be simple, and eventually comes down to adding the WM_KEYDOWN event handler. But I add it and insert a breakpoint inside CTestDlg::OnKeyDown.
But when I run this project, this breakpoint is never reached, meaning CTestDlg::OnKeyDown is never entered.
so again, starting from scratch after creating the project, what are the steps I need to do?
-
February 19th, 2009, 06:02 AM
#9
Re: Focus Problem in MFC Dialog Project
Originally Posted by Ido_I
I'm getting a little confused now - I'm trying all these advises but none is working.
....
From all the comments I understand it should be simple, and eventually comes down to adding the WM_KEYDOWN event handler. But I add it and insert a breakpoint inside CTestDlg::OnKeyDown.
But when I run this project, this breakpoint is never reached, meaning CTestDlg::OnKeyDown is never entered.
so again, starting from scratch after creating the project, what are the steps I need to do?
I wonder, do you ever read the answers?
JohnCz has just written you very clear:
Originally Posted by JohnCz
It does not matter what version of VS you are using. Also, this is not MFC. It is a dialog default behavior that you are struggling against. By design, dialog that has at least one control, never has a keyboard focus.
Proper way is to insert handlers for each control. Alternatively, you can use OnNotify or OnCommand if controls generate command or notification messages.
you could also try to override PreTranslateMessage method and handle WM_KEYDOWN there, although, it is not the best way and should be usually avoided, unless your actions on the pressed keys were the same independent on the control currently having focus.
Victor Nijegorodov
-
February 19th, 2009, 07:09 AM
#10
Re: Focus Problem in MFC Dialog Project
Originally Posted by JohnCz
Proper way is to insert handlers for each control. Alternatively, you can use OnNotify or OnCommand if controls generate command or notification messages.
If I still want to do it straight-forward by inserting handlers, I can still the following handlers are available for each button control:
BCN_HOTITEMCHANGE, BN_CLICKED, BN_DOUBLECLICKED, BN_KILLFOCUS, BN_SETFOCUS and NM_THEMECHANGED.
How is any of these handlers related to keyboard input control?
-
February 19th, 2009, 07:50 AM
#11
Re: Focus Problem in MFC Dialog Project
Originally Posted by Ido_I
If I still want to do it straight-forward by inserting handlers, I can still the following handlers are available for each button control:
BCN_HOTITEMCHANGE, BN_CLICKED, BN_DOUBLECLICKED, BN_KILLFOCUS, BN_SETFOCUS and NM_THEMECHANGED.
How is any of these handlers related to keyboard input control?
None of them. They all are just notification messages
You should subclass your controls and handle this message in the derived classes.
Victor Nijegorodov
-
February 19th, 2009, 04:45 PM
#12
Re: Focus Problem in MFC Dialog Project
Originally Posted by Ido_I
I read the answers alright, there's no need to offend or to attack. As someone who was introduced to the whole OOP concept literaly just a few days ago,
Good luck. Nevertheless question you have is not related to OOP paradigm. It is related to Windows programming.
Originally Posted by Ido_I
How is any of these handlers related to keyboard input control?
They are not handlers. As Victor pointed they are notification codes, used in a WM_COMMAND message when button is pressed using either mouse or (having focus) enter or a space bar.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
February 19th, 2009, 06:12 AM
#13
Re: Focus Problem in MFC Dialog Project
I read the answers alright, there's no need to offend or to attack. As someone who was introduced to the whole OOP concept literaly just a few days ago, I can only hope that the OOP veterans here can understand that trevial things to them might be very intrevial to me.
My actions on the pressed keys are the same, independent on the control currently having focus, so I'll try and see what PreTranslateMessage means and try this direction.
-
February 19th, 2009, 07:50 AM
#14
Re: Focus Problem in MFC Dialog Project
The problem is solved using the PreTranslateMessage, thanks for all who bothered to help!
It will still be nice to get an answer on my last question though...
-
February 19th, 2009, 08:19 AM
#15
Re: Focus Problem in MFC Dialog Project
OK, now it all makes sense!!
Many thanks!
Tags for this Thread
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
|