BUG: KeyDown & KeyUp Events Receive Same Key Codes
ID: Q93669
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
SYMPTOMS
In Visual Basic for MS-DOS, the KeyDown and KeyUp events receive the same
key codes for some standard ASCII and extended keys. For example, the F1
key and the lowercase "p" both return a KeyCode value of 112. Because the
KeyPress event occurs only for standard keys, you can determine which key
was pressed by monitoring whether a KeyPress event occurred.
WORKAROUND
You can work around the problem because the KeyDown and KeyUp events are
called when you press an extended key, whereas the KeyPress event is called
when you press a standard ASCII key. If you set a variable in the KeyPress
event, you can then determine if an ASCII key was pressed. An example is
shown below in the More Information section.
STATUS
Microsoft has confirmed this to be a bug in the Standard and Professional
Editions of Microsoft Visual Basic version 1.0 for MS-DOS. We are
researching this problem and will post new information here in the
Microsoft Knowledge Base as it becomes available.
MORE INFORMATION
This behavior differs from Microsoft Visual Basic for Windows which gives
different KeyCode values for standard ASCII versus enhanced keys.
Note that the Form_KeyDown, Form_KeyUp, and Form_KeyPress occur only if
there are no controls on the form. To detect keys when you do have controls
on a form, you must check for the keys in each control that can have the
focus.
There are three form events that can get called when you press a key:
KeyDown, KeyPress, and KeyUp. When you press an extended key, only the
KeyDown and KeyUp events get called. When you press a standard key, all
three events get called.
Step-by-Step Workaround Example
The following example demonstrates how to work around the problem:
- Start VBDOS.EXE.
- From the File menu select New Form... (FORM1.FRM). Form1 is created
by default.
- Exit the Form Designer, save all changes.
- Add the following code to the module level of Form1:
DIM SHARED Extended AS INTEGER
- Add the following code to the Form_KeyDown event procedure of Form1:
SUB Form_KeyDown (KeyCode AS INTEGER, Shift AS INTEGER)
Extended = -1
END SUB
- Add the following code to the Form_KeyPress event procedure of Form1:
SUB Form_KeyPress (KeyAscii AS INTEGER)
Extended = 0
END SUB
- Add the following code to the Form_KeyUp event procedure of Form1:
SUB Form_KeyUp (KeyCode AS INTEGER, Shift AS INTEGER)
Const KEY_F1 = 112
IF Extended THEN
SELECT CASE KeyCode
CASE KEY_F1
PRINT "F1"
' Add more CASE statements to trap more keys.
END SELECT
ELSEIF KeyCode >= 32 OR KeyCode < 127 THEN
PRINT CHR$(KeyCode)
END IF
END SUB
- Press F5 to run the application. This program traps the F1 and p
keys. To trap more extended keys, add more lines to the SELECT CASE
statement. To trap more standard ASCII characters, add more IF
statements.
Additional query words:
VBmsdos buglist1.00 1.00
Keywords :
Version : MS-DOS:1.0
Platform : MS-DOS
Issue type :