Enter Key Press Event

C

Coby

Is there a way to utilize the event of a user pressing the Enter Key
ONLY.

I understand the keypress event itself, but I want to isolate it or at
least be able to capture and determine which key was pressed.

I want to run a routine only when the user presses the "Enter Key" and
have it not fire when any other key is pressed.


Does anyone know a good way to go about this?

Thanks in advance for any help you can lend.

Coby.
 
R

Rick Rothstein \(MVP - VB\)

Use this structure inside the KeyPress event...

If KeyAscii = 13 Then
' The EnterKey was pressed, run your code here
KeyAscii = 0
End If

The KeyAscii = 0 statement has the effect of canceling the KeyPress
(especially needed to stop the beep sound in a single-line TextBox).

Rick
 
L

lesleyann76

Use this structure inside the KeyPress event...

If KeyAscii = 13 Then
' The EnterKey was pressed, run your code here
KeyAscii = 0
End If

The KeyAscii = 0 statement has the effect of canceling the KeyPress
(especially needed to stop the beep sound in a single-line TextBox).

Rick

I think that will work perfect. I have been playing with it, but have
run into one problem and maybe you have seen this.
It seems that since the KeyAscii = 0 line of code executed no key
press events work for the form anymore even after restarting the
application.

Do you know how to get the key press event to work again?
Whether it was that command or not there must be a way to get the form
object to turn its keypress events back to enabled?

I like the full control of your approach which will work very fast,
too.

Thanks for the help.
Coby.
 
R

Rick Rothstein \(MVP - VB\)

Use this structure inside the KeyPress event...

If KeyAscii = 13 Then
' The EnterKey was pressed, run your code here
KeyAscii = 0
End If

The KeyAscii = 0 statement has the effect of canceling the KeyPress
(especially needed to stop the beep sound in a single-line TextBox).

Rick

I think that will work perfect. I have been playing with it, but have
run into one problem and maybe you have seen this.
It seems that since the KeyAscii = 0 line of code executed no key
press events work for the form anymore even after restarting the
application.

Do you know how to get the key press event to work again?
Whether it was that command or not there must be a way to get the form
object to turn its keypress events back to enabled?

I like the full control of your approach which will work very fast,
too.[/QUOTE]

KeyAscii = 0 can only affect the one time it is executed... what it does is
substitute the value of 0 for the value passed into the KeyPress press event
(see the KeyAscii argument in the event declaration statement) by the system
for that one event execution only... it cannot be responsible for the
problem you are describing.

Do you by any chance have an Application.EnableEvents=False statement in
your code (which was to be followed by an Application.EnableEvents=True
statement later on)? If so, it sounds like your program exited before you
got a chance to set it back to True (something that should be taken care of
by an On Error statement or logic before an Exit Sub/Function). If this is
the case, just execute Application.EnableEvents=True in the Immediate window
to straighten things back out. If not, you will need to show us your code so
we can see what is going on.

Rick
 
C

Coby

KeyAscii = 0 can only affect the one time it is executed... what it doesis
substitute the value of 0 for the value passed into the KeyPress press event
(see the KeyAscii argument in the event declaration statement) by the system
for that one event execution only... it cannot be responsible for the
problem you are describing.

Do you by any chance have an Application.EnableEvents=False statement in
your code (which was to be followed by an Application.EnableEvents=True
statement later on)? If so, it sounds like your program exited before you
got a chance to set it back to True (something that should be taken care of
by an On Error statement or logic before an Exit Sub/Function). If this is
the case, just execute Application.EnableEvents=True in the Immediate window
to straighten things back out. If not, you will need to show us your code so
we can see what is going on.

Rick- Hide quoted text -

- Show quoted text -


The problem is all me (pebkac). . . Other events were still working,
but I set application events to true and still could not get it to
fire.
But, (1) I was trying to execute it on the entrire form, not the text
box object then (2) I was using a keypress instead of keydown, etc.

I have it all worked out now and this will allow me to move on to some
new things I am wanting to do.

Your help is greatly appreciated

Thanks,
Coby.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top