OnChar/OnKeyDown (again...)

I need to handle a few command characters (like DEL and TAB) in a window 
and also some regular ones, like '+' and '-'. OnKeyDown appears to 
detect them all but they are not translates, e.g. '-' comes in as 167 
(IIRC). That's why you need OnChar, I suppose. I would be ok with having 
2 handlers, but some chars (such as TAB) come properly translated 
through both, f*g up my program. (quite a nice framework, he?) Short of 
finding out on per-character basis whether a character is handled by 
both handlers and ignore it in one of them, is there a way to either 
know automatically whether a char is handled by both or translating the 
char to something readable right inside OnKeyDown?
thanks,
max

0
max7512 (1)
9/24/2003 4:10:06 PM
vc.mfc 33608 articles. 0 followers. Follow

1 Replies
1716 Views

Similar Articles

[PageSpeed] 0

"max" <max@nospam.com> schrieb im Newsbeitrag =
news:yljcb.28148$u67.1907@twister.nyc.rr.com...
: I need to handle a few command characters (like DEL and TAB) in a =
window=20
: and also some regular ones, like '+' and '-'. OnKeyDown appears to=20
: detect them all but they are not translates, e.g. '-' comes in as 167=20
: (IIRC). That's why you need OnChar, I suppose. I would be ok with =
having=20
: 2 handlers, but some chars (such as TAB) come properly translated=20
: through both, f*g up my program. (quite a nice framework, he?) Short =
of=20
: finding out on per-character basis whether a character is handled by=20
: both handlers and ignore it in one of them, is there a way to either=20
: know automatically whether a char is handled by both or translating =
the=20
: char to something readable right inside OnKeyDown?
: thanks,
: max

Alas, the inventors of the MFC did not provide a method for OnKeyDown to =
prevent further processing of a keystroke. But you can catch keystrokes =
before they are processed by the MFC.

Add PreTranslateMessage to your window class and process those =
keystrokes your recognize by their virtualkey code there, and process =
other keyboard input in OnChar.

BOOL CMyWnd::PreTranslateMessage(MSG* msg)
{
    if (msg->message =3D=3D WM_KEYDOWN)
    {
        switch (msg->wParam)
        {
            case VK_TAB:
                OnTabKeyDown();
                break;
            ...
            default:
                return FALSE;
        }
        return TRUE;
    }
    return FALSE;
}

HTH
    Heinz
0
wansor42 (24)
9/25/2003 7:06:18 AM
Reply:

Similar Artilces:

Problem w/ CWnd::OnKeydown
Hi all, I am experiencing a strange problem w/ my app. I am using CV6.0SP5 and MFC. I want to be notified when arrow key is pressed when a window has focus, so I overrode OnKeydown. The window belongs to a 2nd window thread created by the first one. The window is a Dialog based one derived by CDialog and contains a big OCX control (about 90% the client size). The keyboard message is sent (WM_KEYDOWN) (according to the TRACER), it is processed by PreTranslateMessage when is lost in the MCF stack. Do you have any idea? Thanx a lot. Regards, Andrea Arrow keys are kinda special when it ...

settin OnKeyDown event
Hello there I have dinamic form which build dinamicly by table selected. I need to add OnKeyDown event on runtime. When adding event not hardcoded control, it can be done only if I will connect it to macro or function: ctl.OnKeyDown="=Func()" the problem in case of OnKeyDown is that it provide parameters that i need to use on function: KeyCode and shift How can I get this parameteres from event into function? you have to use a Event Procedure, ie change from =Func() To [Event Procedure] & Click on the builder icon (...) HtH Pieter "Roy Goldhammer" <roy...

OnKeyDown
Hi, I have a dialog without a titlebar, with tabctrl in it... i need this dialog object to look if the esc(or any other)key is pressed and if that is true to close itself. I tried to handle OnKeyDown messages but they seem not to be sent at all(to this dialog). What can i do? Add PreTranslateMessage(...) to your dialog class and then code like this:- BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) AfxMessageBox("escape"); return CDialog::...

Use of OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) on DialogBased App
Hi Everybody, can you please tell me how we can use OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) on Dialog Based Application. >Hi Everybody, can you please tell me how we can use OnKeyDown(UINT >nChar, UINT nRepCnt, UINT nFlags) on Dialog Based Application. What are you trying to achieve? A dialog doesn't normally handle key presses - they're handled by the currently focused control. Dave Interesting Dave. I just tried putting a OnKeyDown for dialog in VS .NET 2003. But it didn't make sense when WM_KEYDOWN message would be of use to the programmer OR when one...

OnChar/OnKeyDown (again...)
I need to handle a few command characters (like DEL and TAB) in a window and also some regular ones, like '+' and '-'. OnKeyDown appears to detect them all but they are not translates, e.g. '-' comes in as 167 (IIRC). That's why you need OnChar, I suppose. I would be ok with having 2 handlers, but some chars (such as TAB) come properly translated through both, f*g up my program. (quite a nice framework, he?) Short of finding out on per-character basis whether a character is handled by both handlers and ignore it in one of them, is there a way to either know ...

How to implement OnChar?
I have an application I wrote in MSVC4 and OnChar seems to work fine when I compile it for MSVC7. However, when I create my minimal "Hello World" program in MSVC7 and try to implement the OnChar Message handler, it never gets called. When I override OnLButtonDown, that works. Why does not not OnChar work? Thanks, Siegfried "Siegfried Heintze" <siegfried@heintze.com> wrote in message news:OSaMLwQmFHA.3900@TK2MSFTNGP09.phx.gbl... >I have an application I wrote in MSVC4 and OnChar seems to work fine when I > compile it for MSVC7. > > However, when I cre...

Sub-classing, trapping OnChar
I'm doing a bit of sub-classing on CEdit. My objective is to input only digits, which in itself is relatively easy. But I also want the last character of the input field to be either empty, 'K', 'M' or 'G'. If the user attempts to put a digit after the alpha character it's rejected. This was easy also. If the user inputs one of the allowable alpha characters before or after an existing alpha it should replace the exiting alpha character. I'm reading in the edit string using GetWindowText but rather than delete characters that need to be removed before th...

CHtmlView and OnChar
I created an SDI app with CHtmlView as my view class, but cannot recieve WM_CHAR messages, or any keyboard or mouse messages for that matter. I tried routing them thru both the View and Main Frame class but it still doesn't work. Could someone please tell me if there is a way to use these messages with CHtmlView. Thanks in advance. Trap WM_CHAR in PreTranslate method. This particuler ActiveX control that is hosted in CHTMLView does not work as you expect with PreTranslateMessage or trapping WM_CHAR etc. There are couple of ways to do this. One is to automate the control and li...

Catching OnChar while holding ALT
I've subclassed an edit control and want to catch the OnChar() message but when I press ALT+key it does not hit the code. The docs for OnChar() state that the nFlags parameter should hold the information on whether the ALT key is pressed. What gives? Thanks, Drew "Drew" <dam@dam.dam> wrote in message news:OQAkKTquHHA.3668@TK2MSFTNGP03.phx.gbl... > I've subclassed an edit control and want to catch the OnChar() message > but when I press ALT+key it does not hit the code. The docs for OnChar() > state that the nFlags parameter should hold the information...

OnChar event is not triggered..
Hi, I have a problem in CWnd class. I wrote a new class that inherits CWnd, I need OnChar event, but it is not triggered: altoug I used setfocus fuction. What may be I'm missing. Thanks... "Arif �imen" <arifcimen@prowise.com.tr> wrote in message news:%23DPH9LbvEHA.1292@TK2MSFTNGP10.phx.gbl... > Hi, > > I have a problem in CWnd class. > > I wrote a new class that inherits CWnd, I need OnChar event, but it is > not triggered: altoug I used setfocus fuction. > > What may be I'm missing. Are you using this CWnd as a child control in a dialogbo...

Trouble subclassing CEdit and OnChar
I'm trying to create a subclassed CEdit to do some input filtering. One thing that I want to do is convert spaces to underscores. I tried just modifying the nChar value that I'm passing to CEdit::OnChar, but the original character (space) goes through instead of the character I'm substituting. How do I substitute one input character for another in a Subclassed CEdit? Code: void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { if (nChar == VK_SPACE) nChar = '_'; // Limit entry to alphanumeric and the underscore if (isalnum(nChar) || (nChar == '_') ||...

Detecting Alt-Backspace in a KeyDown/OnChar event?
I'm using a CRichEditCtrl and for some reason I can't get to capture that key, Alt-Backspace. (the ALT key doesn't cause either one of those two events be fired... any reason why?) Could someone give me a hint as to how I should go about capturing Alt-Backspace? (I don't want to use an accelerator btw) "Kourosh" <kderakhshan@msn.com> schrieb im Newsbeitrag = news:1144714604.408445.172660@g10g2000cwb.googlegroups.com... > I'm using a CRichEditCtrl and for some reason I can't get to capture > that key, Alt-Backspace. (the ALT key doesn't cause...

OnKeyDown() Problems
Hi guys, My CWnd-derived view (non doc-view architecture) isn't getting WM_KEYDOWN messages. The CFrameWnd-derived mainframe is getting them but not passing them along: BOOL CMainFrame::OnCmdMsg(UINT nID,int nCode,void* pExtra,AFX_CMDHANDLERINFO* pHandlerInfo) { // let view have first crack at the command if(m_activeView->OnCmdMsg(nID,nCode,pExtra,pHandlerInfo)) return TRUE; // otherwise do default handling return CFrameWnd::OnCmdMsg(nID,nCode,pExtra,pHandlerInfo); } Shouldn't the above send off the message ? I was thinking of just calling SendMessa...

Problen with OnKeyDown function
I add OnKeyDown function to my dialog class COnKeyDownDlg but it doesn't work correctly. I don't see any message boxes. ////////////////// void COnKeyDownDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default AfxMessageBox("Test"); switch(nChar) { case VK_END: AfxMessageBox("End Pressed"); break; default: AfxMessageBox("default"); } CDialog::OnKeyDown(nChar, nRepCnt, nFlags); } /////////////////// On 30 Jul 2006 12:22:42 -0700, "hex" <triclosan@mail.ru> wrote: ...

OnKeyDown & SetFocus Problem
I want to use the OnKeyDown function to catch the user input from Dialog. void CMeetingPod_RTCDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default MessageBox("Key is Down"); CDialog::OnKeyDown(nChar, nRepCnt, nFlags); } However, It seems doesn't worked. I notice that it should be the dialog doesn't not be set focus. So, how can i use the 'setfocus' function to set the dialog foucsed? I got problem when I use this function. Thanks a lot. Hi, WM_KEYDOWN will not do for dialogs. Override...

How to prevent OnKeyDown on further processing
Hi, I have a CListView derived class. Normal behavior is: You press a character and the first line which starts with that char is selected. Ok, that works. But in certain situations I don't want the List to change the selected item when a character is pressed. I implemented CListViewRO::OnKeyDown(...) and it's called. In this function there is NO call CListView::OnKeyDown(nChar, nRepCnt, nFlags); But the selection changes. Thanks Norbert Sorry, I bothered you. Found CListView::OnChar, which helps. N. ...

Flag on method OnKeyDown
Hi all, in the method OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags), i receive a char whose flag change depending on the situation. Sometimes, the bit 12 is set to 1, sometimes not. MSDN tells me that it is for internal use. The case where it's set to 1 interess me, because it's when i've a problem in my app. Has someone more information on this bit ? maybe it can help me to solve my problem. Thanks a lot for your help Nicolas H. No one has an idea ? :( nicolas.hilaire@motorola.com a =E9crit : > Hi all, > > in the method OnKeyDown (UINT nChar, UINT nRepCnt,...

OnKeyDown upper case problem
I have an application that responds to any alpha key by opening a dialog and filling an edit box with whatever is typed. It works OK apart from the first character. No matter what the shift or caps lock keys are set to the first character is always upper case. Subsequent characters are as expected. The nChar parameter passed to OnKeyDown shows that the first character is upper case. What is happening? -- Steve Wolstenholme Neural Planner Software EasyNN-plus. The easy way to build neural networks. http://www.easynn.com <steve@tropheus.demon.co.uk> wrote in message news:fgcv...

How to handle CEdit OnKeyDown event?
My application has CEdit and after user input, I checked for the KeyDown event of Return(Enter) key. When I test my application, why does everytime I press the Enter key, the application exit? How to hold it so that user can continue with other application features? Thank you very much. >My application has CEdit and after user input, I checked for the KeyDown >event of Return(Enter) key. > >When I test my application, why does everytime I press the Enter key, the >application exit? Is it a dialog application? Assuming it is, the Enter key is the default action in a di...

OnKeyDown .... for the <ENTER> Key
As silly as this may sound, I can't find the CWnd member function (if any) that handles the message when the <ENTER> key is pressed. First I thought OnKeyDown, but it doesn't work. Thanks a lot See if these KB articles help you: Q102589 Q122489 "Abelardo Vacca" <vacca@gebo-indust.com> wrote in message news:01d201c34bc8$82f2d2c0$a401280a@phx.gbl... > As silly as this may sound, I can't find the CWnd member > function (if any) that handles the message when the > <ENTER> key is pressed. First I thought OnKeyDown, but it > doesn't work...

OnKeyDown() doesn't work??
Dear All. I'm using C++ .NET and trying to figure out how to read input from the keyboard. I use the following function: void CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { TRACE(" ** key down!\n"); CFormView::OnKeyDown(nChar, nRepCnt, nFlags); } inside my code, but it doesn't seem to work.... And I also tried this following function to catch WM_KEYDOWN, but it also didn't work. :-( LRESULT CTestKbdState2View::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_KEYDOWN: TRACE(" ** key down!\n");...

OnKeyDown not getting called in a dialog-based app?
I'm writing a dialog-based app with quite a few different controls and gadets on the main page. I want to catch the "DEL" key so i can implement a delete function on selected items so I added a message handler to the main dialog frame to catch the WM_KEYDOWN message but it never gets called - i've checked the message map and everything seems to be in order. What am I missing? I've also got another smaller problem with keyboard input because the application will quit if ENTER or ESC is pressed (a bit harsh for my purposes). I stopped it quitting on ENTER by comme...

OnKeyDown not called for VK_DOWN or VK_RIGHT but is called for others.
In my Dialog App on Win98 VC6 MFC, my OnKey is not called for VK_DOWN or VK_RIGHT but is called for others including VK_UP and VK_LEFT. Here is a portion of the Spy++ Log (I inserted blank lines to separate the key events). What could be causing this strange behavior? TIA Harvey ----- Spy Log: <00034> 0000030C P WM_KEYDOWN nVirtKey:'C' cRepeat:1 ScanCode:2E fExtended:0 fAltDown:0 fRepeat:0 fUp:0 <00035> 0000030C S WM_GETDLGCODE <00036> 0000030C R WM_GETDLGCODE fuDlgCode:0000 <00037> 0000030C P WM_CHAR chCharCode:'c' (99) cRepeat:1 ScanCode:2E fExtended:...

How to check "." character OnKeyDown event using PreTranslateMessa
I want to make a barrier for my CEdit control so that if user input "." character, it will be rejected. How to check it? I use the PreTranslateMessage like this: BOOL CEditExt::PreTranslateMessage(MSG* pMsg) { // TODO: ここに特定なコードを追加するか、もしくは基本クラスを呼び出してください。 if ( pMsg->message == WM_KEYDOWN ) { if ( pMsg->wParam == VK_RETURN ) { CString sText; sText.Empty(); if ( GetWindowTextLength() != 0 ) { GetWindowText(sText); m_pItem->m_dValue = atof(sText); } GetParent()->SetFocus(); return TRUE; } else if ( pMsg->wParam == VK_ESCAPE ) ...