Microsoft KB Archive/299615

= BUG: KeyPress Event Behaves Differently in a Compiled Executable =

Article ID: 299615

Article Last Modified on 10/15/2002

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q299615



SYMPTOMS
After displaying a dialog box in the KeyDown event, the KeyPress event is not raised when you run a project in the Integrated Development Environment (IDE). However, if you build the project into an executable file, the KeyPress event is raised. The behavior of the KeyPress event should be the same in a built executable as it is in the development environment. This inconsistency is observed with form and control keyboard events.



RESOLUTION
To work around this problem, store and use the window handle of the form or control that raises the KeyDown event to determine if the code in the KeyPress event should be run.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Create a new Standard EXE project in Visual Basic. Form1 is created by default.  Add the following code to the General Declarations section of Form1: Option Explicit

Dim hWndAW As Long

Private Declare Function GetActiveWindow Lib &quot;user32&quot; As Long

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) ' Save the active window handle ' when the KeyDown event occurs. hWndAW = GetActiveWindow MsgBox &quot;KeyDown Event&quot; End Sub

Private Sub Form_KeyPress(KeyAscii As Integer) ' To work around the problem, only run the ' desired code if the object that raised the ' KeyDown event is still the active window. '   If hWndAW = GetActiveWindow Then Form1.Caption = &quot;KeyPress Event&quot; '   End If End Sub  Press the F5 key to run the program. Press the SPACEBAR. Notice that a message box indicates that the KeyDown event has been raised. Also notice that the caption of the form remains &quot;Form1,&quot; which indicates that the KeyPress event has not been raised. Dismiss the message box, and close the running instance of Form1. From the File menu, click Make, and build Project1 into an executable file.</li> Run the built executable.</li> Press the SPACEBAR. Notice that a message box indicates that the KeyDown event has been raised. However, notice that the caption of the form has changed to &quot;KeyPress Event,&quot; which indicates that the KeyPress event has been raised.</li></ol>

To examine the workaround, uncomment the If and End If statements in the Form_KeyPress event, and build the project into an executable file. When you run this executable and press the SPACEBAR, the caption of the form remains &quot;Form1,&quot; which indicates that code in the KeyPress event has been bypassed.

Additional query words: InputBox Compile execute

Keywords: kbbug kbforms kbpending kbcompiler kbactivexevents kbideproject KB299615

-

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

© Microsoft Corporation. All rights reserved.