This is probably a beginner question, but.. how do I enable shortcuts like Alt-F, Alt-E etc. for opening menus? In my programs, and also in the winmenu example that comes with Dev-C++, these shortcuts do not work.
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You would need to use accelerators
acctablename ACCELERATORS
BEGIN
event, idvalue, [type][options]
.
.
.
END
The ACCELERATORS statement defines one or more accelerators for an application. An accelerator is a keystroke defined by the application to give the user a quick way to perform a task. The TranslateAccelerator function is used to translate accelerator messages from the application queue into WM_COMMAND or WM_SYSCOMMAND messages.
Parameter Description
acctablename Specifies either a unique name or an integer value that identifies the resource.
event Specifies the keystroke to be used as an accelerator. It can be any one of the following character types:
Type Description
"char" A single ASCII character enclosed in double quotation marks. The character can be preceded by a caret (^), meaning that the character is a control character.
ASCII character An integer value representing an ASCII character. The type parameter must be ASCII.
Virtual-key character An integer value representing a virtual key. The virtual key for alphanumeric keys can be specified by placing the uppercase letter or number in double quotation marks (for example, "9" or "C"). The type parameter must be VIRTKEY.
idvalue Specifies an integer value that identifies the accelerator.
type Required only when the event parameter is an ASCII character or a virtual-key character. The type parameter specifies either ASCII or VIRTKEY; the integer value of event is interpreted accordingly. When VIRTKEY is specified and event contains a string, event must be uppercase.
options Specifies the options that define the accelerator. This parameter can be one or more of the following values:
Option Description
NOINVERT Specifies that no top-level menu item is highlighted when the accelerator is used. This is useful when defining accelerators for actions such as scrolling that do not correspond to a menu item. If NOINVERT is omitted, a top-level menu item will be highlighted (if possible) when the accelerator is used.
ALT Causes the accelerator to be activated only if the ALT key is down.
SHIFT Causes the accelerator to be activated only if the SHIFT key is down.
CONTROL Defines the character as a control character (the accelerator is only activated if the CONTROL key is down). This has the same effect as using a caret (^) before the accelerator character in the event parameter.
The ALT, SHIFT, and CONTROL options apply only to virtual keys.
Example
The following example demonstrates the usage of accelerator keys:
1 ACCELERATORS
BEGIN
"^C", IDDCLEAR //; control C
"K", IDDCLEAR //; shift K
"k", IDDELLIPSE, ALT //; alt k
98, IDDRECT, ASCII //; b
66, IDDSTAR, ASCII //; B (shift b)
"g", IDDRECT //; g
"G", IDDSTAR //; G (shift G)
VK_F1, IDDCLEAR, VIRTKEY //; F1
VK_F1, IDDSTAR, CONTROL, VIRTKEY //; control F1
VK_F1, IDDELLIPSE, SHIFT, VIRTKEY //; shift F1
VK_F1, IDDRECT, ALT, VIRTKEY //; alt F1
VK_F2, IDDCLEAR, ALT, SHIFT, VIRTKEY //; alt shift F2
VK_F2, IDDSTAR, CONTROL, SHIFT, VIRTKEY //; ctrl shift F2
VK_F2, IDDRECT, ALT, CONTROL, VIRTKEY //; alt control F2
END
use the HACCEL LoadAccelerators(HINSTANCE hInstance, LPCTSTR lpTableName); funtion to load the accelerator resource and use int TranslateAccelerator(HWND hWnd, HACCEL hAccTable, LPMSG lpMsg); to translate it in your message loop.
When TranslateAccelerator returns a nonzero value and the message is translated, the application should not use the TranslateMessage function to process the message again.
1 ACCELERATORS
BEGIN
VK_DELETE, IDM_EDITCUT, SHIFT, VIRTKEY
VK_INSERT, IDM_EDITCOPY, CONTROL, VIRTKEY
VK_INSERT, IDM_EDITPASTE, SHIFT, VIRTKEY
VK_F1, IDM_HELPCONTENTS, VIRTKEY
END
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the reply, but AFAIK accelerators are used for menu items (which send WM_COMMAND with an ID), not for opening menus. If you think I should use accelerators, then can you explain how to define one for Alt-F and use it to open the File menu?
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"The second subtlety of the WM_CONTEXTMENU message is the recognition that context menus can be invoked from the keyboard, not just by the mouse. ... When the user invokes a context menu from the keyboard, the x and y coordinates are both -1. ..."
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The alt keys for menu items are done automatically by windows. All you need to do is indicate which key to use with the & symbol. For example a menu item named &File would open with alt-f. A menu item named Fi&le would open with alt-l.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> intercept the key press in the message loop and then pop-up the menu
are you talking about WM_SYSCHAR? and what do you use to pop up the menu?
> ...processing the IContextMenu...
> ...subtlety of the WM_CONTEXTMENU message...
I'm not talking about a context menu, but the main menu bar of the window
nobody:
> All you need to do is indicate which key to use with the & symbol
of course I did that (and the WinMenu example has it too), but it just doesn't work
it works if I press alt, release it, then press f, but it doesn't work when I press alt-f
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Erg, sorry, for some reason I thouight you wanted to activate a popup menu within your application using a keyboard shortcut, rather than trying to activate a menubar menu (your post was clear ... my brain was not).
I don't have dev-cpp installed here, so I can't test the sample programs.
But, & before teh character to activate the menu is what you need (as in &File). Is it possible that the sample programs are using the same shortcut key for several items? (i.e. as in, &File, and &Find)?
rr
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
yes, and I quote from that page:
"When a menu is active, the user can select a menu item by pressing the key that corresponds to the item's underlined letter. The user makes the menu bar active by pressing the ALT key to highlight the first item on the menu bar."
so the standard interface specifies that you have to press ALT first and then the underlined letter
I can't find any reference to the alt-f-style shortcuts
> But, & before teh character to activate the menu is what you need (as in &File).
that's what I thought too, but apparently it's not enough
> Is it possible that the sample programs are using the same shortcut key for several items?
nope
thanks
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tried one of my apps, I can either press alt, release it, then press f, or hold alt and press f at the same time, and it opens the file menu either way. Using WinXP and I think I just used a normal menu item like &File
Are you trying to open a sub menu without opening the top level menu first?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
no, I just press Alt-F (together) to open the "&File" menu (which is the first menu on the menu bar) and it doesn't work
can you try the WinMenu example from Dev-C++ ?
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry about the double post, I accidentally hit the Enter key twice (laptop keys are too small for me, I thought I was hit the up arrow, sigh) - while simultaneously exitting the edit box (who says I can't multitask?).
But you can access the menu by hitting alt, then hitting F.
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hm, indeed, in the FileEditor example, the shortcuts work
I guess I'll have to study the differences, but I'm not sure if the answer will be satisfactory (what if, for example, I have to create the menu first and then register the window class using that menu? that's not an option for me)
I also asked on a MS newsgroup, I'll see if I can get a good answer there
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It will open the popup at (0,0) but you can adjust that by getting the window position and the size of the caption and menu... Yes, it's a kludge so it involves work that should not be necessary. Surely there is a better solution.
qWake
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
qWake: I have no idea what you're talking about
it doesn't apply to my problem (which I solved long ago), nor to the new nobody's problem
nobody: funny, my Dev-C++ doesn't have any letter underlined in the menu items (but only in the main menu)
also, I tested your menu item in the winmenu example, and it worked (of course, only the first x was underlined)
however, it didn't exit with Ctrl+X, are you using an accelerator for that?
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Oh wow, I didn't look at the dates on this! Yes, I'm sure you got that fixed by now. But my post did address (and fix) your question, specifically this one:
"I just press Alt-F (together) to open the "&File" menu (which is the first menu on the menu bar) and it doesn't work can you try the WinMenu example from Dev-C++ ?"
Adding "TranslateMenu" fixes what you described. A couple of months too late though, that didn't do you any good. :)
About the underlined letters: my menus will usually show them, but sometimes not at all. I have not identified a clear pattern of occurrence to determine what causes this... But since it does not affect their operation then I didn't make it a priority to find the cause. All that to say: I don't know. :(
qWake
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> But my post did address (and fix) your question,
well, that fixes the winmenu example but not my code :p
here's the whole thread in the newsgroup: http://tinyurl.com/6c4js
(I think google didn't catch one or two of the messages, but MS's interface is really horrible)
> About the underlined letters: my menus will usually show them, but sometimes not at all. I have not identified a clear pattern of occurrence to determine what causes this...
is it about the alt key, by any chance? (do they appear when you press alt?)
Adrian
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> is it about the alt key, by any chance? (do they appear when you press alt?)
Well of course after I spoke about it, I no longer see the problem happening at all: the underlines are always present (and work as intended).
No wait, that's not quite correct. I have six windows currently open, and the underlines of the main menu bar appear on some of these but not all. They appear on HTML Kit, Outlook Express and Firefox. They do not appear (until I press Alt) on Windows Explorer, Notepad and the Win32 Help window. But they all work, and the underlines of the popup submenus are all visible. I cannot think of what programming difference would exist in these various programs to cause this particular effect. I will try to pay more attention to them from now on and let you know if I uncover something relevant.
But now, what is your current status exactly? A few posts above you said that you solved your problem long ago, but you also just said that the solution doesn't fix your code. So I'm not sure if you still have a problem or not, and what it is. Is it just that the underlines don't show?
qWake
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There seem to be two of us with the same problem. In my case (RL) alt highlights the first menu item, but the underlines still don't appear. I have no underlines under any of the menu items. I thought the & was supposed to make them appear? In particular, I'm used to CTRL+letter making the relevant selection work, which it does, but missing the underlines is weird.
One more observation: This seems to happen even on professionally written software. E.g., IE right now doesn't have underlined menu items, but when I push ALT they appear---and then disappear again when I do something else. Could I be out of memory or something?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is probably a beginner question, but.. how do I enable shortcuts like Alt-F, Alt-E etc. for opening menus? In my programs, and also in the winmenu example that comes with Dev-C++, these shortcuts do not work.
Adrian
You would need to use accelerators
acctablename ACCELERATORS
BEGIN
event, idvalue, [type] [options]
.
.
.
END
The ACCELERATORS statement defines one or more accelerators for an application. An accelerator is a keystroke defined by the application to give the user a quick way to perform a task. The TranslateAccelerator function is used to translate accelerator messages from the application queue into WM_COMMAND or WM_SYSCOMMAND messages.
Parameter Description
acctablename Specifies either a unique name or an integer value that identifies the resource.
event Specifies the keystroke to be used as an accelerator. It can be any one of the following character types:
Type Description
"char" A single ASCII character enclosed in double quotation marks. The character can be preceded by a caret (^), meaning that the character is a control character.
ASCII character An integer value representing an ASCII character. The type parameter must be ASCII.
Virtual-key character An integer value representing a virtual key. The virtual key for alphanumeric keys can be specified by placing the uppercase letter or number in double quotation marks (for example, "9" or "C"). The type parameter must be VIRTKEY.
idvalue Specifies an integer value that identifies the accelerator.
type Required only when the event parameter is an ASCII character or a virtual-key character. The type parameter specifies either ASCII or VIRTKEY; the integer value of event is interpreted accordingly. When VIRTKEY is specified and event contains a string, event must be uppercase.
options Specifies the options that define the accelerator. This parameter can be one or more of the following values:
Option Description
NOINVERT Specifies that no top-level menu item is highlighted when the accelerator is used. This is useful when defining accelerators for actions such as scrolling that do not correspond to a menu item. If NOINVERT is omitted, a top-level menu item will be highlighted (if possible) when the accelerator is used.
ALT Causes the accelerator to be activated only if the ALT key is down.
SHIFT Causes the accelerator to be activated only if the SHIFT key is down.
CONTROL Defines the character as a control character (the accelerator is only activated if the CONTROL key is down). This has the same effect as using a caret (^) before the accelerator character in the event parameter.
The ALT, SHIFT, and CONTROL options apply only to virtual keys.
Example
The following example demonstrates the usage of accelerator keys:
1 ACCELERATORS
BEGIN
"^C", IDDCLEAR //; control C
"K", IDDCLEAR //; shift K
"k", IDDELLIPSE, ALT //; alt k
98, IDDRECT, ASCII //; b
66, IDDSTAR, ASCII //; B (shift b)
"g", IDDRECT //; g
"G", IDDSTAR //; G (shift G)
VK_F1, IDDCLEAR, VIRTKEY //; F1
VK_F1, IDDSTAR, CONTROL, VIRTKEY //; control F1
VK_F1, IDDELLIPSE, SHIFT, VIRTKEY //; shift F1
VK_F1, IDDRECT, ALT, VIRTKEY //; alt F1
VK_F2, IDDCLEAR, ALT, SHIFT, VIRTKEY //; alt shift F2
VK_F2, IDDSTAR, CONTROL, SHIFT, VIRTKEY //; ctrl shift F2
VK_F2, IDDRECT, ALT, CONTROL, VIRTKEY //; alt control F2
END
use the HACCEL LoadAccelerators(HINSTANCE hInstance, LPCTSTR lpTableName); funtion to load the accelerator resource and use int TranslateAccelerator(HWND hWnd, HACCEL hAccTable, LPMSG lpMsg); to translate it in your message loop.
When TranslateAccelerator returns a nonzero value and the message is translated, the application should not use the TranslateMessage function to process the message again.
1 ACCELERATORS
BEGIN
VK_DELETE, IDM_EDITCUT, SHIFT, VIRTKEY
VK_INSERT, IDM_EDITCOPY, CONTROL, VIRTKEY
VK_INSERT, IDM_EDITPASTE, SHIFT, VIRTKEY
VK_F1, IDM_HELPCONTENTS, VIRTKEY
END
Thanks for the reply, but AFAIK accelerators are used for menu items (which send WM_COMMAND with an ID), not for opening menus. If you think I should use accelerators, then can you explain how to define one for Alt-F and use it to open the File menu?
Adrian
Well, the way I would do it (and it may not be the "right" way) is to intercept the key press in the message loop and then pop-up the menu.
You also may want to read Raymond Chen on handling, displaying, processing the IContextMenu here (there are 9 or 10 articles - I'm kind of leaving you in the middle): http://blogs.msdn.com/oldnewthing/archive/2004/09/24/234113.aspx
This also may be relevant: http://blogs.msdn.com/oldnewthing/archive/2004/09/21/232369.aspx
"The second subtlety of the WM_CONTEXTMENU message is the recognition that context menus can be invoked from the keyboard, not just by the mouse. ... When the user invokes a context menu from the keyboard, the x and y coordinates are both -1. ..."
rr
The alt keys for menu items are done automatically by windows. All you need to do is indicate which key to use with the & symbol. For example a menu item named &File would open with alt-f. A menu item named Fi&le would open with alt-l.
rr:
> intercept the key press in the message loop and then pop-up the menu
are you talking about WM_SYSCHAR? and what do you use to pop up the menu?
> ...processing the IContextMenu...
> ...subtlety of the WM_CONTEXTMENU message...
I'm not talking about a context menu, but the main menu bar of the window
nobody:
> All you need to do is indicate which key to use with the & symbol
of course I did that (and the WinMenu example has it too), but it just doesn't work
it works if I press alt, release it, then press f, but it doesn't work when I press alt-f
Adrian
Erg, sorry, for some reason I thouight you wanted to activate a popup menu within your application using a keyboard shortcut, rather than trying to activate a menubar menu (your post was clear ... my brain was not).
My suggestion seems pretty stupid also ...
Have you tried looking here? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menuprogrammingconsiderations.asp
I don't have dev-cpp installed here, so I can't test the sample programs.
But, & before teh character to activate the menu is what you need (as in &File). Is it possible that the sample programs are using the same shortcut key for several items? (i.e. as in, &File, and &Find)?
rr
rr
> Have you tried looking here?
yes, and I quote from that page:
"When a menu is active, the user can select a menu item by pressing the key that corresponds to the item's underlined letter. The user makes the menu bar active by pressing the ALT key to highlight the first item on the menu bar."
so the standard interface specifies that you have to press ALT first and then the underlined letter
I can't find any reference to the alt-f-style shortcuts
> But, & before teh character to activate the menu is what you need (as in &File).
that's what I thought too, but apparently it's not enough
> Is it possible that the sample programs are using the same shortcut key for several items?
nope
thanks
Adrian
I tried one of my apps, I can either press alt, release it, then press f, or hold alt and press f at the same time, and it opens the file menu either way. Using WinXP and I think I just used a normal menu item like &File
Are you trying to open a sub menu without opening the top level menu first?
no, I just press Alt-F (together) to open the "&File" menu (which is the first menu on the menu bar) and it doesn't work
can you try the WinMenu example from Dev-C++ ?
Adrian
Hmm.. doesn't work with WinMenu here either, I can't think of what I did different in my app to make it work, the menu code all looks about the same.
I'll second black_adder's hmm ...
The WinMenu example does not recognize the shortcut keys, but the other windows examples (example, FileEditor) do.
I'll spend a little time on this and see if I can come up with anything.
rr
Well, I don't know. The alt (or F10) keys do work to access the menu bar, but simultaneously pressing alt-F does not activate the file menu.
Well, I don't know. The alt (or F10) keys do work to access the menu bar, but simultaneously pressing alt-F does not activate the file menu.
Sorry about the double post, I accidentally hit the Enter key twice (laptop keys are too small for me, I thought I was hit the up arrow, sigh) - while simultaneously exitting the edit box (who says I can't multitask?).
But you can access the menu by hitting alt, then hitting F.
rr
hm, indeed, in the FileEditor example, the shortcuts work
I guess I'll have to study the differences, but I'm not sure if the answer will be satisfactory (what if, for example, I have to create the menu first and then register the window class using that menu? that's not an option for me)
I also asked on a MS newsgroup, I'll see if I can get a good answer there
Adrian
You can fake it with this kludge in the window procedure of the demo:
It will open the popup at (0,0) but you can adjust that by getting the window position and the size of the caption and menu... Yes, it's a kludge so it involves work that should not be necessary. Surely there is a better solution.
qWake
Ah, here it is. Just add a line to translate the shortcuts as someone already said above. Your message loop should look like this:
Well, it fixed it at my end.
qWake
Actually I have this already:
and it doesn't show the underlines.
qWake: I have no idea what you're talking about
it doesn't apply to my problem (which I solved long ago), nor to the new nobody's problem
nobody: funny, my Dev-C++ doesn't have any letter underlined in the menu items (but only in the main menu)
also, I tested your menu item in the winmenu example, and it worked (of course, only the first x was underlined)
however, it didn't exit with Ctrl+X, are you using an accelerator for that?
Adrian
Oh wow, I didn't look at the dates on this! Yes, I'm sure you got that fixed by now. But my post did address (and fix) your question, specifically this one:
"I just press Alt-F (together) to open the "&File" menu (which is the first menu on the menu bar) and it doesn't work can you try the WinMenu example from Dev-C++ ?"
Adding "TranslateMenu" fixes what you described. A couple of months too late though, that didn't do you any good. :)
About the underlined letters: my menus will usually show them, but sometimes not at all. I have not identified a clear pattern of occurrence to determine what causes this... But since it does not affect their operation then I didn't make it a priority to find the cause. All that to say: I don't know. :(
qWake
> But my post did address (and fix) your question,
well, that fixes the winmenu example but not my code :p
here's the whole thread in the newsgroup: http://tinyurl.com/6c4js
(I think google didn't catch one or two of the messages, but MS's interface is really horrible)
> About the underlined letters: my menus will usually show them, but sometimes not at all. I have not identified a clear pattern of occurrence to determine what causes this...
is it about the alt key, by any chance? (do they appear when you press alt?)
Adrian
> is it about the alt key, by any chance? (do they appear when you press alt?)
Well of course after I spoke about it, I no longer see the problem happening at all: the underlines are always present (and work as intended).
No wait, that's not quite correct. I have six windows currently open, and the underlines of the main menu bar appear on some of these but not all. They appear on HTML Kit, Outlook Express and Firefox. They do not appear (until I press Alt) on Windows Explorer, Notepad and the Win32 Help window. But they all work, and the underlines of the popup submenus are all visible. I cannot think of what programming difference would exist in these various programs to cause this particular effect. I will try to pay more attention to them from now on and let you know if I uncover something relevant.
But now, what is your current status exactly? A few posts above you said that you solved your problem long ago, but you also just said that the solution doesn't fix your code. So I'm not sure if you still have a problem or not, and what it is. Is it just that the underlines don't show?
qWake
There seem to be two of us with the same problem. In my case (RL) alt highlights the first menu item, but the underlines still don't appear. I have no underlines under any of the menu items. I thought the & was supposed to make them appear? In particular, I'm used to CTRL+letter making the relevant selection work, which it does, but missing the underlines is weird.
One more observation: This seems to happen even on professionally written software. E.g., IE right now doesn't have underlined menu items, but when I push ALT they appear---and then disappear again when I do something else. Could I be out of memory or something?
Nah...
You're just running Windows XP