Solved

VB6 KeyPress prob

Posted on 2006-06-22
10
1,439 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 100 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

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.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses
Course of the Month3 days, 23 hours left to enroll

630 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