Results 1 to 3 of 3

Thread: VBA Dynamic Keybinding

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    31

    Smile VBA Dynamic Keybinding

    Hi All,

    I am using my own defined styles for my developing tool. The user will apply these styles to document using a seperate form. I want to add keybindings for all my styles. Using this shortcut keys combinations the user will apply the style easily. For this I have wrote keybindings for all the styles. But I want to assign the shortcuts and their corresponding event handler during my runtime. is it possible? Please help.

    Thanks,
    Senthil. S

  2. #2
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: VBA Dynamic Keybinding

    Yes you can do that. Here's a simple example to bind a style to CTRL+1.

    Code:
    KeyBindings.Add KeyCode:=BuildKeyCode(wdKey1, wdKeyControl), _
         KeyCategory:=wdKeyCategoryStyle, Command:="Style Name Here"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    31

    Re: VBA Dynamic Keybinding

    Hi dmaruca,

    KeyBindings.Add KeyCode:=BuildKeyCode(wdKey1, wdKeyControl), _
    KeyCategory:=wdKeyCategoryStyle, Command:="Style Name Here"

    I have also used the above concept only. But I have stored in database the stylename ans styleshortcuts. I have also given the user a interface to change the stylename and their corresponding shortcuts whenever he needed. When the user changes the stylename, shortcuts it doesn't matter, we can passes it thorugh argument to the general procedure but the command is the problem. How it is possible to assign different procedures for all stylenames??? Below is the code for your persual.

    Public Sub DynamicKeyBind()
    Dim lCtl As Long
    Dim lAlt As Long
    Dim lShift As Long
    Dim lKey As Long
    Dim sSql As String
    Dim sShort As String
    Dim sShortKey As String, aShortKey() As String
    Dim sShortVal As String, aShortVal() As String

    sShortKey = "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
    sShortVal = "65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|"
    aShortKey = Split(sShortKey, "|")
    aShortVal = Split(sShortVal, "|")

    modConnection.Open_Connection

    Set Rs = New ADODB.Recordset
    sSql = "select StyleName,ShortcutKey from Mst_Styles where ShortcutKey<>''"
    Rs.Open sSql, modConnection.Conn, adOpenStatic, adLockReadOnly
    Do While Not Rs.EOF
    sStyleKey = Rs(0)
    sShort = Rs(1)
    For i = 0 To UBound(aShortKey)
    If LCase(Right(sShort, 1)) = aShortKey(i) Then
    lKey = CLng(aShortVal(i))
    End If
    Next i
    'Control+Shift+Alt combination
    If InStr(1, sShort, "ctrl", vbTextCompare) > 0 And InStr(1, sShort, "shift", vbTextCompare) > 0 And InStr(1, sShort, "alt", vbTextCompare) > 0 Then
    lCtl = wdKeyControl
    lAlt = wdKeyAlt
    lShift = wdKeyShift
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="DynaKeyBndEvntHandler", KeyCode:=BuildKeyCode(lCtl, lAlt, lShift, lKey)
    'Control+Shift combination
    ElseIf InStr(1, sShort, "ctrl", vbTextCompare) > 0 And InStr(1, sShort, "shift", vbTextCompare) > 0 Then
    lCtl = wdKeyControl
    lShift = wdKeyShift
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="DynaKeyBndEvntHandler", KeyCode:=BuildKeyCode(lCtl, lShift, lKey)
    'Control+Alt combination
    ElseIf InStr(1, sShort, "ctrl", vbTextCompare) > 0 And InStr(1, sShort, "alt", vbTextCompare) > 0 Then
    lCtl = wdKeyControl
    lAlt = wdKeyAlt
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="DynaKeyBndEvntHandler", KeyCode:=BuildKeyCode(lCtl, lAlt, lKey)
    'Control combination
    ElseIf InStr(1, sShort, "ctrl", vbTextCompare) > 0 Then
    lCtl = wdKeyControl
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="DynaKeyBndEvntHandler", KeyCode:=BuildKeyCode(lCtl, lKey)
    'Alt combination
    ElseIf InStr(1, sShort, "alt", vbTextCompare) > 0 Then
    lAlt = wdKeyAlt
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="DynaKeyBndEvntHandler", KeyCode:=BuildKeyCode(lAlt, lKey)
    'Shift combination
    ElseIf InStr(1, sShort, "shift", vbTextCompare) > 0 Then
    lShift = wdKeyShift
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="DynaKeyBndEvntHandler", KeyCode:=BuildKeyCode(lShift, lKey)
    End If
    Rs.MoveNext
    Loop


    End Sub

    Public Sub DynaKeyBndEvntHandler()
    Dim oNode As Object

    If bFrmStylesLoaded = True Then
    For Each oNode In frmStyles.trvBack.Nodes
    If oNode.Text = sAStyle Then
    modStyling.StyleSelection CInt(Replace(oNode.Key, "key", ""))
    Exit For
    End If
    Next oNode
    End If

    End Sub

    Here using the above method I can able to bind all the keys at runtime. But after binding while pressing any assigned shortcut the control will directly to DynaKeyBndEvntHandler procedure. Here I dont know which shortcut key as pressed to assign the style name.

    regards,
    Senthil. S

Posting Permissions

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



Click Here to Expand Forum to Full Width