By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,471 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,471 IT Pros & Developers. It's quick & easy.

Help with KeyDown Event

100+
P: 365
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
Mar 2 '10 #1
Share this Question
Share on Google+
7 Replies


ADezii
Expert 5K+
P: 8,597
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:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. Select Case KeyCode
    3.   Case vbKeyRight
    4.     SendKeys "%F"
    5.   Case vbKeyLeft
    6.     SendKeys "%B"
    7.   Case Else
    8.     'Do nothing
    9. End Select
    10. End Sub
  5. This should enable the specific Command Button to retain Focus.
Mar 2 '10 #2

100+
P: 365
Sendkeys works, i was calling the on click event in the same way.


Genious as ever Cheers

Dan
Mar 2 '10 #3

100+
P: 365
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)
Mar 2 '10 #4

ADezii
Expert 5K+
P: 8,597
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.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. Dim cbo As ComboBox
    3.  
    4. Set cbo = Me![cboTest]
    5.  
    6. If cbo.ListCount = 0 Then Exit Sub      'Combo not populated
    7.  
    8. With cbo
    9.   Select Case KeyCode
    10.     Case vbKeyDown
    11.       KeyCode = 0
    12.       'A ListIndex of -1 means that no Item is selected in the Combo Box. The
    13.       'Combo Box itself may/may not be selected, so let's be sure by directing
    14.       'Focus to it
    15.       cbo.SetFocus
    16.         If .ListIndex = .ListCount - 1 Then         'Last Item in Combo
    17.           'Move back to the first? It's up to you.
    18.           .ListIndex = 0       '1st Item
    19.         Else
    20.           'Move Down one Item
    21.           .ListIndex = .ListIndex + 1
    22.         End If
    23.     Case vbKeyUp
    24.       KeyCode = 0
    25.       'for you to fill in
    26.     Case vbKeyLeft
    27.       KeyCode = 0
    28.     Case vbKeyRight
    29.       KeyCode = 0
    30.     Case Else
    31.       'Do nothing
    32.   End Select
    33. End With
    34. 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).
Mar 2 '10 #5

100+
P: 365
Thanks ADezii, the Keycode = 0 bit was the most helpfull in my situation.
Mar 3 '10 #6

P: 15
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
Apr 26 '10 #7

ADezii
Expert 5K+
P: 8,597
@Annie Bender
Your are quite welcome, Annie. Glad it benefitted you also.
Apr 26 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.