Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CDialog and Keyboard events

CDialog and Keyboard events

Scheduled Pinned Locked Moved C / C++ / MFC
c++testingbeta-testinghelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    braveheartkenya
    wrote on last edited by
    #1

    I have dialog (window/form) that inherits from the CDialog class. This dialog has various controls on it including a few buttons and textboxes. I am trying to get some keyboard events (key presses) to fire some events but its not working. In the Header file for the dialog i have this as a protected declaration [code] afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); [/code] I have an ON_WM_KEYDOWN() event created and its corresponding method in the .cpp file [code] void COpenGLMFCDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) [/code] Within this method i have written some test code to change the text in a textbox just to see whether the event is fired correctly, but the text is never changed. I also tried changing the dialog's name and also firing some Message Boxes ... but nothing worked. The code in the "OnKeyDown" method is as follows: [code] void COpenGLMFCDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){ switch(nChar) { case VK_RETURN: SetWindowText("You pressed Enter"); break; case VK_F1: SetWindowText("Help is not available at the moment"); break; case VK_DELETE: //SetWindowText("Can't Delete This"); //AfxMessageBox("Testing"); m_Txt_Test.SetWindowText("The Event was fired"); break; case VK_DIVIDE: //SetWindowText("Testing"); m_Txt_Test.SetWindowText("Another Event fired"); break; default: SetWindowText("Whatever"); } } [/code] Could someone please (pretty please) tell me why this isn't working and whether i have done all that is needed for a keyboard event to fired & a result to be seen on screen. Thanks in advance.

    H 1 Reply Last reply
    0
    • B braveheartkenya

      I have dialog (window/form) that inherits from the CDialog class. This dialog has various controls on it including a few buttons and textboxes. I am trying to get some keyboard events (key presses) to fire some events but its not working. In the Header file for the dialog i have this as a protected declaration [code] afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); [/code] I have an ON_WM_KEYDOWN() event created and its corresponding method in the .cpp file [code] void COpenGLMFCDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) [/code] Within this method i have written some test code to change the text in a textbox just to see whether the event is fired correctly, but the text is never changed. I also tried changing the dialog's name and also firing some Message Boxes ... but nothing worked. The code in the "OnKeyDown" method is as follows: [code] void COpenGLMFCDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){ switch(nChar) { case VK_RETURN: SetWindowText("You pressed Enter"); break; case VK_F1: SetWindowText("Help is not available at the moment"); break; case VK_DELETE: //SetWindowText("Can't Delete This"); //AfxMessageBox("Testing"); m_Txt_Test.SetWindowText("The Event was fired"); break; case VK_DIVIDE: //SetWindowText("Testing"); m_Txt_Test.SetWindowText("Another Event fired"); break; default: SetWindowText("Whatever"); } } [/code] Could someone please (pretty please) tell me why this isn't working and whether i have done all that is needed for a keyboard event to fired & a result to be seen on screen. Thanks in advance.

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Hello braveheartkenya , If you have each control's in the form then WM_KEYDOWN and UP isn't work and All Event's(WM_KEYDOWN and Up) send to controls unless in the form non controls

      B 1 Reply Last reply
      0
      • H Hamid Taebi

        Hello braveheartkenya , If you have each control's in the form then WM_KEYDOWN and UP isn't work and All Event's(WM_KEYDOWN and Up) send to controls unless in the form non controls

        B Offline
        B Offline
        braveheartkenya
        wrote on last edited by
        #3

        So how do i put the focus on the Dialog itself rather than on a control. The focus is currently on a button and i would like to put it on the dialog so that the events will be sent there.

        H 1 Reply Last reply
        0
        • B braveheartkenya

          So how do i put the focus on the Dialog itself rather than on a control. The focus is currently on a button and i would like to put it on the dialog so that the events will be sent there.

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          I think It's not possible. I suggestion for example: delete all controls in the from(the form is empty) I think now WM_KEYDOWN is work. or if you need to controls in the form you can create controls in the application Example: Like this for each control //////////////in the Header file////////////////////// #define IDC_BUTTON1 1000 .... class CMyCpp: public CDialog { ... ... ... DECLARE_MESSAGE_MAP() CButton m_Button; afx_msg void OnBnClickedButton(); }; /////////In the cpp file///////// void CMyCpp::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_BUTTON1, m_Button); } BEGIN_MESSAGE_MAP(CMyCppCDialog) ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton) END_MESSAGE_MAP() BOOL CMyCpp::OnInitDialog() { m_Button.Create("MyButton",WS_CHILD|WS_VISIBLE...,CRect(0,0,100,25),this,IDC_BUTTON1); CDialog::OnInitDialog(); } void CMyCpp::OnBnClickedButton() { }

          B 1 Reply Last reply
          0
          • H Hamid Taebi

            I think It's not possible. I suggestion for example: delete all controls in the from(the form is empty) I think now WM_KEYDOWN is work. or if you need to controls in the form you can create controls in the application Example: Like this for each control //////////////in the Header file////////////////////// #define IDC_BUTTON1 1000 .... class CMyCpp: public CDialog { ... ... ... DECLARE_MESSAGE_MAP() CButton m_Button; afx_msg void OnBnClickedButton(); }; /////////In the cpp file///////// void CMyCpp::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_BUTTON1, m_Button); } BEGIN_MESSAGE_MAP(CMyCppCDialog) ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton) END_MESSAGE_MAP() BOOL CMyCpp::OnInitDialog() { m_Button.Create("MyButton",WS_CHILD|WS_VISIBLE...,CRect(0,0,100,25),this,IDC_BUTTON1); CDialog::OnInitDialog(); } void CMyCpp::OnBnClickedButton() { }

            B Offline
            B Offline
            braveheartkenya
            wrote on last edited by
            #5

            Thanks for the idea ... i'll try it out

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups