• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1249
  • Last Modified:

problem with keypress VS keydown

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?
1 Solution
post your code.
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
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.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

jkaiosIT DirectorCommented:
Yes, if both the KeyDown and KeyPress events are present, then the "KeyDown" event would fire, thus suppressing the KeyPress event.
jessi_vsAuthor Commented:
thanks, let me check it up. both events or in a textbox.
jkaiosIT DirectorCommented:
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] -----------------------------------------------------------------

  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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now