Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to handle WM_KEYDOWN in a Dialog???

Posted on 2002-04-24
7
Medium Priority
?
1,720 Views
Last Modified: 2013-11-25
Hello,
I have a problem.

I'm writing a standard application
on Visual C++, using Win32 and
the ATL classes included in
the headers ATLBase.h and ATLWin.h.
I use my own classes derived from:

CMyWindow : public CWindowImpl<CMyWindow>

CMyDialog : public CDialogImpl<CMyDialog>

I make my main window,
and then open a dialog at some time.

My question is:
How to handle for example the WM_KEYDOWN message in this
dialog.
I want when I press for example "Alt+A" and the focus
is in any of the controls in the dialog, this WM_KEYDOWN message to be handled from a single function
for example:
CMyDialog::OnKeyDown(.........),
not from this control's WM_KEYDOWN handler.

If this would help, I want to make the same as the Form KeyPreview property in Visual Basic does.
The WM_KEYDOWN should be handled somehow firstly by the dialog, and
then by the concreate control.

How could I do this???

Thank you in advance.

Pavel Tsekov, Varna, Bulgaria
0
Comment
Question by:paul_tsekov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 1200 total points
ID: 6966852
A dialog window doesn't normally send your application WM_KEYDOWN messages.  So if you want to get them you must SUBCLASS the dialog and provide your own message handler.

See the SDK help for SubclassDlgItem() or SubclassWindow().
0
 
LVL 4

Expert Comment

by:mblat
ID: 6966910
There is an article Q117563 in Microsoft Knowleage Base.
HOWTO: How to Trap WM_KEYDOWN Messages in a CDialog.

It shows the way for MFC based app.  I see that you are using WTL, but it may point you to the right direction.

Hope it helps.
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 6967122
Hi,

I am not sure about the architecture of ur app. If u r using MFC, then using classwizard, map the PreTranslateMessage(...) function to ur dialong class and modify it as

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
     if(pMsg->message == WM_KEYDOWN)
     {
          //Here U R when a key is hit, but, Alt is handled differently.
          //So u can get here by hitting any other key combinations. Like
          //if(pMsg->wParam == 'B')
          // Key B is hit
          //and
          //if(GetKeyState(VK_CONTROL) & 0x8000) && (pMsg->wParam == 'A'))
          // Key Ctrl+B is hit
     }

     //When Alt is hit check like this
     if((GetKeyState(VK_MENU) & 0x8000) && (pMsg->wParam == 'A'))
     {
          // U R Here as User hit Alt+A
          //Call Ur Function!!!!
          DoKeyProcess();
     }
     
     return CDialog::PreTranslateMessage(pMsg);
}

void CTestDlg::DoKeyProcess()
{
     AfxMessageBox("Alt+A Hit");
}

Here DoKeyProcess()is declared as void DoKeyProcess()in public section of dialog's .h file

Try it out.
VinExpert

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 1

Expert Comment

by:Moondancer
ID: 6973515
Please finalize the following year 2001 question:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20246850
Moondancer - EE Moderator
0
 

Expert Comment

by:SrinivasaRao
ID: 6974738
i think vinexpert is right:)
bsr
0
 
LVL 1

Author Comment

by:paul_tsekov
ID: 6979198
No, the EXPERT is not right.
I'm using the CDialogImpl<> class,
not CDialog.
I'm not talking about MFC here.
I talk about ATL!!!!!!!!!!!!!!!!
0
 
LVL 1

Author Comment

by:paul_tsekov
ID: 6979238
No, the EXPERT is not right.
I'm using the CDialogImpl<> class,
not CDialog.
I'm not talking about MFC here.
I talk about ATL!!!!!!!!!!!!!!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question