Solved

VB6 KeyPress prob

Posted on 2006-06-22
10
1,415 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
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 85

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 85

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 85

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 85

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 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