Microsoft KB Archive/200266

= PRB: Form's KeyPress Is Not Fired When ActiveX Control Has Focus =

Article ID: 200266

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition

-



This article was previously published under Q200266



SYMPTOMS
You have a form with code in the KeyPress event and the KeyPreview property is set to true (.T.). When an ActiveX control has focus, the KeyPress event of the form does not fire.



CAUSE
The KeyPress event for many ActiveX controls happens to have the same name as the Visual FoxPro native KeyPress event. ActiveX controls on Visual FoxPro forms also have their own window. As a result, they handle the KeyPress event independently of Visual FoxPro.



RESOLUTION
In the KeyPress of the ActiveX control, add the following code as the first line of the event: ThisForm.KeyPress



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Create a new form with a command button and an ActiveX control. Use an ActiveX control with a KeyPress event, such as the Calendar control. Set the KeyPreview property of the form to .T. (true). Add the following code to the following events (assumes default object names are used). When finished, save and run the form.   Form1.KeyPress: WAIT WINDOW "Form" TIMEOUT 1   Command1.KeyPress: WAIT WINDOW "Button" TIMEOUT 1   Olecontrol1.KeyPress: WAIT WINDOW "control" TIMEOUT 1 </li></ul> </li> With the form running, set focus to the command button and press any key. Watch the Wait windows in the upper right-hand corner of the screen. Note that the KeyPress event of the form fires before that of the command button.</li> Set focus to the ActiveX control and press any key. The form's KeyPress event does not fire, but the control's KeyPress event does.</li></ol>

Keywords: kbbug kbnofix kbcodesnippet kbprb kbctrl KB200266

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.