Results 1 to 11 of 11

Thread: Unable to get Form_KeyDown to activate

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    440

    Unable to get Form_KeyDown to activate

    I have two Forms, Form1 shows Form2 (not Load it but causes it to be shown). Form2 has two buttons on it and here is the Form2 KeyDown event

    Code:
      
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
     Select Case KeyCode
       Case vbKeyY
         '
         ' Do some stuff to activate the Yes button
         '
         
       Case vbKeyN
         '
         ' Do some stuff to activate the No button
         '     
     End Select
    End Sub
    I'm not using the mouse but instead I want to use the keyboard. Now, after Form1 shows Form2, Form1 gives focus to Form2

    Code:
       '
       ' Do some stuff that causes Form2 to show
       ' 
       Form2.SetFocus
       '
       '
    Now when I key down on either the Y key or the N key nothing happens. I set a break point at the entry to the KeyDown event and it never breaks there

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,039

    Re: Unable to get Form_KeyDown to activate

    Set the KeyPreview property of said form(s) to True. It is False by default.

  3. #3
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,051

    Re: Unable to get Form_KeyDown to activate

    all controls that have keydown will fight to be the one in charge (focus) , even the one that u have not input any code will still trigger.
    to be able to "always" monitor the key-press, the control with the keydown code need to be in focus.

    solutions:

    1. have all controls with keydown send to 1 function, example:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    myFuncKeyDown KeyCode, Shift
    End Sub

    and do this to all controls, pictureboxes, textboxes etc.

    2. use a timer, that will always make one control focus.

    3. use a timer and use an API to monitor the key-press.

    4. subclassing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    440

    Re: Unable to get Form_KeyDown to activate

    Question about SetFocus

    If Form2 has the Focus after it is shown by Form1 will it lose the focus if Form1 continues on with it's processing even if Form1 does not set focus to any other control

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,000

    Re: Unable to get Form_KeyDown to activate

    What does a quick test of your supposition show?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    440

    Re: Unable to get Form_KeyDown to activate

    Well, after I show Form2 and set it's focus Form1 continues on with it's processing but nowhere in the code of Form1 do I set focus to anything but still Form2's KeyDown event does not trigger so my guess is that Form2 looses it's focus when execution continues on in Form1.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,173

    Re: Unable to get Form_KeyDown to activate

    Keep in mind that while code is processing events do not fire, unless of course you put doevents in your code to allow other events to fire which may or may not cause issues.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,000

    Re: Unable to get Form_KeyDown to activate

    Quote Originally Posted by Ordinary Guy View Post
    Well, after I show Form2 and set it's focus Form1 continues on with it's processing but nowhere in the code of Form1 do I set focus to anything but still Form2's KeyDown event does not trigger so my guess is that Form2 looses it's focus when execution continues on in Form1.
    And you did set the KeyPreview property of Form2 to True in its load event?

    A quick test seems to work fine for me.
    Form1 code.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Form2.Show
      Timer1.Interval = 1
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Static DoingStuff As Long
      DoingStuff = DoingStuff + 1
      
      Cls
      Print DoingStuff
    End Sub
    Form2 code
    Code:
    Option Explicit
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      Print "I got a KeyDown"
    End Sub
    
    Private Sub Form_Load()
      KeyPreview = True
    End Sub
    As DataMiser says, if you are running code in a sub without exiting the sub, then events can't be processed, so neither form nor the timer will be processing events until your code exits, or you purposely add DoEvents to suspend your code so pending events can be processed.

    It would be best that you didn't have extensive processing in a single sub, but used the event driven nature that you are suppose to use with GUI applications, i.e. spread the processing out into steps and execute the steps from timer ticks, if possible.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    440

    Re: Unable to get Form_KeyDown to activate

    First off, the KeyPreview doesn't make any difference. As long as Form2 has the focus I can get the KeyDown to fire even when KeyPreview is False. The problem I'm having is keeping the focus on Form2. Form1 is executing code in a Timer event and I do have DoEvents. When the Timer event exits I'm not executing any other code (at least no code in my program) so the only thing it is doing is waiting for the user to press either the Y or the N key. I can't find anywhere in the code that would cause the focus to be taken away from Form2.

  10. #10
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,051

    Re: Unable to get Form_KeyDown to activate

    its very hard to control focus. if keypreview dont work, maybe u need another solution, like API or subclassing.
    Last edited by baka; May 19th, 2020 at 11:39 PM.

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,830

    Re: Unable to get Form_KeyDown to activate

    Mayhaps instead of explaining what you're doing, explain the end goal here. There may be a better way to skin this cat. Given the descriptions so far, I have my suspicions as to what's going on (as far as intentions go) but I want to see if that's true or not.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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