?
Solved

Form_KeyDown event will not fire?

Posted on 2006-07-23
23
Medium Priority
?
1,115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
  • +4
23 Comments
 

Author Comment

by:lee88
ID: 17164160
I changed to this:

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

Still, no luck.
0
 

Author Comment

by:lee88
ID: 17164228
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
ID: 17164459
Have you tried setting KeyPreview in the IDE?  Either that, or setting it before you show the form?

J.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:Ryan_R
ID: 17164677
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
ID: 17164688
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
ID: 17164699
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
ID: 17164703
>> I have been doing a ton of editing on this program

Did you add any third-party controls?
0
 

Author Comment

by:lee88
ID: 17164778
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
ID: 17165697
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
ID: 17166488

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

Author Comment

by:lee88
ID: 17167136
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
 

Author Comment

by:lee88
ID: 17167600
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
ID: 17167884
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 2000 total points
ID: 17170010
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
ID: 17170074
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
ID: 17170110
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
ID: 17170159
>>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
ID: 17170196
<SLAP>Ouch</SLAP>
0
 

Author Comment

by:lee88
ID: 17170202
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
ID: 17170209

>>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
ID: 17173165
>>>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
ID: 17174529

>>"frmMain_Activate"
that is totally wrong code!

that should be "Form_Activate"
0
 
LVL 15

Expert Comment

by:Ryan_R
ID: 17188580
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

765 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