?
Solved

VB6 KeyPress prob

Posted on 2006-06-22
10
Medium Priority
?
1,451 Views
Last Modified: 2010-05-18
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
Comment
Question by:shifty12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16960999
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16961013
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16961028
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Expert Comment

by:alainbryden
ID: 16961906
Is Num Lock on?
0
 

Author Comment

by:shifty12
ID: 16968378
To alainbryden:
Yes, NumLock is on. My question again is, why is keypress returning a 43 instead of a 107? Thanx
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 16968672
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
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16968723
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
 
LVL 20

Expert Comment

by:alainbryden
ID: 16970559
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
 

Author Comment

by:shifty12
ID: 16985533
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16985843
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question