Disable the Alt+F4 Key Combination to Keep the Window from Closing

Disable the Alt+F4 Key Combination to Keep the Window from Closing

Assume you have an MFC application. The solution is to override the virtual function:

 virtual BOOL CWinApp::ProcessMessageFilter(int code, LPMSG lpMsg);

The following code demonstrates:

 BOOL CMyProgramApp::ProcessMessageFilter(int code, LPMSG lpMsg){  if(WM_SYSKEYDOWN == lpMsg->message)  {    if(VK_F4 == lpMsg->wParam)    {      //Eliminate Alt+F4      if(::GetKeyState(VK_MENU) < 0)      {        //Do nothing        return TRUE; //We are processing, not the System      }    }  }  return CWinApp::ProcessMessageFilter(code, lpMsg);}

You can also check if the action is from a given dialog or control in that dialog. You can also use the Alt+F4 key combination for other purposes. For instance, you could replace it with a rarely-used key, like Alt+F24, which is not possible from a standard keyboard.

 BOOL CMyProgramApp::ProcessMessageFilter(int code, LPMSG lpMsg){  if(m_hwndDialog != NULL)  {    if((lpMsg->hwnd == m_hwndDialog) || ::IsChild(m_hwndDialog,lpMsg->hwnd))    {      if(WM_SYSKEYDOWN == lpMsg->message)      {        if(VK_F4 == lpMsg->wParam)        {          //Eliminate Alt+F4          if(::GetKeyState(VK_MENU) < 0)          {            //Use VK_F24 instead VK_F4, in this way Alt+F4 can be used forother purposes            ::PostMessage(lpMsg->hwnd, WM_SYSKEYDOWN, VK_F24,lpMsg->lParam);            //We are processing, not the System            return TRUE;          }        }      }    }  }  // Default processing of the message.  return CWinApp::ProcessMessageFilter(code, lpMsg);}

Note: Only the combinations Alt+F4 and Alt+Shift+F4 are interpreted as System Keys that close the window. The combinations Ctrl+Alt+F4 and Shift+Ctrl+Alt+F4 are not detected, in these cases F4 goes strangely as TAB.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several