Solved

problem with keypress VS keydown

Posted on 2004-09-29
6
1,227 Views
Last Modified: 2008-02-01
I put some coding both in the keypress event and keydown event. but the keypress event is not fired why? is it keydown suppress keypress?
0
Comment
Question by:jessi_vs
6 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 12177486
post your code.
0
 
LVL 10

Expert Comment

by:ADSaunders
ID: 12177538
Hi jessi_vs,
Is the keypress event in the form or in a control on the form? If it is on the form, the keypress will only be seen if the KeyPreview property of the form is set to true.

Regards .. Alan
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12177755
keypress event occurs when the user presses and releases an ANSI key.The object with the focus receives the event.
keydown event occurs when the user presses a key while an object has the focus.

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 12

Expert Comment

by:jkaios
ID: 12187451
Yes, if both the KeyDown and KeyPress events are present, then the "KeyDown" event would fire, thus suppressing the KeyPress event.
0
 

Author Comment

by:jessi_vs
ID: 12187473
thanks, let me check it up. both events or in a textbox.
0
 
LVL 12

Accepted Solution

by:
jkaios earned 20 total points
ID: 12187772
Perhaps you might want to try this to confirm.

1. Place a text box on the form and call it Text1
2. Paste the following codes below
3. Run your app the hit some key -- you should confirm that only the KeyDown event fires

-----------------[Start of Code] ----------------------------------------------------------------

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
 MsgBox "KeyCode = " & KeyCode, , "Text1_KeyDown"
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
 MsgBox "KeyAscii = " & KeyAscii, , "Text1_KeyPress"
End Sub

-----------------[End of Code] -----------------------------------------------------------------

P.S.
  The advantage of KeyDown event is that it can detect the Navigation keys, such as
  the PageUp, PageDown, Home, End keys. It can also detect the Delete and Back Space
  and the Function keys.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

776 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