Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1455
  • Last Modified:

VB6 KeyPress prob

I have a Text_Keypress sub and I want it to convert a + sign from the numkeypad to an equal sign. The stmt reads "if keyascii = vbkeyadd then keyascii = 61". When I debug after hitting "+" on the numkeypad, keyascii has a value of 43, which is vbkeyexecute, whatever that is. It should be 107, which is the value of vbkeyadd. Whats the deal?
Thanx
0
shifty12
Asked:
shifty12
  • 5
  • 2
  • 2
  • +1
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You would see the vbKeyAdd constant in the KEYDOWN event which has the KeyCode parameter:

    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyAdd Then
            Debug.Print "Add key pressed..."
        End If
    End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could use something like this to change "+" into "=":

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Chr(KeyAscii) = "+" Then
            KeyAscii = Asc("=")
        End If
    End Sub

This doesn't differentiate between the "+" on the Keypad and the "+" above the equals sign though.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want it to only convert the KeyPad "+" then you could do something like:

    Private KeyAdd As Boolean

    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyAdd Then
            KeyAdd = True
        Else
            KeyAdd = False
        End If
    End Sub

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Chr(KeyAscii) = "+" And KeyAdd Then
            KeyAscii = Asc("=")
        End If
    End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
alainbrydenCommented:
Is Num Lock on?
0
 
shifty12Author Commented:
To alainbryden:
Yes, NumLock is on. My question again is, why is keypress returning a 43 instead of a 107? Thanx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I told ya already...

The KeyPress() event returns the KeyAscii value which is the Ascii value of final key generated regardless of which keys generated it.  The Ascii value of "+" is 43.  Try this in your Immediate window:

    ? Asc("+")

The KeyDown() event returns the KeyCode value which is the keyboard scancode of the key pressed.  This value allows you to determine the actual keyboard key being pressed.

For instance, the equals key has both "=" and "+" on it.  Regardless of whether you have the Shift key held down, when you press that key you will get 187 in the KeyDown() event because that is the key scan code.  The KeyPress() event, however, will give you 61 or 43.

See the difference?
0
 
kaliyugkaarjunCommented:
There r 2 types if keyt codes..
1) Ascii code : keys like a-z or number keys.
2) Virtual key code : Keys like shift, ctrl etc.

When u press keys from a-z or 0-9 or any of '+' or '-' operator key then both ascii code ane vk (virtual key ) code r sent through keyboard . But if function keys like shift , ctrl are pressed then only vk code is sent. The value ' 43' instead of '107' might be bcoz of keypress is getting only ascii code as a returned value and not vk code...

I am saying it might be the case...

0
 
alainbrydenCommented:
He's right, you can't just detect that 107 has been pressed because technically on the main part of the keyboard, it can't be pressed. You need to detect that 43 has been pressed in combination with one of the 'shift' buttons.
0
 
shifty12Author Commented:
The following is from VB help under Constants/VB:
-------------------------------------------
Keys on the Numeric Keypad
Constant Value Description
vbKeyNumpad0 96 0 key
vbKeyNumpad1 97 1 key
vbKeyNumpad2 98 2 key
vbKeyNumpad3 99 3 key
vbKeyNumpad4 100 4 key
vbKeyNumpad5 101 5 key
vbKeyNumpad6 102 6 key
vbKeyNumpad7 103 7 key
vbKeyNumpad8 104 8 key
vbKeyNumpad9 105 9 key
vbKeyMultiply 106 MULTIPLICATION SIGN (*) key
vbKeyAdd 107 PLUS SIGN (+) key
vbKeySeparator 108 ENTER (keypad) key
vbKeySubtract 109 MINUS SIGN (-) key
vbKeyDecimal 110 DECIMAL POINT(.) key
vbKeyDivide 111 DIVISION SIGN (/) key
-----------------------------------
It states quite clearly the numkey + should return a 107. Gee, could help be wrong? So I now compare to 43. I'll award the points to idle mind. Thanx guys
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The help is NOT wrong...

The constants you listed are KeyCode constants which are used in the KeyDown/KeyUp events.

You cannot use that value in the KeyPress event which gives you a KeyAscii value.

    KeyAscii <> KeyCode
    KeyPress <> KeyUp/KeyDown

!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now