Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WM_KEYDOWN don't work

1,011 views
Skip to first unread message

mais76

unread,
Apr 1, 2004, 8:24:23 AM4/1/04
to
Hi all

I've just created new dialog application, first of all I would like to
capture the key pressure. So I open the MFC Class Wizzard and in the
messages list I clicked on WM_KEYDOWN, then I click on Edit code to
generate the OnKeyDown function. Without writing any line in that
function I put a brekpoint in it and then I run the application.

If I press any key of the keyboard, the application don't stop in the
breakpoint, the OnKeyDown function never called.

WHY?????

Can someone help me?

Thanks in advance
Mais

Damian Driscoll

unread,
Apr 1, 2004, 8:57:47 AM4/1/04
to
mais76 wrote:

> If I press any key of the keyboard, the application don't stop in the
> breakpoint, the OnKeyDown function never called.
>
> WHY?????

The WM_KEYDOWN message is being sent to a control in your dialog box, not
the dialog box itself. If you really want to receive this message then you
need to subclass the control. However, you usually don't need to do this,
you just need to respond to some other message sent to the dialog box by
the control.

Damian

Kunst

unread,
Apr 1, 2004, 9:18:56 AM4/1/04
to
Hi,

try with accelerators. Insert a new accelerator as resource and implement some code.
My accelerator (Ctrl-A = Select All) is stored in the MyApp.rc file and is something like:

IDR_ACCELERATOR ACCELERATORS DISCARDABLE
BEGIN
"A", IDC_SELECTALL, VIRTKEY, CONTROL, NOINVERT
END


The Code to implement is something like:

///////////////////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(MyDialog, CDialog)
ON_COMMAND(IDC_SELECTALL, OnSelectAll)
END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////////////////////

BOOL MyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
m_hAccelerator = LoadAccelerators(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR));
}

///////////////////////////////////////////////////////////////////////////////////////////

BOOL MyDialog::PreTranslateMessage(MSG* pMsg)
{
if (WM_KEYFIRST <= pMsg->message && pMsg->message <= WM_KEYLAST)
{
if (m_hAccelerator && ::TranslateAccelerator(m_hWnd, _hAccelerator, pMsg))
return TRUE;
}

return CDialog::PreTranslateMessage(pMsg);
}


///////////////////////////////////////////////////////////////////////////////////////////

OnSelectAll()
{
// DoSomething for Ctrl-A key :-)
}

///////////////////////////////////////////////////////////////////////////////////////////


Bye,
Robi

"mais76" <apann...@inwind.it> schrieb im Newsbeitrag news:5e39a1ef.0404...@posting.google.com...

Francesco Emanuele D'Agostino

unread,
Apr 2, 2004, 7:37:54 AM4/2/04
to
Assure that, when u press key, your dlg is the active window.

The WM_KEYDOWN message is posted to the window with the keyboard focus


"mais76" <apann...@inwind.it> ha scritto nel messaggio
news:5e39a1ef.0404...@posting.google.com...

mais76

unread,
Apr 2, 2004, 10:09:51 AM4/2/04
to
I requested an help because my really problem is the following:

in my application I use a Tab Control, and I need to change the focus
from the controls that are in the main dialog and that are in the Tab
dialog using the Tab Key. For example:
_____________________________________
|My Application |
| _____________ |
| | Button 1 | |
| |_____________| |
| |
| _______________ |
| | Tab1 | Tab 2 | |
| |_______|_______|_____________ |
| | | |
| | | |
| | _____________ | |
| | | Button 2 | | |
| | |_____________| | |
| | | |
| | | |
| |_____________________________| |
|_____________________________________|

Using the Tab key I need to change the focus from Button 1 and Button
2.

I think to use the PreTranslateMessage function as:

BOOL CSpypanDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->wParam == VK_TAB)
{
....
....
}

return CDialog::PreTranslateMessage(pMsg);
}

but it doesn't work because sometimes the if condition is true also if
the Tab key isn't pressed.

I think it doesn't work because I use the WindowProc function to
implement the OnIdle function as in the following code:

LRESULT CSpypanDlg::WindowProc(UINT message, WPARAM wParam, LPARAM
lParam)
{
DWORD QueueStatus;
LRESULT resValue = 0;
BOOL OnIdleRetVal = TRUE;

if(message == WM_IDLE)
{
OnIdleRetVal = OnIdle(0);
if(!OnIdleRetVal)
wParam = 0;
}
else
resValue = CDialog::WindowProc(message, wParam, lParam);

QueueStatus = GetQueueStatus(QS_ALLINPUT);

if(HIWORD(QueueStatus) == 0)
PostMessage(WM_IDLE, wParam + (OnIdleRetVal ? 1 : 0), 0);

return resValue;
}

BOOL CSpypanDlg::OnIdle(LONG lCount)
{
.....
.....

return TRUE;
}

I look your example but it use the PreTranslateMessage...

Have you an idea to resolve this problem?

Thanks very much
mais

Jugoslav Dujic

unread,
Apr 2, 2004, 10:36:58 AM4/2/04
to
mais76 wrote:
| I requested an help because my really problem is the following:
|
| in my application I use a Tab Control, and I need to change the focus
| from the controls that are in the main dialog and that are in the Tab
| dialog using the Tab Key. For example:
| _____________________________________
|| My Application |
|| _____________ |
||| Button 1 | |
||| _____________| |
|| |
|| _______________ |
||| Tab1 | Tab 2 | |
||| _______|_______|_____________ |
||| | |
||| | |
||| _____________ | |
||| | Button 2 | | |
||| |_____________| | |
||| | |
||| | |
||| _____________________________| | _____________________________________|
|
| Using the Tab key I need to change the focus from Button 1 and Button
| 2.

You should be achieve that to fix that by giving WS_EX_CONTROLPARENT style
to everything in sight (Tab control, child dialog 1, child dialog 2 --
the latter two should have DS_CONTROL as well).

Note, however, that WS_EX_CONTROLPARENT style (in pure Win32) also
applies forwarding of WM_COMMAND messages to the parent window
(parent of Tab control at the end); unless MFC forwards WM_COMMAND
internally, your button press on Button2 will end in main window's
handler instead of CDialog which is parent of button 2.

--
Jugoslav
___________
www.geocities.com/jdujic

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

Jussi Jumppanen

unread,
Apr 4, 2004, 1:44:45 PM4/4/04
to
mais76 wrote:

Just because the wizard writes some code does not mean the code is
correct.

The reason the break point is not triggered is because the WM_KEYDOWN
message is NOT being sent to that window. The wizard wrote key handing
code fora window that never gets WM_KEYDOWN messages.

Solution: Use the spy++ message spying utility to to see wich window
is getting the WM_KEYDOWN message.

Jussi Jumppanen
Author of: Zeus for Windows (All new version 3.92 out now)
"The C/C++, Cobol, Java, HTML, Python, PHP, Perl programmer's editor"
Home Page: http://www.zeusedit.com

0 new messages