Results 1 to 8 of 8

Thread: How can i detect what key has been pressed on a form that has command buttons on it?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    3

    How can i detect what key has been pressed on a form that has command buttons on it?

    How can i detect what key has been pressed on a form that has command
    buttons on it? I know that using the Form_KeyDown event with the
    Form.KeyPreview property et to True works when there are no buttons on
    the form, but i have buttons on the form and when i try to detect
    events like pressing the arrow keys, the program doe...s not respond to
    the events but instead just shifts focus from button to button using
    the arrow keys.
    When the focus is on the command button it detects most of the keyboard inputs except for the arrow keys. Help me, please!!
    Link:
    http://www.mediafire.com/file/47jfve...entKeyDown.rar
    src:
    Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
    'SendKeys (vbKeyShift)
    If KeyCode = 40 Then
    MsgBox ("Key DOWN")
    End If
    If KeyCode = 38 Then
    MsgBox ("Key UP")
    End If
    End Sub
    Private Sub Form_Load()
    KeyPreview = True
    End Sub

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: How can i detect what key has been pressed on a form that has command buttons on

    Hi quangvinh777... Welcome to the forums...

    You must write the code in the Form's KeyDown event !

    I think, a workaround for the focus problem of command button is to set focus to some other control other than buttons (eg: TextBox).
    Maybe, someone will post a better solution
    Code:
    Option Explicit
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = 40 Then
            MsgBox ("Key DOWN")
        End If
        
        If KeyCode = 38 Then
            MsgBox ("Key UP")
        End If
    End Sub
    
    Private Sub Form_Load()
        KeyPreview = True
        
        Text1.TabIndex = 0  '~~~ You have to create a Textbox and set TabIndex of it as first one.
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: How can i detect what key has been pressed on a form that has command buttons on

    I don't think you can trap the arrow keys (or the tab key) using native VB. Maybe if you subclassed?

    My standard workaround is to rethink my GUI design. If I really absolutely must implement custom arrow navigation, I use the number pad. (Have "4" do the desired left arrow action, "8" does up, et cetera...)

    EDIT: akhileshbc, pictureboxes are better for hiding focus. They have the advantage of not displaying a blinking caret.

    EDIT2: I just had a thought: if you set every control's TabStop property to False, you might be able to trap arrow keys manually in the Key events since they wouldn't be initiating any navigation. You'd then need to manually handle tabbing, but at least it's worth a shot.
    Last edited by Ellis Dee; Oct 20th, 2010 at 11:37 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: How can i detect what key has been pressed on a form that has command buttons on

    @akhileshbc, @Ellis Dee: Thanks!
    ex:
    vb Code:
    1. Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  
    3.        If KeyCode = 40 Then
    4.             Command2_click
    5.        End If
    6.  
    7. End Sub
    8. Private Sub Command2_KeyDown(KeyCode As Integer, Shift As Integer)
    9.  
    10.       If KeyCode = 38 Then
    11.            Command1_click
    12.       End If
    13.       If KeyCode = 40 Then
    14.            Command3_click
    15.       End IF
    16.  
    17. End Sub
    ...
    How can i do?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How can i detect what key has been pressed on a form that has command buttons on

    How can you do what?

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: How can i detect what key has been pressed on a form that has command buttons on

    what should I do to fulfill the request above?

  7. #7
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: How can i detect what key has been pressed on a form that has command buttons on

    Quote Originally Posted by Ellis Dee View Post
    EDIT: akhileshbc, pictureboxes are better for hiding focus. They have the advantage of not displaying a blinking caret.

    EDIT2: I just had a thought: if you set every control's TabStop property to False, you might be able to trap arrow keys manually in the Key events since they wouldn't be initiating any navigation. You'd then need to manually handle tabbing, but at least it's worth a shot.
    Great idea, Ellis

    I was thinking about hiding that textbox to somewhere else, outside the user's focus. Eg: Text1.Left=-9999

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  8. #8
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: How can i detect what key has been pressed on a form that has command buttons on

    If you've set KeyPreview = True on the form, and you toggle NumLock on the keyboard keypad you should be able to trap any keystroke, including CTRL and ALT in KeyDown and/or KeyPress.

    You may have to TELL your user to toggle NumLock, unless you want to force that on them. And of course, you'll have to tell them to use the Arrow Keys from the KEYPAD.

    I'm new here, I hope I haven't misunderstood the problem.

    Good luck.

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