How to provide shortcut keys for controls like text box,button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sujathaeeshan
    New Member
    • Feb 2008
    • 22

    #1

    How to provide shortcut keys for controls like text box,button

    Hi all,

    I need to provide shortcut keys for controls like text box and buttons etc...
    That is i dont want to go for tabspace or mouse click for perticular controls..
    By having shortcut keys i need to go for specifeid controls(textbo x,button)...

    Is there any way to provide shotcut keys for controls.....or built in property values.....


    could any anoe help me out of this query...
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    For Buttons You can Change the Caption:

    &Save

    So, cmdsave_Click will be fired, when you press "Alt + S" on the form..

    For TextBoxes, you have to code..

    REgards
    Veena

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      If you want the textbox or any control to be on focus, with an appropriate shortcut key and keypress event

      [code=vb]
      <ControlName>.S etFocus
      [/code]

      Comment

      • mafaisal
        New Member
        • Sep 2007
        • 142

        #4
        Hello

        Firstly Set Forms Property Keypreview True
        Then Set All Controls in the Form have regular TAbIndex

        Then in Form keydown event
        Code:
        Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
         sendkeys vbtab
        End Sub
        Originally posted by sujathaeeshan
        Hi all,

        I need to provide shortcut keys for controls like text box and buttons etc...
        That is i dont want to go for tabspace or mouse click for perticular controls..
        By having shortcut keys i need to go for specifeid controls(textbo x,button)...

        Is there any way to provide shotcut keys for controls.....or built in property values.....


        could any anoe help me out of this query...

        Comment

        • olseni
          New Member
          • Jan 2008
          • 19

          #5
          Hi all,
          Sorry to "butt in" on this, but I have the same issue, and the answers so far does not seem to do the trick, or maybe I am doing it wrong....

          If I have a form with a button called Open Details which opens another form. If I wish to use a shortcut key (instead of clicking the button with the mouse), e.g. Alt+O, how do I do that ? Is there functionality, or code to be used ?

          Thanks
          Best regards
          Ingrid

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by olseni
            Hi all,
            Sorry to "butt in" on this, but I have the same issue, and the answers so far does not seem to do the trick, or maybe I am doing it wrong....

            If I have a form with a button called Open Details which opens another form. If I wish to use a shortcut key (instead of clicking the button with the mouse), e.g. Alt+O, how do I do that ? Is there functionality, or code to be used ?

            Thanks
            Best regards
            Ingrid
            Depending on the version you can use Veena's solution, but lotus' one is more general, add in the form's keypress event the code to call the click event's sub of the button, for example, something like this in te form keydown button will do:

            [CODE=vb]If KeyCode = Asc("O") And Shift = 2 Then CommandButton1_ Click[/CODE]

            add it to the controls that can be selected, so when they're selected and you press Ctrl+O the command1_click sub will be called. It'll be some copypaste work if you have many commands, but is the simplest way for few.

            Note 2 is the value of Ctrl. check the help file or the object browser to see the shift or alt values, you can also use vb constants.

            HTH

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Both people who have the problem, please tell us what version of VB you're using.

              Comment

              • debasisdas
                Recognized Expert Expert
                • Dec 2006
                • 8119

                #8
                If using VB 6.0 then it is quite easy to set focus for text boxes.

                1.Use a label control for the labeling of the textbox .
                2.UseMnemonic should be set to true for the label (default).
                3.Use & to assign the Mnemonic.
                4.Set the tab index of the textbox to one more than the corresponding label.

                When you try set the focus on the label using the Mnemonic character and since label is a static control so could not receive the focus , focus will be set to next control in higher tab index order.
                Last edited by Killer42; Apr 11 '08, 06:33 AM. Reason: Just corrected "quiet" (to "quite") and "lable".

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Thanks Debasis. Exactly what I would have said, if I'd had time. :)

                  Comment

                  • sujathaeeshan
                    New Member
                    • Feb 2008
                    • 22

                    #10
                    Hi,

                    Just provide the &Open Details at the text property of the button.
                    while running the form if u fired Alt+O ,it automatically fires the function of button(open details)-- which opens another form.

                    Regards
                    suji

                    Comment

                    • olseni
                      New Member
                      • Jan 2008
                      • 19

                      #11
                      Hi all,
                      thanks a lot for the many replies, and sorry for not being in here for so long, however now I am back and can reply to some of the suggestions and questions.

                      I am using VB 6,5 with Access 2007.

                      Thanks HTH for your suggestion code, I just tried it out, but it doesnt seem to work, or I am doing it wrong. I used the code you provided and set it into the OnKeyDown property of the Form as

                      Private Sub Form_KeyDown(Ke yCode As Integer, Shift As Integer)
                      If KeyCode = Asc("O") And Shift = 2 Then Command142_Clic k
                      End Sub

                      Command142 being the button I want to open. But when I press Ctrl+O nothing happens..... what am I doing wrong ?

                      Suji, I am not quite sure I understand your suggestion, could you please elaborate ?

                      I hope you can still help me....
                      Thanks in advance
                      Best regards
                      Ingrid

                      Comment

                      • kadghar
                        Recognized Expert Top Contributor
                        • Apr 2007
                        • 1302

                        #12
                        Originally posted by olseni

                        Thanks HTH for your suggestion code, I just tried it out, but it doesnt seem to work, or I am doing it wrong. I used the code you provided and set it into the OnKeyDown property of the Form as

                        Private Sub Form_KeyDown(Ke yCode As Integer, Shift As Integer)
                        If KeyCode = Asc("O") And Shift = 2 Then Command142_Clic k
                        End Sub

                        Command142 being the button I want to open. But when I press Ctrl+O nothing happens..... what am I doing wrong ?
                        HTH = hope that helps
                        Call me Kad

                        Its because as your form has many controls, when you initilialize the form, what is selected is not the form, but another control. So for Ctrl-O to work it has to be in the KeyDown event of the selected control. (so you can write it on every control key down event)

                        HTH (again ^.^)

                        Comment

                        • olseni
                          New Member
                          • Jan 2008
                          • 19

                          #13
                          Hi Kad,
                          Sorry about the HTH :-)
                          Anyway I tried it and it works when I am already placed on the command, however is there a way of using shortcut keys to activate a command, or go to a specific field without being placed on the command already ?

                          My reason for wanting this function is that I will use it as shortcuts for a user, being anywhere in the form, and instead of using the mouse, or the tabs to "get there" I would want to use a shortcutkey.... sorry if I explained my issue wrong earlier....

                          Thanks in advance
                          Best regards
                          Ingrid

                          Comment

                          • olseni
                            New Member
                            • Jan 2008
                            • 19

                            #14
                            Close of this issue since solution has been provided

                            Comment

                            • smartchap
                              New Member
                              • Dec 2007
                              • 236

                              #15
                              I think this is the final solution and will be beneficial for everyone:
                              Use the code below:

                              [CODE=vb]
                              Dim A As Integer
                              Dim B As Integer

                              Sub SetControlFocus (KeyCode As Integer, Shift As Integer)
                              If KeyCode = Asc("A") And Shift = 2 Then Command1_Click
                              If KeyCode = Asc("B") And Shift = 2 Then Command2_Click
                              If KeyCode = Asc("D") And Shift = 2 Then Text1.SetFocus
                              If KeyCode = Asc("E") And Shift = 2 Then Text2.SetFocus
                              If KeyCode = Asc("F") And Shift = 2 Then Text3.SetFocus
                              If KeyCode = Asc("G") And Shift = 2 Then Option1.SetFocu s

                              'MsgBox "OK"
                              End Sub

                              Private Sub Command1_Click( )
                              MsgBox "Clicked Command Button 1"
                              End Sub

                              Private Sub Command2_Click( )
                              MsgBox "Clicked Command Button 2"
                              End Sub

                              Private Sub Option1_KeyDown (KeyCode As Integer, Shift As Integer)
                              A = KeyCode: B = Shift
                              If B <> 0 Then
                              If B = 2 And A <> 17 Then
                              Call SetControlFocus (A, B)
                              End If
                              End If
                              End Sub
                              [/CODE]
                              Write the code in Option1_KeyDown event in every control's key down event and the results will be as required. If satisfied please post reply.
                              Last edited by Killer42; Apr 27 '08, 02:27 PM. Reason: Added CODE=vb tag

                              Comment

                              Working...