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

  • Home
  • SEARCH
  • 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 6786519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:15:18+00:00 2026-05-26T17:15:18+00:00

I want a few menu entries that show accelerators that are normal keys, like

  • 0

I want a few menu entries that show accelerators that are normal keys, like the space-bar or comma key, but I don’t want wxWidgets to make those accelerators itself (because then they can’t be used anywhere in the program, including in things like edit boxes).

Unfortunately, wxWidgets insists on always making anything it recognizes in that column into an accelerator under its control, and simply erases anything it doesn’t recognize.

I’m looking for some way to either put arbitrary text into the accelerator column (which I don’t think exists, I’ve looked at the source code), or get ‘hold of the accelerator table used for the menus so I can modify it myself (haven’t found it yet). Can anyone point me in the right direction?

  • 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-26T17:15:19+00:00Added an answer on May 26, 2026 at 5:15 pm

    I couldn’t find a way to access the menu’s accelerator keys directly, but modifying the accelerator menu text works just as well. Here’s the code I came up with:

    In a header file:

    class accel_t {
        public:
        // If idcount == -1, idlist must be null or terminated with a -1 entry.
        accel_t(): mMenu(0) { }
        accel_t(wxMenuBar *m, int *idlist = 0, int idcount = -1);
        void reset(wxMenuBar *m, int *idlist = 0, int idcount = -1);
        void restore() const;
        void remove() const;
    
        private: //
        struct accelitem_t {
            accelitem_t(int _id, wxAcceleratorEntry _key): id(_id), hotkey(_key) { }
            int id;
            wxAcceleratorEntry hotkey;
        };
        typedef std::vector<accelitem_t> data_t;
    
        void noteProblemMenuItems(wxMenu *m);
        static bool isProblemAccelerator(wxAcceleratorEntry *a);
    
        wxMenuBar *mMenu;
        data_t mData;
    };
    

    In a cpp file:

    accel_t::accel_t(wxMenuBar *m, int *idlist, int idcount) {
        reset(m, idlist, idcount);
    }
    
    void accel_t::reset(wxMenuBar *m, int *idlist, int idcount) {
        mMenu = m;
        mData.clear();
        if (idlist == 0) {
            for (int i = 0, ie = m->GetMenuCount(); i != ie; ++i)
                noteProblemMenuItems(m->GetMenu(i));
        } else {
            if (idcount < 0) {
                int *i = idlist;
                while (*i != -1) ++i;
                idcount = (i - idlist);
            }
    
            for (int *i = idlist, *ie = i + idcount; i != ie; ++i) {
                wxMenuItem *item = mMenu->FindItem(*i);
                if (item) {
                    wxAcceleratorEntry *a = item->GetAccel();
                    if (a != 0) mData.push_back(accelitem_t(*i, *a));
                }
            }
        }
    }
    
    bool accel_t::isProblemAccelerator(wxAcceleratorEntry *a) {
        if (a == 0) return false;
        int flags = a->GetFlags(), keycode = a->GetKeyCode();
    
        // Normal ASCII characters, when used with no modifier or Shift-only, would
        // interfere with editing.
        if ((flags == wxACCEL_NORMAL || flags == wxACCEL_SHIFT) &&
            (keycode >= 32 && keycode < 127)) return true;
    
        // Certain other values, when used as normal accelerators, could cause
        // problems too.
        if (flags == wxACCEL_NORMAL) {
            if (keycode == WXK_RETURN ||
                keycode == WXK_DELETE ||
                keycode == WXK_BACK) return true;
        }
    
        return false;
    }
    
    void accel_t::noteProblemMenuItems(wxMenu *m) {
        // Problem menu items have hotkeys that are ASCII characters with normal or
        // shift-only modifiers.
        for (size_t i = 0, ie = m->GetMenuItemCount(); i != ie; ++i) {
            wxMenuItem *item = m->FindItemByPosition(i);
            if (item->IsSubMenu())
                noteProblemMenuItems(item->GetSubMenu());
            else {
                wxAcceleratorEntry *a = item->GetAccel();
                if (isProblemAccelerator(a))
                    mData.push_back(accelitem_t(item->GetId(), *a));
            }
        }
    }
    
    void accel_t::restore() const {
        if (mMenu == 0) return;
        for (data_t::const_iterator i = mData.begin(), ie = mData.end(); i != ie;
            ++i)
        {
            wxMenuItem *item = mMenu->FindItem(i->id);
            if (item) {
                wxString text = item->GetItemLabel().BeforeFirst(wxT('\t'));
                wxString hotkey = i->hotkey.ToString();
                if (hotkey.empty()) {
                    // The wxWidgets authors apparently don't expect ASCII
                    // characters to be used for accelerators, because
                    // wxAcceleratorEntry::ToString just returns an empty string for
                    // them. This code deals with that.
                    int flags = i->hotkey.GetFlags(), key = i->hotkey.GetKeyCode();
                    if (flags == wxACCEL_SHIFT) hotkey = wx("Shift-") + wxChar(key);
                    else hotkey = wxChar(key);
                }
                item->SetItemLabel(text + '\t' + hotkey);
            }
        }
    }
    
    void accel_t::remove() const {
        if (mMenu == 0) return;
        for (data_t::const_iterator i = mData.begin(), ie = mData.end(); i != ie;
            ++i)
        {
            wxMenuItem *item = mMenu->FindItem(i->id);
            if (item) {
                wxString text = item->GetItemLabel().BeforeFirst(wxT('\t'));
                item->SetItemLabel(text);
            }
        }
    }
    

    The easiest way to use it is to create your menu-bar as normal, with all accelerator keys (including the problematic ones) in place, then create an accel_t item from it something like this:

    // mProblemAccelerators is an accel_t item in the private part of my frame class.
    // This code is in the frame class's constructor.
    wxMenuBar *menubar = _createMenuBar();
    SetMenuBar(menubar);
    mProblemAccelerators.reset(menubar);
    

    It will identify and record the accelerator keys that pose problems. Finally, call the remove and restore functions as needed, to remove or restore the problematic accelerator keys. I’m calling them via messages passed to the frame whenever I open a window that needs to do standard editing.

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

Sidebar

Related Questions

I want to make a WPF Window that behaves like a context menu. So,
I'm want to create a menu for a status bar item like the one
I am invoking camera from my application. i want to remove few menu items
I want to expose few web services but thinking of hosting those as Windows
Hi I have a flash menu showing a few links, but when the user
I am having a problem with my app's menu. I want a few items
Here my story: I want something like the new facebook menu on the left
I'm using eclipse for a few months, I realy like gvim better but eclipse
I want to add the functinality of Home,Back,Searcha and Menu Hard keys via some
I have this asp:menu with a few items and want to be able to

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.