Link to home
Start Free TrialLog in
Avatar of lee88
lee88Flag for United States of America

asked on

Form_KeyDown event will not fire?

I have a VB6 program that has several forms and modules. The frmMain form uses the Form_KeyDown event, which used to work.

I have been doing a ton of editing on this program, and now, when I run the app (in the VB6 IDE), the Form_KeyDown event will not fire when I hit any key. (I set a breakpoint on the first statement in Sub Form_KeyDown(KeyCode As Integer, Shift As Integer), [which is: Debug.Print "Here"], and program control never breaks.

I was guessing that it is a focus problem. The Startup object is Sub Main, which looks like this:

Sub Main()
    frmMain.Show
End Sub

frmMain is the only form that is displayed or loaded (for now), so, once frmMain loads, program control hits Sub Main()'s "End Sub", as shown above, and is done. So, I think frmMain has focus.

I have tried clicking on frmMain (in areas that do not have objects) to try to ensure that frmMain has focus before I hit a key, but that does not help either.

Any ideas?
Thanks!
Avatar of lee88
lee88
Flag of United States of America image

ASKER

I changed to this:

Sub Main()
    frmMain.Show
    frmMain.KeyPreview = True
End Sub

Still, no luck.
Avatar of lee88

ASKER

I tried compiling the app, thinking the problem might be with my VB6 IDE, but still no luck.
Have you tried setting KeyPreview in the IDE?  Either that, or setting it before you show the form?

J.
does the KeyPress work (does it matter which one you use in this instance)

is your debug.print in a KeyCheck if statement

ie

Private Sub frmMain.KeyDown(KeyCode as integer, Shift as Integer)
msgbox "this works"
If KeyCode = 127 then msgbox "but this doesn't"
End Sub

'yes i prefer to use msgbox's to detect running code - you can put whatever yo uwant in them to specify what's what!
Avatar of lee88

ASKER

jimbobmcgee: I did set KeyPreview to true on the form at design-time. Is that what you were asking?

Ryan_R: None of these work at all for any key: KeyPress, KeyDown, KeyUp; even with msgbox.
try adding a new form with the default options set and see if you can get it to work on that form - you may have changed an option somewhere in your code that you need to find
>> I have been doing a ton of editing on this program

Did you add any third-party controls?
Avatar of lee88

ASKER

Ryan_R: good idea. I added a new form - nothing on it and all default settings. KeyDown works fine on it. Do you know of any settings other than KeyPreview = true (and focus) that have anything to do with KeyDown, KeyUp, or KeyPress that I might have set on frmMain to disable its KeyDown, KeyUp, and KeyPress?

jkaios: no third party controls. (another good idea, though).
To be sure that the "KeyPreview" property is set, just place a test in the Activate event of your main form:

Private Sub frmMain_Activate()
  Msgbox "KeyPreview = " & frmMain.KeyPreview
End Sub

Also, to be sure again, search the entire codes in your main form or project for "KeyPreview" to ensure that no other routines set that property
other than the Sub Main startup routine.  Since that you've made a "ton of changes" to your program, you might want to check for this in case
you inadvertently reset the KeyPreview property to false by mistake.

don't you use subclassing to intercept messages somewhere,
or are there any controls that use setcapture?
Avatar of lee88

ASKER

I placed a test in the Activate event of my frmMain form:

Private Sub frmMain_Activate()
  Msgbox "KeyPreview = " & frmMain.KeyPreview
End Sub

The msgbox never appears. What does that tell you?
Avatar of lee88

ASKER

There was a third party control. I removed its reference from the project, and commented out all instances of it being invoked.

I also confirmed that there are no "frmMain.KeyPreview = false" statements.

I saved the project, closed VB, and reopened VB and the project.

Now, the msgbox in Activate event of my frmMain form does show "KeyPreview = true" when the form is first shown on the screen, but still no KeyDown event for any keys (I try many different keys).


create a new project with frmmain and copy all the code and control to it and try?
ASKER CERTIFIED SOLUTION
Avatar of Rob_Jeffrey
Rob_Jeffrey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rob_Jeffrey
Rob_Jeffrey

You can see this happen by starting a brand new project - only one form (have it as startup) and add the event

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Debug.Print "Fired"
End Sub

Run the program and press any key.
Now - without adding any more code - add any standard control other than frame, label, shape, timer or image.
If you run the program now you will not see the event fire.
Oh - one more thing - you could alse set Enabled=false to all controls and have the form event fire again.
>>If you run the program now you will not see the event fire.

now set the form's keypreview to true,
and it will fire again...
<SLAP>Ouch</SLAP>
Avatar of lee88

ASKER

Rob: Good suggestion about adding a keyDown event to *every* control that has a KeyDown event.

I must have other issues, though. Now, if I compile the app, when I run the EXE (on my Laptop or on the target PC), the KeyDown even fires (the form's KeyDown event, not another control's keyDown event), but, when I run the app in the VB6 IDE, the event does not fire.

What could cause running the app in the IDE to handle the event differently that the app's EXE?


>>What could cause running the app in the IDE to handle the event differently that the app's EXE?
the code is interpreted "live" when running in ide
and compiled in an exe

this can cause some things to work different...
>>>I placed a test in the Activate event of my frmMain form:

Private Sub frmMain_Activate()
  Msgbox "KeyPreview = " & frmMain.KeyPreview
End Sub

The msgbox never appears. What does that tell you?<<<<
----------------------------------------------------
frm_Activate????? this ain't foxpro!
use Form_Load or Form_Initialize, preferably Load

running your apps in the IDE instead of live can result in different forms taking focus either in your app or another one, depending on what type of calls/declarations are in your code (eg when starting up into the sys tray)

>>"frmMain_Activate"
that is totally wrong code!

that should be "Form_Activate"
why do we want to use Activate anyway (apart from separating it from all the code probably found in Form_Load)?
code in Form_Load will run before Form_initialize, dunno where in the order of code running Activate comes into it.
In Vis FoxPro there is no Form_Load event - you would use Form_Activate instead (terrible language - yet so similar to VB but you have to type everything the long way)

ie: in VB6 you might type:
txtBox = "Hello"
or txtBox.Text = "Hello"
in FoxPro you have to type
thisform.txtBox.Text = "Hello"  *otherwise it won't work (just 1 example anyway) - only good thing about FoxPro is that using databases with your app is easier