简体   繁体   中英

Populating a DropDown with Excel Special Keys for Dynamic Shortcut Assignment

I am designing a GUI to allow users to assign shortcut keys to macro functions from a UserForm in Microsoft Excel. The form performs exactly as intended, but in order to assign keys I use an InputBox to fetch data from the user.

However, the user cannot enter special keys (like Del, F1-F12, Home, End, etc.) into an InputBox. I wanted to populate a DropDown with all of the regular AND special keys that Excel recognizes as viable shortcuts.

Is there a library object or default array which I can reference to populate my DropDown menu or will I have to find a list and add each item to an array manually?

在此处输入图片说明

You should consider using the KeyDown event on a TextBox to capture the user's input. Put a label to the left of a textbox and use code like this

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    Dim dcText As Scripting.Dictionary

    Set dcText = New Scripting.Dictionary

    If Shift And 2 Then dcText.Add "Ctrl", 2
    If Shift And 4 Then dcText.Add "Alt", 4
    If Shift And 1 Then dcText.Add "Shift", 1

    Me.Label1.Caption = Join(dcText.Keys, "+") & "+"
    Me.TextBox1.Text = Chr$(KeyCode)

End Sub

That code needs a little work (like to limit the options), but it should get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM