Help with KeyDown Event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan2kx
    Contributor
    • Oct 2007
    • 365

    Help with KeyDown Event

    Hello peeps,

    bit of a trivial question really...

    I have a form with 2 buttons, a left scroll and a right which moves the form through dates.

    i want to use the keyboard arrow buttons to "press" these buttons which all works fine so far..

    My problem is that i want the focus to remain on these buttons (which ever was "pressed") but every other "press" moves the focus to the next form control and then back on the next control...

    I want to avoid this...

    I have tried to set the focus again to the button in question and have also tried to first set the focus to the opposite button to then be set again to the button i want but it still seems to only work alternatively.. .. any suggestions?

    cheers fellas (and fellettes)

    Dan
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Let's assume that you have two Command Buttons named cmdForward and cmdBackward that you wish to also activate through the Arrow Keys (Left and Right):
    1. Set the Caption Property of the Forward Key to &Forward (Shortcut Key F).
    2. Set the Caption Property of the Backward Key to &Backward (Shortcut Key B).
    3. Set the KeyPreview Property of the Form to True/Yes.
    4. Place the following code in the KeyDown() Event of your Form:
      Code:
      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      Select Case KeyCode
        Case vbKeyRight
          SendKeys "%F"
        Case vbKeyLeft
          SendKeys "%B"
        Case Else
          'Do nothing
      End Select
      End Sub
    5. This should enable the specific Command Button to retain Focus.

    Comment

    • Dan2kx
      Contributor
      • Oct 2007
      • 365

      #3
      Sendkeys works, i was calling the on click event in the same way.


      Genious as ever Cheers

      Dan

      Comment

      • Dan2kx
        Contributor
        • Oct 2007
        • 365

        #4
        OK.. round two...

        What about using the up and down keys to cycle through a combo box, no caption box?

        EDIT: i dont want the focus to remain on the combo box in this instance...Just want to stop the form scrolling through controls (up/down)

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          I'm literally running out the door, so here goes some thrown-together code that will cycle through entries Downward in a Combo Box. You may have to modify it, and I didn't have time to complete KeyUp, but here it goes with two Assumptions:
          1. The KeyPreview Property of the Form is set to True/Yes.
          2. Combo Box Name is cboTest.
            Code:
            Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
            Dim cbo As ComboBox
            
            Set cbo = Me![cboTest]
            
            If cbo.ListCount = 0 Then Exit Sub      'Combo not populated
            
            With cbo
              Select Case KeyCode
                Case vbKeyDown
                  KeyCode = 0
                  'A ListIndex of -1 means that no Item is selected in the Combo Box. The
                  'Combo Box itself may/may not be selected, so let's be sure by directing
                  'Focus to it
                  cbo.SetFocus
                    If .ListIndex = .ListCount - 1 Then         'Last Item in Combo
                      'Move back to the first? It's up to you.
                      .ListIndex = 0       '1st Item
                    Else
                      'Move Down one Item
                      .ListIndex = .ListIndex + 1
                    End If
                Case vbKeyUp
                  KeyCode = 0
                  'for you to fill in
                Case vbKeyLeft
                  KeyCode = 0
                Case vbKeyRight
                  KeyCode = 0
                Case Else
                  'Do nothing
              End Select
            End With
            End Sub
          3. As far as the Arrow Key Movement through Fields, the code also Demos how to Cancel that effect by setting KeyCode = 0 for the Arrow Keys (Lines 11, 24, 27, and 29).

          Comment

          • Dan2kx
            Contributor
            • Oct 2007
            • 365

            #6
            Thanks ADezii, the Keycode = 0 bit was the most helpfull in my situation.

            Comment

            • Annie Bender
              New Member
              • Feb 2010
              • 15

              #7
              I know this is an old thread, but I came across it and wanted to let ADezii know that his KeyCode=0 bit solved a problem for me too. Not altogether sure I know why that works, but it does what is needed. Thanks.

              Annie

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by Annie Bender
                I know this is an old thread, but I came across it and wanted to let ADezii know that his KeyCode=0 bit solved a problem for me too. Not altogether sure I know why that works, but it does what is needed. Thanks.

                Annie
                Your are quite welcome, Annie. Glad it benefitted you also.

                Comment

                Working...