Short Key in a ToolBar 
Author Message
 Short Key in a ToolBar

Hi People,

I would like to know how can I put short keys in a toolbar ? If its
possible, somebody tell me please ! Thanks a lot.

Leonardo inojoza


Leonardo A. Inojoza
Analista de sistemas

Mon, 12 Jan 2004 01:35:07 GMT  
 Short Key in a ToolBar

The toolbar control in VB doesn't have built-in support for shortcut key.
To obtain the function, you need to do it by yourself. As far as I know,
there are three methods.

1. Define shortkeys through menu.

You can create a menu, define a shortcut key for it. In the event handler
for the menu, call the code for your toolbar buttons.

2. If the short key starts with Alt + ..., you use create some dummy cmmand
buttons and call your code when they are pressed. Below is some sample code:

Option Explicit

Private Sub Command1_Click()
End Sub

Private Sub Command2_Click()
End Sub

Private Sub Command3_Click()
End Sub

Private Sub Form_Load()
    Dim ButtonX As Button
    Dim intS As Integer
    intS = 400

    Command1.Width = intS
    Command1.Height = intS
    Command1.Caption = "&E"
    Command1.Move 0, 0
    Command1.ZOrder 1

    Command2.Width = intS
    Command2.Height = intS
    Command2.Caption = "&P"
    Command2.Move 0, 0
    Command2.ZOrder 1

    Command3.Width = intS
    Command3.Height = intS
    Command3.Caption = "&M"
    Command3.Move 0, 0
    Command3.ZOrder 1

    Set ButtonX = Toolbar1.Buttons.Add(1, , "&End", , tbrDefault)
    Set ButtonX = Toolbar1.Buttons.Add(2, , "&Print", , tbrDefault)
    Set ButtonX = Toolbar1.Buttons.Add(3, , "&Message", , tbrDefault)
End Sub

Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
    Select Case Button.Index
        Case 1
        Case 2
        Case 3
        Case Else
    End Select
End Sub

Public Sub ToolBarEnd()
    MsgBox "Pressed End"
End Sub

Public Sub ToolBarPrint()
    MsgBox "Pressed Print"
End Sub

Public Sub ToolBarMessage()
    MsgBox "Pressed Message"
End Sub

3. Use Win API to register a hotkey. This method is a little complex, but
provides more flexibility. Below are some sample code and detailed steps.

(We will define a shortcut key Ctrl+ T in the example below)

a.) Open your VB project and add a standard module (e.g. Module1)
b.) Add the following code to the Declarations section of Module1:

Option Explicit

Declare Function CallWindowProc Lib "user32" Alias _
    "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
    ByVal hwnd As Long, ByVal Msg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long

Declare Function SetWindowLong Lib "user32" Alias _
    "SetWindowLongA" (ByVal hwnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Declare Function RegisterHotKey Lib "user32" _
    (ByVal hwnd As Long, ByVal id As Long, _
    ByVal fsModifiers As Long, ByVal vk As Long) As Long

Public Declare Function UnregisterHotKey Lib "user32" _
    (ByVal hwnd As Long, ByVal id As Long) As Long

Public Const GWL_WNDPROC = -4
Public IsHooked As Boolean
Global lpPrevWndProc As Long
Global gHW As Long

Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4

Public Const HOTKEY_CTRL_T = 100

Public Sub Hook()

    If IsHooked Then
        MsgBox "Don't hook it twice without " & _
          "unhooking, or you will be unable to unhook it."
        lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
        AddressOf WindowProc)
        IsHooked = True
    End If

End Sub

Public Sub Unhook()

    Dim temp As Long
    temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
    IsHooked = False

End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As _
    Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Debug.Print "Message: "; hw, uMsg, wParam, lParam
    WindowProc = CallWindowProc(lpPrevWndProc, hw, _
        uMsg, wParam, lParam)

    If uMsg = WM_HOTKEY Then
        'handle shortcut key here
        Select Case wParam
            Case HOTKEY_CTRL_T:

                'Call your procedure that handles your toolbar button click
                MsgBox "Hotkey (Ctrl+T) is triggered."

        End Select

    End If

End Function

c.) In your form module that contains toolbar, add code that call the
decared API to hook windows message and registrer hotkey:

Private Sub Form_Load()

    Dim lResult As Long

    'Your other code here

    'Register hotkey message
    lResult = RegisterHotKey(Me.hwnd, HOTKEY_CTRL_T, MOD_CONTROL, vbKeyT)
    If Not lResult = 1 Then
        'Register hotkey failed, possibly the HOTKEY_CTRL_T already exists
        MsgBox "Register hotkey failed"
    End If

    'Hook message
    gHW = Me.hwnd

End Sub

Private Sub Form_Unload(Cancel As Integer)

    UnregisterHotKey gHW, HOTKEY_CTRL_T

    'Unhook before terminating
    If IsHooked Then
    End If

End Sub

Mon, 12 Jan 2004 14:44:43 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Create Outlook toolbar & short-cuts

2. adding short keys to buttons

3. short-cut key

4. assign macro to button or short-key

5. Print Macros and Short Keys on Tool Bars

6. creating short-cut keys in form fields

7. Key Board Short Cut For Selection

8. Is there a short-Key for Step Into?

9. Menu Short-Cut-Key Problems

10. short cut keys VB4

11. Short Cut Key Combination

12. Menu and short cut key


Powered by phpBB® Forum Software