Microsoft KB Archive/188550

= BUG: vbKeySeparator Constant Does Not Work =

Article ID: 188550

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition

-



This article was previously published under Q188550



SYMPTOMS
When using the constant vbKeySeparator to evaluate keystrokes in the KeyUp, KeyDown, or KeyPress event, the comparison always returns False.



CAUSE
The Visual Basic documentation lists a keycode constant of vbKeySeparator. The value of this constant is 108 (0x6C) and that value corresponds to the ENTER key on the Numeric Keypad. It also lists the constant vbKeyReturn that has the value 13 (0xD) and corresponds to the ENTER key on the Keyboard. In the KeyDown, KeyPress, or KeyUp events, the keycode returned is 13 regardless of which ENTER key is pressed. A test to see if the keycode is equal to 108 will always return False because 13 is obviously not equal to 108. The result is that there is no way to determine which ENTER key was depressed.



RESOLUTION
It is possible to differentiate between the two ENTER keys using the Win32 API PeekMessage. A bitwise comparison can then be made to determine which of the ENTER keys was pressed.



WORKAROUND
 Start a new Standard EXE project in Visual Basic. Form1 is created by default.  Add the following code to Form1: Private Declare Function PeekMessage Lib "user32" Alias _ "PeekMessageA" (lpMsg As MSG, ByVal hwnd As Long, _       ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, _        ByVal wRemoveMsg As Long) As Long

Private Type POINTAPI x As Long y As Long End Type

Private Type MSG hwnd As Long message As Long wParam As Long lParam As Long time As Long pt As POINTAPI End Type

Const PM_NOREMOVE = &H0 Const WM_KEYDOWN = &H100 Const WM_KEYUP = &H101 Const VK_RETURN = &HD

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Dim MyMsg As MSG, RetVal As Long

' pass: '  MSG structure to receive message information '  my window handle '  low and high filter of 0, 0 to trap all messages '  PM_NOREMOVE to leave the keystroke in the message queue '  use PM_REMOVE (1) to remove it      RetVal = PeekMessage(MyMsg, Me.hwnd, 0, 0, PM_NOREMOVE)

' now, per Q77550, you should look for a MSG.wParam of VK_RETURN ' if this was the keystroke, then test bit 24 of the lparam - if ON, ' then keypad was used, otherwise, keyboard was used If RetVal <> 0 Then If MyMsg.wParam = VK_RETURN Then If MyMsg.lParam And &H1000000 Then MsgBox "Enter from Keypad pressed" Else MsgBox "Enter from Keyboard pressed" End If     End If      Else MsgBox "No message waiting, or possible problems calling PeekMessage" End If     End Sub  Run the project and press on both ENTER keys. You will get a message box correctly identifying which ENTER key was pressed.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.



Steps to Reproduce Behavior
 Start a new Standard EXE project in Visual basic. Form1 is created by default.</li>  Add the following code to Form1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode

Case vbKeyReturn MsgBox "KeyCode 13. Keyboard Enter Key Pressed."

Case vbKeySeparator MsgBox "KeyCode 108. NumPad Enter Key Pressed."

End Select End Sub </li> Run the project and press both ENTER keys. Note that the KeyCode is 13 regardless of which ENTER key is pressed.</li></ol>

<div class="references_section">