Results 1 to 11 of 11

Thread: Calculator Program

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566

    Unhappy Calculator Program

    Hi,

    I found a small calculator program in the MSDN samples
    called CALC.

    I need to change it, because I need 2 result displays.
    We need that stupid Euro you see.

    Instead of using the mouse I want to use the numeric keypad
    keys so I changed the program by putting the key codes.

    The problem I have is that I can use the numeric and the
    operator keys, but the enter key won't work.

    I use the vbKeySeparator Key Code for activating the '=' key
    but it won't work.

    When using the mouse and clicking the '=' button all works
    fine.

    Is there someone who could help me with this or maybe
    someone have a source of the windows calculator program.

    thanx
    Ray
    Ray

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    I'm not sure what u mean, could you post some sample code ?
    -= a peet post =-

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566
    Hi peet,

    The problem is simple.
    If you start the calculator program in windows, than you
    can use either the mouse or the numeric pad buttons.

    If you have the MSDN library then there'is in the sample
    directory an application called CALC written by microsoft.

    That works only with the mouse so I changed it for using also
    the numeric key pad.

    All buttons are working, 0 to 9, the +,-,* and / also.

    The "=" key don't exists so I need to use the numeric
    pad enter key for simulating the '=' key.

    It is called vbKeySeparator so when I press the enter key
    I translate it to operator '='.

    And here it goes wrong, the application ignores the vbKeySeparator check and I don't know why.

    cheers
    Ray
    Ray

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566
    Peet,

    Here the calc source code.
    Maybe you find the error.


    thanx
    Ray
    Ray

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hohohoooo... look what i found in the MSDN : chekc article Q188550

    it should explain your problems i think
    -= a peet post =-

  6. #6
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    or else you could trap either keycodes 13 (enter) or 187 (=) in the form keydown event

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566
    peet

    can't find the article, can you explain?



    rudvs2

    Isn't working either.


    I think the problem lies on the form focus.

    For some reason I lose the form focus so the Keyup procedure
    hasn't been called.


    Ray
    Ray

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    ok, here goe's...

    MSDN

    BUG: vbKeySeparator Constant Does Not Work

    Q188550


    --------------------------------------------------------------------------------
    The information in this article applies to:

    Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
    Microsoft Visual Basic Standard, Professional, and Enterprise Editions for Windows, version 4.0

    --------------------------------------------------------------------------------


    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:

    Code:
          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.



    MORE INFORMATION

    Steps to Reproduce Behavior
    Start a new Standard EXE project in Visual basic. Form1 is created by default.


    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



    Run the project and press both ENTER keys. Note that the KeyCode is 13 regardless of which ENTER key is pressed.





    REFERENCES
    For a more detailed explanation of this bitwise comparison, please see the following article in the Microsoft Knowledge Base:

    Q77550 Differentiating Between the Two ENTER Keys

    Additional query words: kbDSupport kbDSD kbVBp kbvbp400bug kbVBp500bug kbVBp600bv kbKeyIn kbHook

    Keywords : kbGrpVB
    Issue type : kbbug
    Technology :


    Last Reviewed: February 2, 2000
    © 2001 Microsoft Corporation. All rights reserved. Terms of Use.


    -= a peet post =-

  9. #9
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    try this code

    VB Code:
    1. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    2. Select Case KeyCode
    3.  
    4.     Case Is = vbKeyNumpad0
    5.         Number_Click 0
    6.    
    7.     Case Is = vbKeyNumpad1
    8.         Number_Click 1
    9.  
    10.     Case Is = vbKeyNumpad2
    11.         Number_Click 2
    12.  
    13.     Case Is = vbKeyNumpad3
    14.         Number_Click 3
    15.  
    16.     Case Is = vbKeyNumpad4
    17.         Number_Click 4
    18.  
    19.     Case Is = vbKeyNumpad5
    20.         Number_Click 5
    21.  
    22.     Case Is = vbKeyNumpad6
    23.         Number_Click 6
    24.  
    25.     Case Is = vbKeyNumpad7
    26.         Number_Click 7
    27.  
    28.     Case Is = vbKeyNumpad8
    29.         Number_Click 8
    30.  
    31.     Case Is = vbKeyNumpad9
    32.         Number_Click 9
    33.  
    34.     Case Is = vbKeyDivide
    35.         Operator_Click 0
    36.  
    37.     Case Is = vbKeyAdd
    38.         Operator_Click 1
    39.  
    40.     Case Is = vbKeyMultiply
    41.         Operator_Click 2
    42.  
    43.     Case Is = vbKeySubtract
    44.         Operator_Click 3
    45.  
    46.     Case Is = 187
    47.         Operator_Click 4
    48.  
    49.     Case Is = vbKeyEnd
    50.         cmdStop_Click
    51.        
    52.     Case Is = 13
    53.         Operator_Click 4
    54.        
    55. End Select
    56.  
    57.  
    58.  
    59. End Sub

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566
    peet

    Thanx peet now I can solve the problem.



    rudvs2

    Your code works only when I put a text control on
    the form and set the focus on the text control.

    Regarding the article from peet the vbKeySeparator
    or code 187 don't work.



    thanx both for your help
    Ray
    Ray

  11. #11
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    any time Ray
    -= a peet post =-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width