Solved

VB6 KeyPress prob

Posted on 2006-06-22
10
1,410 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
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 20

Expert Comment

by:alainbryden
Comment Utility
Is Num Lock on?
0
 

Author Comment

by:shifty12
Comment Utility
To alainbryden:
Yes, NumLock is on. My question again is, why is keypress returning a 43 instead of a 107? Thanx
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now