Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Jun 2002
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accelerator Keys Problem (Word XP)

    I'm trying to make a user form that will display a number of command
    buttons, each of which has an accelerator key, then respond with different
    actions depending on the key pressed. (Basically, I'm recreating the old
    WordStar two-key navigation sequences). But I'm having a problem with the
    accelerator keys. I want the commands to be executed
    when the user presses a plain key. I get the dialog to display, but the
    accelerator keys only work if you use them as alt-key combinations. In other words, I
    want Command Button 1 to fire if the user presses "u" when the dialog box is
    on screen, but it will only fire when the user presses Alt+u. How do I get
    the buttons to work with plain keys rather than Alt-combinations? I was
    able to do this just fine in WordBasic.

    I also posted this in the VBA.Beginners forum on the Microsoft.News server. Sorry for the duplication.

    Mark

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accelerator Keys Problem (Word XP)

    Maybe something like this:<pre>Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
    Case 65 ' A
    MsgBox "A"
    Case 66 ' B
    MsgBox "B"
    Case Else
    Debug.Print KeyCode
    End Select

    End Sub</pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    New Lounger
    Join Date
    Jun 2002
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accelerator Keys Problem (Word XP)

    Geoff,

    Thanks for the suggestion--I see the logic of the way you suggest, though it seems like a weirdly clunky way to do something that used to be totally transparent and easy. Have they really changed the way the accelerator keys work? Why? This seems like an amazingly dumb decision. I might as well code the whole thing with old Wordbasic code. I seem to recall getting this working right with Word 97. This must have broken a huge number of programs--how dumb can MS get (don't answer that question)?

    Mark

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Accelerator Keys Problem (Word XP)

    I suffered in the same way when the macro language for Excel was switched to VBA in Excel 5. It drove me crazy until I got the hang of it, but it means that code is largely transferrable between Office applications instead of being so firmly tied to a specific app and that the development environment is the same across all the apps. On the other hand, I've never done much programming in Word and so don't see the purpose of what you're trying to accomplish. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  5. #5
    New Lounger
    Join Date
    Jun 2002
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accelerator Keys Problem (Word XP)

    >> On the other hand, I've never done much programming in Word and so don't see the purpose of what you're trying to accomplish <<

    This isn't really a WordBasic to VBA issue, since I recall, during the brief time I used Word 97, that accelerator keys worked the same way in the VBA 97 userforms as they had in WordBasic dialog boxes. If you're curious (or even if you're not), here's what I'm trying to do. The less one has to take one's hands from the home row of the keyboard, the faster one can type and get work done. So I want to get as many common navigation functions mapped to the home row keys as possible. I've reassigned Ctrl-u to be the equivalent of the page-up key, for instance. But there aren't enough keys to assign all the navigation functions to single keys, so I want to make two-key combinations. So, I'd like Ctrl-L, followed by y, to move the selection point to the beginning of the document, Ctrl-L, followed by j, to move to the beginning of the line, etc. Wordstar had a handy set of navigation key combinations that worked like this. Back in the January 28, 1992, "Working Word" column by David Stone in PC Magazine, he explained how to do the equivalent in WordBasic 2.0. I wanted to port my Wordbasic code to VBA, but it appears that MS has changed the way accelerator keys work--they now work only as Alt-key combinations, rather than as plain keys. OK, try this code:

    Public Sub MAIN()
    REM Macro named QuickMenu
    Dim Button
    WordBasic.BeginDialog 411, 150, "Quick Menu"
    WordBasic.PushButton 217, 73, 127, 22, "&UTop of Doc"
    WordBasic.PushButton 209, 125, 145, 22, "&MBottom of Doc"
    WordBasic.PushButton 130, 99, 122, 22, "&JStart of Line"
    WordBasic.PushButton 260, 99, 114, 22, "&KEnd of Line"
    WordBasic.PushButton 14, 73, 135, 22, "&YTop of Screen"
    WordBasic.PushButton 14, 125, 158, 22, "&NBottom of Screen"
    WordBasic.PushButton 95, 7, 88, 22, "&LPrevious"
    WordBasic.CancelButton 340, 7, 65, 22
    WordBasic.EndDialog
    Dim QuickMenu As Object: Set QuickMenu = WordBasic.CurValues.UserDialog
    Button = WordBasic.Dialog.UserDialog(QuickMenu)
    If Button = 1 Then
    WordBasic.StartOfDocument
    ElseIf Button = 2 Then
    WordBasic.EndOfDocument
    ElseIf Button = 3 Then
    WordBasic.StartOfLine
    ElseIf Button = 4 Then
    WordBasic.EndOfLine
    ElseIf Button = 5 Then
    WordBasic.StartOfWindow
    ElseIf Button = 6 Then
    WordBasic.EndOfWindow
    ElseIf Button = 7 Then
    WordBasic.GoBack
    End If
    End Sub

    One can assign this macro to a key, and it works precisely the way I want it to. With the dialog displayed, I only have to press y to move the selection point to the top of the document. But if I make a VBA userform with a command button, assign code to the button ("Selection.HomeKey Unit:=wdStory"), and assign an accelerator key to the button, the code won't fire unless I press Alt-y when the form is displayed. It's a bizarre change, and a really bad idea. Sorry to go on so long, but do you see what the problem is? I don't want to have to press Ctrl-key, then Alt-key to move the selection point, I want to press Ctrl-key, then plain key. It's weird that this has been changed.

    Mark

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Accelerator Keys Problem (Word XP)

    I think the change was made for consistency with general Windows behavior. Which means that what you want could be done through a menu (CommandBarPopup). See the attached demo. The document contains a new commandbar called Ctrl+L, which is not visible when you open the document. There is a keyboard mapping of Ctrl+L to a macro that makes the menu visible and displays it. Then, there is a macro to implement the first of your desired keyboard navigation options, which is implemented directly from the menu; no keyboard mapping is required. The menu then hides itself. You thus can build as many of these as you like without cluttering your menu bar.

    Oops, forgot to post the code for those not interested in downloading:

    <pre>Sub CtrlL()
    ' Show and "open" the Ctrl+L menu for one-more-key navigation
    With CommandBars("Menu Bar").Controls("Ctrl+L")
    .Visible = True
    .Execute
    End With
    End Sub

    Sub CtrlLy()
    ' Move insertion point to the beginning of the currently
    ' open story and then hide the Ctrl+L menu if it's open
    Selection.HomeKey unit:=wdStory, Extend:=wdMove
    With CommandBars("Menu Bar").Controls("Ctrl+L")
    If .Visible = True Then
    .Visible = False
    End If
    End With
    End Sub</pre>

    Note: this was tested in Word 2000.
    Attached Files Attached Files

  7. #7
    New Lounger
    Join Date
    Jun 2002
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Accelerator Keys Problem (Word XP)

    So there's always more than one way to skin a cat. Thanks for the suggestion, though I think I might go ahead and use the old WordBasic code I have--it still works, and does exactly what I want. I understand that consistency is a good thing, but why does it always seem as if MS, when making things consistent, chooses to downgrade one application's capabilities rather than enhance the others to the same level? I'm still continually frustrated by one instance of this that happened many versions ago. When you select some text, and that text is all in bold or italic, the bold or italic button on the formatting toolbar lights up. It used to be that, when you selected a block of text of which part was in italic or bold, the italic or bold button would appear in a sort of "grey" mode, that let you know that only part of the selection was formatted in the relevant font style. This highly useful capability was eliminated (I think in WinWord 95), for consistency w/ Excel. I still get aggravated about that one.

    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •