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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Win32 capture Escape and Enter key window message.

Win32 capture Escape and Enter key window message.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
3 Posts 3 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.
  • H Offline
    H Offline
    Hirakawa
    wrote on last edited by
    #1

    Good day. I have a question about how to capture escape and enter key window message in Win32. Normally in MFC, I will use PreTranslateMessage like below to capture enter and escape key...

    BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
    {
    if (pMsg->message == WM_KEYDOWN) {
    if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN || pMsg->wParam==VK_CANCEL) {
    return FALSE;
    }
    }

    return CDialog::PreTranslateMessage(pMsg);
    

    }

    But somehow it seems that enter key and escape key cannot be captured in Win32 when I waiting for WM_KEYDOWN window message like below:

    int CALLBACK TestDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    switch (Message) {
    case WM_KEYDOWN:
    switch (wParam) {
    case VK_ESCAPE:
    MessageBox(NULL, "VK_ESCAPE", "WM_KEYDOWN", MB_OK);
    break;

    			case VK\_RETURN:
    				MessageBox(NULL, "VK\_RETURN", "WM\_KEYDOWN", MB\_OK);
    				break;
    		}
    
    		break;
    }
    

    }

    Please help me. Is there anything I haven't done to make my code work?

    R C 2 Replies Last reply
    0
    • H Hirakawa

      Good day. I have a question about how to capture escape and enter key window message in Win32. Normally in MFC, I will use PreTranslateMessage like below to capture enter and escape key...

      BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
      {
      if (pMsg->message == WM_KEYDOWN) {
      if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN || pMsg->wParam==VK_CANCEL) {
      return FALSE;
      }
      }

      return CDialog::PreTranslateMessage(pMsg);
      

      }

      But somehow it seems that enter key and escape key cannot be captured in Win32 when I waiting for WM_KEYDOWN window message like below:

      int CALLBACK TestDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
      {
      switch (Message) {
      case WM_KEYDOWN:
      switch (wParam) {
      case VK_ESCAPE:
      MessageBox(NULL, "VK_ESCAPE", "WM_KEYDOWN", MB_OK);
      break;

      			case VK\_RETURN:
      				MessageBox(NULL, "VK\_RETURN", "WM\_KEYDOWN", MB\_OK);
      				break;
      		}
      
      		break;
      }
      

      }

      Please help me. Is there anything I haven't done to make my code work?

      R Offline
      R Offline
      Rajkumar R
      wrote on last edited by
      #2

      I can suggest you 2 approach now, 1) use WM_COMMAND (wparam = IDOK for Enter key, and wparam = IDCANCEL for ESC key) and use different Dlg ID for OK and CANCEL key if your dialog has one. 2) in the main message loop put a route for your pretranslate function

      // Main message loop:
      while (GetMessage(&msg, NULL, 0, 0))
      {
      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
      {
      BOOL bTranslate = TRUE;

                         if (msg.hwnd is your dialog window)
                         {
                           bTranslate = pretranslate(); // get your key message 
                         }
      
      		if (bTranslate)
                              TranslateMessage(&msg);
      		DispatchMessage(&msg);
      	}
      }
      

      I didn't tried please try and may us know

      1 Reply Last reply
      0
      • H Hirakawa

        Good day. I have a question about how to capture escape and enter key window message in Win32. Normally in MFC, I will use PreTranslateMessage like below to capture enter and escape key...

        BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
        {
        if (pMsg->message == WM_KEYDOWN) {
        if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN || pMsg->wParam==VK_CANCEL) {
        return FALSE;
        }
        }

        return CDialog::PreTranslateMessage(pMsg);
        

        }

        But somehow it seems that enter key and escape key cannot be captured in Win32 when I waiting for WM_KEYDOWN window message like below:

        int CALLBACK TestDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
        {
        switch (Message) {
        case WM_KEYDOWN:
        switch (wParam) {
        case VK_ESCAPE:
        MessageBox(NULL, "VK_ESCAPE", "WM_KEYDOWN", MB_OK);
        break;

        			case VK\_RETURN:
        				MessageBox(NULL, "VK\_RETURN", "WM\_KEYDOWN", MB\_OK);
        				break;
        		}
        
        		break;
        }
        

        }

        Please help me. Is there anything I haven't done to make my code work?

        C Offline
        C Offline
        Catherine Sea
        wrote on last edited by
        #3

        Hi, You can also try SetWindowsHookEx API. ---- Catherine Sea Dynamsoft Corporation

        www.dynamsoft.com the leading developer of version control and issue tracking software

        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