Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Keydown/up events in userform to set shortcut keys (Revisited)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lisa - 08 Feb 2007 05:07 GMT
The other day I posted a question re. setting up a user form to assign
shortcut keys to various macros. (Thank you Perry and Jean-Guy for assistance
with this!)

This is sort of an update -- with new code and three new questions... (And
sorry for the excrutiatingly long post.)

Below is the code I finally ended up with. It seems to work the way I want
it to. That is, it assigns a shortcut key to a macro based on user input in a
userform text box. (The shortcut key combo must include Ctrl or Alt or both
and an alphanumeric key -- and may include the Shift key.)

However, I'm new to all of this coding stuff, and would very much appreciate
help fixing a few things if feasible. I've been trying, but I haven't figured
out how.
Specifically:

1) I have a total of 10 text boxes that will be used to assign shortcut keys
to 10 different macros. The KeyDown code for each of these would be identical
except for "TextBox1.text = strKeySequence" at the end of the macro.
(Obviously, that would vary by text box.)

Also the KeyUp code for each could be identical  except for the
"strCurrent=TextBox1.text" at the beginning of the macro and the command
assigned to the shortcut key.

I've tried putting the common bits of code in a separate sub, but it won't
compile ("Argument not optional").

How can I push the "meat" of the code out of the field-specific stuff, so I
don't have the same thing repeated 10 times (once for each text box)?

2) I think there's probably a more elegant/streamlined way to do the
keybinding based on what the user enters in the text box. However, my
attempts to use the "strKeySequence" more directly/efficiently have not
worked.

How can I better capture the KeyDown info and use it in the KeyUp code, so I
don't have all those silly-looking "Ifs" in the KeyUp?

3) Everytime I press the Alt key in the (modal) userform text box, I get a
"ding" (error-type sound... I don't know what it's officially called). I
don't get this when pressing other keys (Ctrl, Shift, etc.)

How can I make that annoying dinging stop?

Thanks in advance for any help/guidance.

THE INELEGANT BUT FUNCTIONAL CODE

Public myKey, myChar As String

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
'Based on code from Peter Hewett posted to _
microsoft.public.word.vba.general

Const MaskShift As Integer = 1
Const MaskCtrl As Integer = 2
Const MaskAlt As Integer = 4
Dim strKeys As String
' Ignore keystroke sequence unless Ctrl and/or Alt is pressed as well
If Shift And (MaskCtrl Or MaskAlt) Then
   ' Add Ctrl/Alt/Shift prefix text
   If Shift And MaskCtrl Then
   strKeys = "Ctrl"
   End If
   If Shift And MaskAlt Then
       If LenB(strKeys) > 0 Then
           strKeys = strKeys & "+Alt"
       Else
           strKeys = "Alt"
       End If
   End If
   If Shift And MaskShift Then
       If LenB(strKeys) > 0 Then
           strKeys = strKeys & "+Shift"
       Else
           strKeys = "Shift"
       End If
   End If
   'For use in KeyUp to determine the Ctrl, Alt, and Shift combo for
shortcut key
   myKey = strKeys
   ' Keyboard character
   If LenB(strKeys) > 0 Then
       strKeys = strKeys & "+" & Chr$(KeyCode)
   Else
       strKeys = Chr$(KeyCode)
   End If
   myChar = (KeyCode)
Else: strKeys = ""
End If
  TextBox1.Text = strKeys
End Sub

Private Sub TextBox1_KeyUp(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
Dim strCurrent As String
strCurrent = TextBox1.Text
If LenB(strCurrent) = 2 Then
   msgbox "need to use ctrl or alt (or both)"
   Else
       If myKey = "Ctrl" Then
           CustomizationContext = ActiveDocument.AttachedTemplate
           KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, myChar), _
           KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
       End If
       If myKey = "Ctrl+Alt" Then
           CustomizationContext = ActiveDocument.AttachedTemplate
           KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
           wdKeyAlt, myChar), _
           KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
       End If
       If myKey = "Ctrl+Shift" Then
           CustomizationContext = ActiveDocument.AttachedTemplate
           KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
           wdKeyShift, myChar), _
           KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
       End If
       If myKey = "Ctrl+Alt+Shift" Then
           CustomizationContext = ActiveDocument.AttachedTemplate
           KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
           wdKeyAlt, wdKeyShift, myChar), _
           KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
       End If
       If myKey = "Alt" Then
           CustomizationContext = ActiveDocument.AttachedTemplate
           KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, myChar), _
           KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
       End If
       If myKey = "Alt+Shift" Then
           CustomizationContext = ActiveDocument.AttachedTemplate
           KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, _
           wdKeyShift, myChar), _
           KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
       End If
End If
myKey = ""
End Sub
Perry - 08 Feb 2007 18:55 GMT
Create a new userform, and place 3 textboxes (names: TextBox1 .. TextBox3)
Paste below code in the form's codegrid:

'==begin Userform code
Dim arrTB(2) As Class1

Private Sub UserForm_Initialize()
   For x = 0 To 2
       Set arrTB(x) = New Class1
       Set arrTB(x).MyTb = Me.Controls("Textbox" & CStr(x + 1))
   Next
End Sub
'==end Userform code

Re-assigning the eventhandler
Add a new classmodule to yr VBA project, and use the default name: Class1
Add following code to it:
it's the same code as Peter Hewet for the TextBox keydown event but now
hosted in a classmodule that will re-assign the eventhandlers)

'== Code to re-assign eventhandler in Class1 classmodule
Public WithEvents MyTb As TextBox

Private Sub MyTb_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift
As Integer)
Const MaskShift As Integer = 1
Const MaskCtrl As Integer = 2
Const MaskAlt As Integer = 4
Dim strKeys As String

If Shift And (MaskCtrl Or MaskAlt) Then

   If Shift And MaskCtrl Then
   strKeys = "Ctrl"
   End If
   If Shift And MaskAlt Then
       If LenB(strKeys) > 0 Then
           strKeys = strKeys & "+Alt"
       Else
           strKeys = "Alt"
       End If
   End If
   If Shift And MaskShift Then
       If LenB(strKeys) > 0 Then
           strKeys = strKeys & "+Shift"
       Else
           strKeys = "Shift"
       End If
   End If

   myKey = strKeys

   If LenB(strKeys) > 0 Then
       strKeys = strKeys & "+" & Chr$(KeyCode)
   Else
       strKeys = Chr$(KeyCode)
   End If
   myChar = (KeyCode)
Else: strKeys = ""
End If
  MyTb.Text = strKeys
End Sub
'== End Code to re-assign eventhandler in Class1 classmodule
--
Krgrds,
Perry

System: Vista/Office Ultimate
> The other day I posted a question re. setting up a user form to assign
> shortcut keys to various macros. (Thank you Perry and Jean-Guy for
[quoted text clipped - 145 lines]
> myKey = ""
> End Sub
Perry - 08 Feb 2007 19:13 GMT
I didn't thought CTRL key had a callback until I read Peter Hewett's code.
Combination of (shift And 2) hmmm ... nifty

Why hasn't Peter been awarded the MVP status by the way ???
His contributions were ... solid and of great (re)solvability

--
Krgrds,
Perry

System: Vista/Office Ultimate
> Create a new userform, and place 3 textboxes (names: TextBox1 .. TextBox3)
> Paste below code in the form's codegrid:
[quoted text clipped - 217 lines]
>> myKey = ""
>> End Sub
Jean-Guy Marcil - 08 Feb 2007 19:37 GMT
Perry was telling us:
Perry nous racontait que :

> Why hasn't Peter been awarded the MVP status by the way ???
> His contributions were ... solid and of great (re)solvability

If I remember correctly, he was, but then not long after the award, he
dropped out of sight and wasn't consequently re-awarded as he was absent
from public life. I now he is still involved with this kind of work (he
contacted me not long ago regarding some code he had misplaced...) but I
guess he hasn't got the time to be involved in the NG anymore.

I do concur with you that he was very creative and knowledgeable, too bad he
isn't active anymore in the NG...

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Perry - 09 Feb 2007 20:26 GMT
If you ever get to communicate with him, give him my sincere regards.

It's like missing Ibby in these newsgroups.
Where as Peter was elaborating quite a bit including explanations, Ibby was
to-the-point and supplied solutions
rather than explanations.

Both aussies not?

--
Krgrds,
Perry

System: Vista/Office Ultimate

> Perry was telling us:
> Perry nous racontait que :

>> Why hasn't Peter been awarded the MVP status by the way ???
>> His contributions were ... solid and of great (re)solvability
[quoted text clipped - 7 lines]
> I do concur with you that he was very creative and knowledgeable, too bad
> he isn't active anymore in the NG...
Lisa - 08 Feb 2007 19:19 GMT
Thank you Perry! This looks to be exactly what I was hoping to accomplish,
but didn't have a clue as to how to do...

Will give it a whirl tonight. REALLY appreciate your help!

> Create a new userform, and place 3 textboxes (names: TextBox1 .. TextBox3)
> Paste below code in the form's codegrid:
[quoted text clipped - 213 lines]
> > myKey = ""
> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.