Results 1 to 9 of 9

Thread: [RESOLVED] KeyDown (loosing focus problem)

  1. #1

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Resolved [RESOLVED] KeyDown (loosing focus problem)

    I have a CommandButton which executes some code (shows a Form modally). I'm using a "shortcut" for it - F10 - and .KeyPreview at True:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Select Case (KeyCode)
            Case vbKeyF2
                '...
            Case vbKeyF10
                cmdUsers.Value = True
            Case vbKeyF12
                '...
        End Select
    End Sub
    But whenever i press F10 and it doesn't get executed (for some reason that is not important), i have to re-set focus to any other control (even thow cmdUsers is NOT visible) in order for any other "shortcut" (F2, F12) to work. Why's that? It work's fine after when the code executes normally.

  2. #2
    Addicted Member
    Join Date
    Oct 2006
    Location
    Chennai, India
    Posts
    198

    Re: KeyDown (loosing focus problem)

    1. The sample code as given works fine.

    2. Please ensure that F10 key is not set as shortcut to anyother Desktop application

    3. Also kindly correct the code as :

    Code:
    Case vbKeyF10
                cmdUsers.Value = True
                cmdUsers.SetFocus

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

    Re: KeyDown (loosing focus problem)

    Where is the focus that you have to reset?

  4. #4

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: KeyDown (loosing focus problem)

    Quote Originally Posted by cheenu_vasan
    1. The sample code as given works fine.

    2. Please ensure that F10 key is not set as shortcut to anyother Desktop application

    3. Also kindly correct the code as :

    Code:
    Case vbKeyF10
                cmdUsers.Value = True
                cmdUsers.SetFocus
    1. Well... i already explained when it works - perhaps you should re-"read" my post
    2. I did. It's not being used.
    3. I cannot set the focus, because sometimes the cmdUsers isn't visible.
    Quote Originally Posted by Hack
    Where is the focus that you have to reset?
    It doesn't matter. It's actually on a ListView. But, for some reason, i simply have to click somewhere (ANYWHERE!!) in order for "shortcuts" to work. It's very very strange. On the other hand, it F10 executes it's code, "shortcuts" work fine. Should i "flush" the keyboard, perhaps? Like "fflush()" in C? If so, how is it being done in VB?

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

    Re: KeyDown (loosing focus problem)

    Well, as odd as it may sound, it the focus is on a ListView, why not use its KeyDown event?

  6. #6

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: KeyDown (loosing focus problem)

    Quote Originally Posted by Hack
    Well, as odd as it may sound, it the focus is on a ListView, why not use its KeyDown event?
    True. But the focus can be ANYWHERE ELSE as well

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: KeyDown (loosing focus problem)

    maybe if the control is not visible you just need to setfocus to some visible control
    or instead of setting it's value to true, just call its click event
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: KeyDown (loosing focus problem)

    Quote Originally Posted by westconn1
    maybe if the control is not visible you just need to setfocus to some visible control
    I tried using Me.ActiveControl.SetFocus - but nothing happens. I still gotta click somewhere in order for "shortcuts" to work again.
    Quote Originally Posted by westconn1
    or instead of setting it's value to true, just call its click event
    I did - it's the same.

  9. #9

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: KeyDown (loosing focus problem)

    Well. I guess it's being used by another application. But nothing happens when i press it - but for some reason, it "freezes" my application. I'll use another "shortcut"

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