Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:26:33+00:00 2026-05-29T07:26:33+00:00

I have a business requirement that for message boxes, the user cannot press the

  • 0

I have a business requirement that for message boxes, the user cannot press the enter key to accept the default option, but has to press the key of the option. eg. Given a MessageBox with the options Yes/No, the user must press the Y or N keys. Now I’ve implemented this below using keyboard hooks, but when the code returns, the KeyUp event also gets returned to the calling code as well.

So the question is: How do I flush all the keyboard events before returning to the calling code?

I’ve removed boiler plate code, but if you need it, please advise.

The calling code:

    private static ResultMsgBox MsgResultBaseNoEnter(string msg, string caption, uint options)
    {
        ResultMsgBox res;
        _hookID = SetHook(_proc);
        try
        {
            res = MessageBox(GetForegroundWindow(), msg, caption, options);
        }
        finally
        {
            UnhookWindowsHookEx(_hookID);
        }
        return res;
    }

And the Hook Code:

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            if (vkCode == VK_RETURN)
                return (IntPtr)(-1);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:26:34+00:00Added an answer on May 29, 2026 at 7:26 am

    Add these lines of code somewhere in your class (or in some static class that can be used by other classes):

    [StructLayout(LayoutKind.Sequential)]
    public class MSG
    {
        public IntPtr hwnd;
        public uint message;
        public IntPtr wParam;
        public IntPtr lParam;
        public uint time;
        int x;
        int y;
    }
    
    [DllImport("user32")]
    public static extern bool PeekMessage([Out]MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, int wRemoveMsg);
    
    /// <summary>
    /// Examines the message queue for key messages.
    /// </summary>
    /// <param name="remove">If this parameter is true, the returned message is also removed from the queue.</param>
    /// <returns>Returns the next available key message, or null if there is no key message available.</returns>
    public static MSG PeekKeyMessage(bool remove)
    {
        MSG msg = new MSG();
        if (PeekMessage(msg, IntPtr.Zero, 0x0100 /*WM_KEYFIRST*/, 0x0109 /*WM_KEYLAST*/, remove ? 1 : 0))
            return msg;
        return null;
    }
    
    public static void RemoveAllKeyMessages()
    {
        while (PeekKeyMessage(true) != null) ; // Empty body. Every call to the method removes one key message.
    }
    

    Calling RemoveAllKeyMessages() does exactly what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a business requirement that when the user clicks a series of checkboxes
I have a business requirement that calls for an end user to be able
I have a SharePoint list with Content Approval enabled. The business requirement is that
I have a business user who tried his hand at writing his own SQL
I have a business logic classes that are written in pure C# (without any
I have some business objects which use: Web.Configuration.WebConfigurationManager.AppSettings.Item(SomeSetting) Now that I'm breaking those objects
I have a business object that compiles into a DLL that handles all calculations
Let's say we have a function that changes a password for a user in
I have a business requirement where i need to create a workflow on a
I have a small business that sells website solutions to clients. The websites serve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.