Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a keycode while editing text (Word, VB6.3)

Status
Not open for further replies.

565u

Technical User
Apr 25, 2005
46
CZ
Hi and thanks for reading my post!

My situation is during editing/writing text in word. No forms or objects. Just text.

I have a series of macros I need to assign to several (alt+key)s. But since the macros are very similar I would like to do something nicer. I would like to have one macro assigned to all these (alt+key)s and inside the macro read what key aside from alt is being pressed and then run the right part of the code.

The problem obviously is I don't think I can read the keycode outside a form or an object.

Anybody smarter than me out there please?

Best regards,

Pavel
 
I do not quite understand. ONE keyboard shortcut fires ONE procedure.

Within that procedure, what happens is strictly a matter of the logic written into the procedure.

Think of this logically.

Say your super macro is fired by Alt-M. Alt-M fires ONE procedure.

Sub myAlt_M()
' the code
End Sub

"inside the macro read what key aside from alt is being pressed"

It will always be "M".

You could, though, use the one key shortcut macro to get another. I am not sure how efficient this would be, but it is a possibility. Say you had two procedures (macros) previously using Alt-A and Alt-D respectively.
Code:
Sub myAlt_M()
Dim response As String
response = Inputbox("Enter a single letter for the " & _
    "macro you wish to run.")
Select Case Ucase(response)
   Case "A"
      ' execute the previous Alt-A macro
   Case "D"
      " execute the previous Alt-D macro
   etc.
   etc.
End Select
End Sub
You use Ucase(response) so it does not matter if they input "a" or "A".

Or, you could use the super macro to display a userform with all the other showing. The user could select one and fire that macro.

Or, you could have all your macros as menu items.

However, a shortcut (Alt-letter) fires ONE procedure - whatever it is assigned to it. Any reading of the keypress (the letter) will return that letter, and only that letter.

faq219-2884

Gerry
My paintings and sculpture
 
Thank you for writing back.

Pavel
 
Short of getting real messy and having to use an API to check for keyboard state, I don't believe there is any way to do exactly what you're asking. Short of that you could assign each hot key its own 1-line subroutine and call a generalized routine passing in a constant keyboard value:
Code:
Public Sub HandleHotKeyM()
    Call HandleAnyHotKey("M")
End Sub

Public Sub HandleHotKeyX()
    Call HandleAnyHotKey("X")
End Sub

Public Sub HandleAnyHotKey(HotKey As String)

    Select Case HotKey
        Case "M"
            ' Handle Alt-M
        Case "X"
            ' Handle Alt-X
    End Select

End Sub
 
That is weird and twisty. WHAT is going to call Public Sub HandleHotKeyM()???? A hot-key? In which case, is that not exactly the same as the assigned procedure?

Public Sub HandleHotKeyM()
Call HandleAnyHotKey("M")
End Sub

which...ummmm, calls.....Alt-M

Just use Alt-M.

Also, how is passing (HotKey As String) " a constant keyboard value"

What precisely is "constant" about it???

faq219-2884

Gerry
My paintings and sculpture
 
My understanding was that Pavel wanted all Alt-Key processing in a single subroutine, which, I'm sure we'll both agree is not a good idea.

The comment "Handle Alt-M" doesn't mean "Call Alt-M", it means place the code that provides Alt-M functionality at that point.

Pavel - I don't think you really want to structure your code like this but rather break similar code into separate subroutines and call them from individual Alt-Key handlers.
 
Hi and thank you all for writing back.

I think I will have to go with separate subroutines. They will look the same except for a few differences. I don't really like that approach, but I don't think there is a nice way around it - all for the matter any way around it. I was advised to use a parameter when calling the single subroutine, but Word will not allow me to assign a key to a subroutine with a parameter. So that more or less does it, I guess.

Best regards,

Pavel
 
Word will not allow me to assign a key to a subroutine with a parameter"

Correct. And does this not make sense? A Sub with a parameter parses the parameter BEFORE it executes. Parameters are passed into the procedure. A hotkey simply executes the procedure. HOW would you get a parameter?

"My understanding was that Pavel wanted all Alt-Key processing in a single subroutine, which, I'm sure we'll both agree is not a good idea."


It is not a good idea...it is an impossible idea.

faq219-2884

Gerry
My paintings and sculpture
 
Hi Fumei,

actually it is not such a bad idea to be able to assign a procedure with a parameter to a hotkey, provided you can feed the parameter to the procedure in the assignment.

Imagine assigning MySubroutine(A) to Alt+A, MySubroutine(B) to Alt+B, etc. Then inside the sub, you can work with the key that was pressed.

I could actually use it right now :)

Best,
Pavel

 
Pavel - Using an API to check the keyboard state wasn't nearly as difficult as I thought it would be. Assign subroutine HandleShortcutKey to any shortcut key you want to handle.
Code:
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Public Sub HandleShortcutKey()
    
    If (GetKeyState(vbKeyA) < 0) Then
        ' Handle Alt-A
    ElseIf (GetKeyState(vbKeyM) < 0) Then
        ' Handle Alt-M
    ElseIf (GetKeyState(vbKeyZ) < 0) Then
        ' Handle Alt-Z
    End If
    
End Sub
 
Assign subroutine HandleShortcutKey to any shortcut key you want to handle."

Did you actually try this? I did. I copied your code, and assigned Alt-R to it.

What happens? You need to press Alt-R to execute HandleShortcutKey...as THAT is the assigned shortcut. Right?

So...nothing happens, as in your code, there is no "r". Sure, IF you put an R in, like:
Code:
Public Sub HandleShortcutKey()
    
    If (GetKeyState(vbKeyA) < 0) Then
        ' Handle Alt-A
    ElseIf (GetKeyState(vbKeyM) < 0) Then
        ' Handle Alt-M
    ElseIf (GetKeyState(vbKeyZ) < 0) Then
        ' Handle Alt-Z
    ElseIf (GetKeyState(vbKeyR) < 0) Then
        Msgbox "R is pressed."
    End If
    
End Sub

You will get "R is pressed." as THAT is the key pressed...but no other is possible. Even if you make ANOTHER procedure:
Code:
Sub ThisIs_AltA()
   MsgBox "A"
End Sub
and assign THAT Alt-A. Test it. Pressing Alt-A will display:

"A"

OK? So...Alt-A does work....by itself.

Now, change the code a little, so that a "A" should - according to you - "handle" Alt-A. Remember, Alt-A, by itself DOES work. But, also remember you stated: "Assign subroutine HandleShortcutKey to any shortcut key you want to handle." I assigned Alt-R to HandleShortcutKey.
Code:
Public Sub HandleShortcutKey()
    
    If (GetKeyState(vbKeyA) < 0) Then
        ' Handle Alt-A
        Call ThisIs_AltA
    ElseIf (GetKeyState(vbKeyM) < 0) Then
        ' Handle Alt-M
        MsgBox "M?"
    ElseIf (GetKeyState(vbKeyZ) < 0) Then
        ' Handle Alt-Z
        MsgBox "Z?"
    ElseIf (GetKeyState(vbKeyR) < 0) Then
         ' handle Alt-R
        MsgBox "R?"
    End If
End Sub
What happens?

Msgbox "R" is what happens. Again...HOW is it going to get an "A". NO "A" was pressed. The only key pressed was the "R", needed to execute HandleShortcutKey in the first place.

Again, HOW are you getting a parameter? You simply can not. There IS no parameter. You can NOT get an "A", or a "M", or a "Z"...because you pressed "R" (assuming that is what you assigned as a shortcut).

It does NOT matter what shortcut key you assigned, the only value you can get is THAT key. Put another way:
Code:
' shortcut key = Alt-R
Public Sub HandleShortcutKey()
    
    If (GetKeyState(vbKeyA) < 0) Then
        ' Handle Alt-A
        Selection.EndKey Unit:=wdStory

Alt-R executes Sub HandleShortcutKey.

Sub HandleShortcutKey executes, but it will NEVER execute (or "handle") the instructions for Alt-A...even if there IS a procedure with an Alt-A shortcut.

I have a procedure with an Alt-A shortcut. But as I press Alt-R to execute Sub HandleShortcutKey, where is it getting the "A"? Even if you directly Call the procedure, it will not execute. Even if you just have the instructions, it will not execute.

The only value that can be returned is the key you pressed for the shortcut you assign to HandleShortcutKey. You can put as many ElseIf as you want (the whole alphabet if you want), the only one that will do anything is...the key you pressed as the shortcut key to execute HandleShortcutKey.

Unless I am missing something drastic I simply can not see how this will work.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top