Announcement

Collapse
No announcement yet.

Multiple Hot Keys

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Hot Keys

    Hi All!

    I have spent the evening (last 4 hours, much to my wife's
    annoyance), searching the forum archives for a simple code
    snippet that illustrates the use of multiple hot keys that
    are active in a single dialog at one time.

    Any help would be much appreciated.

    Regards,

    Jim...

    ------------------

  • #2
    From PBForms help file:
    The ampersand (&) character in a Caption of a menu member makes the character that follows it into a mnemonic or hot-key.
    Do you want two hot keys to the same item?

    ------------------
    Glossary under accelerator
    A keystroke or combination of keystrokes that acts as a hot-key or shortcut to perform some programmer-defined operation. When an accelerator is used, it usually results in a %WM_COMMAND or %WM_SYSCOMMAND message being sent to the dialog Callback Function. There are two forms of accelerator: command accelerators (such as indicated by an underscored character on control labels and menu items, for example "File"), and a keyboard accelerators, which are separately configurable through an accelerator table, such as the ACCEL ATTACH statement.


    [This message has been edited by Mike Doty (edited November 17, 2006).]

    Comment


    • #3
      Search for ACCEL in source code or ACCEL ATTACH in help?

      ------------------

      Comment


      • #4
        Jim,

        Here's the function and some demo code showing you how to do it.
        For particulars you can look things up in the help file.

        Code:
        FUNCTION ASSIGNACCEL ALIAS "ASSIGNACCEL" (      tAccel  As ACCELAPI, _
                                                  BYVAL wKey    As Word    , _
                                                  BYVAL wCmd    As Word    , _
                                                  BYVAL byFVirt As Byte      ) EXPORT AS LONG
         
          tAccel.fVirt = byFVirt
          tAccel.key   = wKey
          tAccel.cmd   = wCmd
         
        END FUNCTION
         
        'This code goes into the SUB/FUNCTION that creates the control(s).
        
          DIM tA(1) AS LOCAL ACCELAPI
          AssignAccel tA(0), %VK_X  , %BTN_Exit, %FVIRTKEY OR %FALT
          AssignAccel tA(1), %VK_F10, %BTN_Exit, %FVIRTKEY OR %FNOINVERT
          Accel Attach hMain, tA() TO X
        ------------------
        C'ya
        Don
        don at DASoftVSS dot com
        http://www.DASoftVSS.com
        --
        The perfect American sentence:
        "Like, absolutely awesome!"
        C'ya
        Don

        http://www.ImagesBy.me

        Comment


        • #5
          That's like, absolutely awesome gentlemen!
          Thank you very much!
          I need to enable all of the function keys but
          I don't want a string of button controls all
          over the dialog box. So I just gave them a state
          of "hidden" and used your code above and presto!,
          I can trap all of those keys presses, even if
          the operator is typing into a text box at the time.
          Thank you for your help, both of you.
          I am now back on track!

          Jim...

          ------------------

          Comment


          • #6
            Ha!
            I just realized after reading more on this...
            I don't have to create the button and hide it,
            just define the code to be sent back to the
            callback function.
            Even MORE awesome! )

            ------------------

            Comment


            • #7
              Just a few more "Hot" Keys..
              Code:
              #Compile Exe
              #Dim All
              #Include "WIN32API.INC"
               
              %TXT_Edit1   = 1001
              %LBL_Label   = 1002
              %BTN_AltN    = 1003
              %HK_F5        = 1103 '*
              %HK_AltF5     = 1104 '* - Identifiers for 'Hot' Keys
              %HK_AltShftF5 = 1105 '*
              %HK_CtrlF5    = 1106 '*
              '------------------/
               
              CallBack Function DlgProc()
              Local hFoc As Dword, ID_Control As Long, sTemp As String
                Select Case As Long CbMsg
                  Case %WM_INITDIALOG
               
                  Case %WM_COMMAND
                    Select Case As Long CbCtl
                      Case %HK_F5
                        Control Set Text CbHndl, %LBL_Label, "HotKey: "+"F5"
                      Case %HK_AltF5
                        Control Set Text CbHndl, %LBL_Label, "HotKey: "+"Alt-F5"
                      Case %HK_AltShftF5
                        Control Set Text CbHndl, %LBL_Label, "HotKey: "+"Alt-Shft-F5
                      Case %HK_CtrlF5
                        Control Set Text CbHndl, %LBL_Label, "HotKey: "+"Control-F5
                    'These don't need accelerator table entries!
                      Case %IDOK
                        Control Set Text CbHndl, %LBL_Label, "HotKey: "+"Enter"
                      Case %IDCANCEL
                        Control Set Text CbHndl, %LBL_Label, "HotKey: "+"Escape" 
                      Case %BTN_AltN
                        If CbCtlMsg = %BN_CLICKED Or CbCtlMsg = 1 Then
                          Control Set Text CbHndl, %LBL_Label, "HotKey: "+"Alt-N"
                        End If
                    End Select
                  Case %WM_HELP
                    Control Set Text CbHndl, %LBL_Label, "HotKey: "+"F1"
                  Case %WM_SYSCOMMAND
                    If CbWParam = %SC_KEYMENU Then 
                      Control Set Text CbHndl, %LBL_Label, "HotKey: "+"F10"
                      Function = 1
                      Exit Function
                    End If
                End Select
              End Function 
              '------------------/DlgProc
               
              Function PBMain()
                Local hDlg, hResult As Dword 
                Dim AccelTbl(3) As ACCELAPI           ' dim array of 'ACCELAPI' type 
               
                  AccelTbl(0).FVIRT   = %FVIRTKEY     ' flag indicates key member specifies a virtual-key code
                  AccelTbl(0).KEY     = %VK_F5        ' F5 key (could be ASCII Char code, see FVIRT)
                  AccelTbl(0).CMD     = %HK_F5        ' accelerator ID for %WM_COMMAND / CBCTL {LOWRD(WPARAM)} msg
               
                  AccelTbl(1).FVIRT   = %FVIRTKEY Or %FALT
                  AccelTbl(1).KEY     = %VK_F5 
                  AccelTbl(1).CMD     = %HK_AltF5
               
                  AccelTbl(2).FVIRT   = %FVIRTKEY Or %FALT OR %FSHIFT
                  AccelTbl(2).KEY     = %VK_F5 
                  AccelTbl(2).CMD     = %HK_AltShftF5 
               
                  AccelTbl(3).FVIRT   = %FVIRTKEY Or %FCONTROL
                  AccelTbl(3).KEY     = %VK_F5 
                  AccelTbl(3).CMD     = %HK_CtrlF5 
               
                Dialog New 0, "Misc. Hot Keys", , , 135, 165, %WS_OVERLAPPEDWINDOW, To hDlg
                Control Add TextBox, hDlg, %TXT_Edit1, "With cusor here, press Enter", 15, 5, 100, 13
                Control Add Button,  hDlg, %BTN_AltN,  "Alt-&N", 65, 40, 50, 15
                Control Add Label,   hDlg, %LBL_Label, "Label1", 15, 25, 100, 10
                Control Add Label,   hDlg, 102, "'Hot' / Accel Keys:", 15, 60, 100, 10
                Control Add Label,   hDlg, 103, "F5", 35, 70, 70, 10
                Control Add Label,   hDlg, 104, "Alt-F5", 35, 80, 70, 10
                Control Add Label,   hDlg, 105, "Alt-Shft-F5", 35, 90, 70, 10
                Control Add Label,   hDlg, 106, "Ctrl-F5", 35, 100, 70, 10
                Control Add Label,   hDlg, 106, "Enter", 35, 110, 70, 10
                Control Add Label,   hDlg, 106, "Escape", 35, 120, 70, 10
                Control Add Label,   hDlg, 107, "F1", 35, 130, 70, 10
                Control Add Label,   hDlg, 108, "F10", 35, 140, 70, 10
                Control Add Label,   hDlg, 109, "Alt-N", 35, 150, 70, 10
               
                Accel Attach hDlg, AccelTbl() To hResult
               
                Dialog Show Modal hDlg, Call DlgProc
              End Function 
              '------------------/PbMain
              Rgds Dave


              ------------------
              Rgds, Dave

              Comment


              • #8
                Great example.
                I didn't know about the keys that don't need to be defined.
                That is very helpful.
                Thank you!
                Jim...

                ------------------

                Comment

                Working...
                X