Solved

Form_KeyDown event will not fire?

Posted on 2006-07-23
23
1,101 Views
Last Modified: 2013-11-26
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!
0
Comment
Question by:lee88
  • 7
  • 4
  • 4
  • +4
23 Comments
 

Author Comment

by:lee88
Comment Utility
I changed to this:

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

Still, no luck.
0
 

Author Comment

by:lee88
Comment Utility
I tried compiling the app, thinking the problem might be with my VB6 IDE, but still no luck.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
Comment Utility
Have you tried setting KeyPreview in the IDE?  Either that, or setting it before you show the form?

J.
0
 
LVL 15

Expert Comment

by:Ryan_R
Comment Utility
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!
0
 

Author Comment

by:lee88
Comment Utility
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.
0
 
LVL 15

Expert Comment

by:Ryan_R
Comment Utility
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
0
 
LVL 12

Expert Comment

by:jkaios
Comment Utility
>> I have been doing a ton of editing on this program

Did you add any third-party controls?
0
 

Author Comment

by:lee88
Comment Utility
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).
0
 
LVL 12

Expert Comment

by:jkaios
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
Comment Utility

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

Author Comment

by:lee88
Comment Utility
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?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:lee88
Comment Utility
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).


0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
create a new project with frmmain and copy all the code and control to it and try?
0
 
LVL 9

Accepted Solution

by:
Rob_Jeffrey earned 500 total points
Comment Utility
If you have *any* controls on your form that are capable of recieving the KeyDown event then the parent form will never see that event.
(textbox, picturebox, button, checkbox, option buttons, combo box, lists, scrolls, etc)

I have had this problem as well and needed to add a keyDown event to *every* control which calls Form_KeyDown(KeyCode As Integer, Shift As Integer) with the values from its own event.  This also works if you make your own function/sub for this event and call it directly from each event.

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

The only other way I've has success is to use a hook and call back when a key was pressed anywhere in/on the form.

Rob
0
 
LVL 9

Expert Comment

by:Rob_Jeffrey
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:Rob_Jeffrey
Comment Utility
Oh - one more thing - you could alse set Enabled=false to all controls and have the form event fire again.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
Comment Utility
>>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...
0
 
LVL 9

Expert Comment

by:Rob_Jeffrey
Comment Utility
<SLAP>Ouch</SLAP>
0
 

Author Comment

by:lee88
Comment Utility
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?

0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
Comment Utility

>>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...
0
 
LVL 15

Expert Comment

by:Ryan_R
Comment Utility
>>>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)
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
Comment Utility

>>"frmMain_Activate"
that is totally wrong code!

that should be "Form_Activate"
0
 
LVL 15

Expert Comment

by:Ryan_R
Comment Utility
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
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now