Results 1 to 10 of 10

Thread: last key pressed - Easy?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    111

    Smile

    Does anyone know how to detect the last key that was pressed in a LostFocus() event for a button. The button is at the bottom of my form, and I would like for the fist field to receive focus if they pressed the tab key. I read this in a post, but they didn't give the code.

    Thanks.

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Why do you need to detect the last key pressed? If you set the Tabindex of the first control to 0, and the last control has the last index, then it will go back to 0.
    <removed by admin>

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    111

    I can't

    The form actually has tabs on it. So the two controls are on the same tab. Since there is only one tab order, I can not do that.

    Thanks for the suggestion though.

    Does anyone know how to do this?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    111

    Please Help!

    This is all I have left to complete my project. Is there another approach that I am not thinking of? I just need to capture when tab is hit from a button.

    Thanks.

  5. #5
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    That's not what I meant. Each control, no matter if you have tabs on your program or not, have a tab index. But, if you want to do it your way, here is some code.
    Code:
    Private Sub Text2_KeyPress(KeyAscii As Integer)
    If KeyAscii = 9 Then
        Text1.SetFocus
    End If
    End Sub
    I don't know if that will work for sure... try it and see
    <removed by admin>

  6. #6
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    oh wait. I see now that you are trying to use command buttons. here is some updated code.
    Code:
    Private Sub Command2_KeyPress(KeyAscii As Integer)
    If KeyAscii = 9 Then Command1.SetFocus
    End Sub
    and set the tabstop values or all the controls to false. That should work.
    <removed by admin>

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    111

    Thanks for the help

    Thanks for your replies. I was not clear enough in my second post. The reason I could not use the tab stops is that each tab, on the form has this same situation. So I could use it for one instance, but not all three.

    The users of my app want the focus to return to the first field on the tab when they tab past the last button on the tab. They want this on all three tabs, so I can not set the tab stop to the same number on three controls.

    Anyway, I will try this tomorrow when I get to work.

    Thanks again for your help.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    111

    KeyPress

    The KeyPress event does not seem to fire on a command button, when you hit the tab key. Key up and key down do not either. Does anyone have any ideas?

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Command1_LostFocus()
        If GetAsyncKeyState(vbKeyTab) Then Debug.Print "tab"
        If GetAsyncKeyState(vbKeyLeft) Then Debug.Print "left"
        If GetAsyncKeyState(vbKeyRight) Then Debug.Print "right"
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Location
    Texas
    Posts
    111

    Talking Thanks Kedeman

    I just found that on the Microsoft Support Site, and I was fixing to post it. For the record, it works great.

    Thanks again for all of your input.

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