#1
|
|||
|
|||
Show Key Combination Pressed by User
I need the code that would emulate what happens when you click in the edit box across from "Press new keyboard shortcut in Customize Toolbars & Menus > Customize Keyboard (when Keyboard... button is pressed). This is the dialog box in which user assigns a key combination to a macro.
I need to find out exactly what key combination user has pressed inside a text box in a dialog box and show it exactly like MS Word does in Customize Keyboard dialog box., e.g. Command+Control+Shift+A (I'm a Mac user). I'm using TextBox_KeyUp() in a dialog box but for some reason it doesn't register the Option key or perhaps I don't know what the Shift value for the option is key! The MS Words key in their Customize Keyboard dialog works perfectly. All help is appreciated. |
#2
|
||||
|
||||
Windows keyboards don't generally have an option key so that might explain why you can't find out if it has been pressed.
You might like to look at some resources where VBA is used to investigate key states eg http://www.cpearson.com/excel/keytest.aspx https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
I already have all of this rudimentary knowledge about key states. If you read my thread carefully you would know that I'm a Mac user and I specifically stated that the MS Word dialog box DOES indeed detect the Option key on the Mac keyboard. I was wondering if perhaps an MS insider would have the code for the Customize Keyboard key detection code...
|
#4
|
||||
|
||||
In Windows, you might use userform code like:
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) Dim strMask As String, StrKey As String Select Case Shift Case 1: strMask = "Shift+" Case 2: strMask = "Ctrl+" Case 3: strMask = "Ctrl+Shift+" Case 4: strMask = "Alt+" Case 5: strMask = "Alt+Shift+" Case 6: strMask = "Ctrl+Alt+" Case 7: strMask = "Ctrl+Alt+Shift+" Case Else: strMask = "" End Select Select Case KeyCode Case vbKeyF1: StrKey = "F1" Case vbKeyF2: StrKey = "F2" Case vbKeyF3: StrKey = "F3" Case vbKeyF4: StrKey = "F4" Case vbKeyF5: StrKey = "F5" Case vbKeyF6: StrKey = "F6" Case vbKeyF7: StrKey = "F7" Case vbKeyF8: StrKey = "F8" Case vbKeyF9: StrKey = "F9" Case vbKeyF10: StrKey = "F10" Case vbKeyF11: StrKey = "F11" Case vbKeyF12: StrKey = "F12" Case vbKeyHome: StrKey = "Home" Case vbKeyEnd: StrKey = "End" Case vbKeyPageUp: StrKey = "PageUp" Case vbKeyPageDown: StrKey = "PageDown" End Select TextBox1.Text = strMask & StrKey End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
The KeyDown event looks like it makes a lot more sense than the KeyUp event.
I had a little play with Paul's code to add CaseElse's that would reveal the integer values of other keys which may be pressed. I note that combinations can give different results depending on the order of the key presses (eg Ctrl then Alt returns a different result to Alt then Ctrl). The first Case Else should be amended as follows to reveal the integer value of the modifiers Case Else: strMask = Shift & "+" You can then add a CaseElse to the second select Case Else: StrKey = KeyCode
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
key bindings |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Autofill form in word in combination with VBA | nouki | Word VBA | 9 | 10-14-2015 01:20 PM |
Preventing text from moving down in a form when enter is pressed | tgeary00 | Word | 2 | 10-29-2013 06:51 PM |
tcp-t F3 key combination used in template | revans611 | Word | 1 | 11-29-2010 11:57 AM |
Help with Combination function | sanasath | Excel | 0 | 12-13-2005 07:24 AM |
Outlook problem - Open Other User’s Folder doesn’t show Sent | imported_bankboysb | Outlook | 0 | 12-12-2005 11:41 AM |