MFC reacting to arrow keys

This is a discussion on MFC reacting to arrow keys within the C++ Programming forums, part of the General Programming Boards category; Greetings and such, Can anyone tell me how to catch the keyboard input of up and down arrows in an ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Question MFC reacting to arrow keys

    Greetings and such,
    Can anyone tell me how to catch the keyboard input of up and down arrows in an MFC dialog box?
    Keydown doesn't seem to work;

    void CRCISDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // TODO: Add your message handler code here and/or call default
    AfxMessageBox("On Key Down");
    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
    }

    This afx message box never pops up...

    thanks,
    Lyle

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    This contibution should be in Windows Programming board, but I will respond.

    Only window with keyboard focus can receive keyboard messages. Such a window is only one in the system (or none). Windows determines which thread has created the window with focus and post keyboard messages to the queue of that thread. You retrieve the messages from queue and dispatch them to appropriate window procedure. The basic facts so far.

    Dialog box is a window with some child controls. Controls are windows created form predefined window classes, each with own window procedure. Dialog window itself is also based on system window class and has private window procedure, however you can provide dialog procedure, which is called back from dialog window procedure, to customize dialog behavior. For modeless dialog you must supply message loop. You don't do it for modal dialog. Basic facts, too.

    When some control has a focus, it should receive all keyboard messages. It's not completely true. As you might have noticed whenever you press up or down arrow in an edit box, system moves keyboard focus to the next control in a group. Same for button with little change - it also uses left and right arrows to move to the next control. It is because the message loop which retrieves and dispatches the messages does some kind of preprocessing by IsDialogMessage function.

    The retrieved message is first passed to [/I]IsDialogMessage (hwndDlg, &msg)[/I]. It can be a keyboard message. This function checks if the message is for specified dialog window, or any of its child control windows. If so, it processes the message, if not, it does nothing.

    If the message is from keyboard, IsDialogMessage asks related window (control) if it needs to process this message. It does it by sending WM_GETDLGCODE to the control. Control responds appropriately. Edit box needs left and right arrow keys, it doesn't need up and down arrows. Button doesn't need any of arrow keys. If the control responds negatively, IsDialogMessage uses the key in its own way - it moves focus to another window, for example. This is default keyboard interface that Windows use in dialog boxes.

    You see, if you want to get rid of this default behavior, you must omit IsDialogMessage call in your message loop. I don't know how you would do this in modal dialog, as it is probably impossible, however there is a chance for modeless dialog, because it has accessible message loop.

    --golem

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Talking

    For anyone interested Marting from Clear Creek Software came up with what got me out of the hole. It was a matter of going to the acceleration tables (under resources) and catching the keystroke as a hotkey.

    thanks anyway,
    Lyle "Alzado" Miller

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 03:35 PM
  2. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 12:29 PM
  3. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  4. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-07-2002, 11:29 PM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 10:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21