Add shortcut to dynamic buttons

Hi All,

I have a container and I am binding multiple buttons dynamically. My Question is how to add shortcut to these buttons?


If I reall correctly, the buttons are in an array.

So you add the buttons using Btn(0), Btn(1), etc.

Hi Schwarz,

Thanks for your response. I am able to add buttons dynamically. Below are the code snippets.
My Question was how to add a shortcut to these buttons? If possible please provide a piece of codes to give more clarity for me.

For ansOptions as Integer = 0 To ansList.Ubound dim b as new PushButtonAnswerSet b.Visible = true b.Left = ((ansOptions + 1) *(PushButtonAnswerSet(0).Width + 22)) - 94 b.Top = 190 b.Width = 95 b.Height = 30 b.Bold = true b.Caption = ansList(ansOptions) + "(" + Str(ansOptions + 1) + ")" b.Index = ansOptions + 1 Next

OK: b is the reference to your button: attach the Keyboard shortcut to it.
To make the thinks UI friendly, you have to add a visual clue for the user.

Now, if your question incudes “How do I attach a Keyboard Shortcut” to a button, I have to search because I do not recall how I’ve done that15 or more years ago (ton PushButtons).

That is… IF I have that project handly.

I do not have that project handly. A quick search in this forum returns:

I have already gone through this forum link and it says about to add the shortcut on macOS, not in windows. Anyway, I really appreciated your time.

Prakash, the simplest way to add shortcuts is to add them to the menus of the application. That way, the keyboard combination triggers the action regardless of where it is called from.

You may want to add a new menu, or add to existing menus.

What about using a timer and poll the keyboard in its Action Event ?

Yes, that would work. But not in the action event. Poll the Keyboard class.

I have done that in one of my app that stays in the background and pops up when a key combination is pressed.

To add an accelerator key to a button put a & before the letter so:

b.caption = “&Moo”

would allow you to press ALT+M to select that button.

Is that what you were after?

Thanks, everyone for your inputs. I will verify with different approaches as suggested by you and let you know if I get into a conclusion.

Hi Micheal/All,

Hope all are doing well!

As you suggested I have tried with adding shortcuts is to add them to the menus of the application and if user press with CTRL +
any keyword, the shortcut would trigger and it is working as expected. However, I would like to add the shortcut without any CTRL combination. For example, if I press 1 it should trigger the shortcut. If possible could you please provide a piece of code with an example of this.

Any help would be greatly appreciated.


Add a keydown event handler to the window, and deal with the key codes in a select case.
This assumes that none of the controls is a text input control.. you want them all to ignore the key press and allow it to cascade to the window’s handler

Thanks, Jeff for your inputs.

I have the main window and from that main window, I am opening a movable model window. I have added the key down event in both the window(Main & movable model window). However, the key down event is not triggering for both. Any idea?

Have you tested the keydown events for controls on that window?
What control has the focus?
Consider: Which can take the focus?
If you press 1 and the window does not see it, something else is seeing it.
You will need to see which of your controls is eating the keypress