For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > September 2004 > Can't catch enter key in form.keypress event









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can't catch enter key in form.keypress event
Marty

2004-09-29, 3:55 pm

Hi,

Do you have any idea why a form keypress event would stop to catch the
"enter" key?

Even if I run this:

Private Sub Form_KeyPress(KeyAscii As Integer)
msgbox KeyAscii
End Sub

The "enter" key is not catched.....

In my form I don't have any control who could disable the enter key event.

Any suggestion would be appreciated.

Marty
Bob Butler

2004-09-29, 3:55 pm

"Marty" <reply@to.forum> wrote in message
news:FeC6d.2433$eq.843@edtnps84
> Hi,
>
> Do you have any idea why a form keypress event would stop to catch the
> "enter" key?
>
> Even if I run this:
>
> Private Sub Form_KeyPress(KeyAscii As Integer)
> msgbox KeyAscii
> End Sub
>
> The "enter" key is not catched.....
>
> In my form I don't have any control who could disable the enter key
> event.


Do you have a command button with .Default=True ?

--
Reply to the group so all can participate
VB.Net... just say "No"

Marty

2004-09-29, 3:55 pm

>
> Do you have a command button with .Default=True ?
>



No, I don't.
Charlie

2004-09-29, 3:55 pm

The KeyPress event seems to favor controls. I just use the KeyPress event of
the control I know will have the focus.

"Marty" wrote:

>
>
> No, I don't.
>

Rick Rothstein

2004-09-29, 3:55 pm

> "enter" key?
>
> Even if I run this:
>
> Private Sub Form_KeyPress(KeyAscii As Integer)
> msgbox KeyAscii
> End Sub
>
> The "enter" key is not catched.....
>
> In my form I don't have any control who could disable the enter key

event.

Do you have the form's KeyPreview property set to True? If not, try
setting it to that.

Rick - MVP

Bob Butler

2004-09-29, 3:55 pm

"Marty" <reply@to.forum> wrote in message
news:XtC6d.2436$eq.1382@edtnps84
>
> No, I don't.


The form's KeyPreview property is True? do the KeyDown and/or KeyUp events
fire?

what control types are on the form?

--
Reply to the group so all can participate
VB.Net... just say "No"

Marty

2004-09-29, 3:55 pm

Yes the form' KeyPreview = True

I did another test.

sub Form_load()
btnMyEvent.Default = True 'Command button control
btnMyEvent.getFocus = True
end sub

Private Sub btnMyEvent_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii
End Sub

This won't catch the "enter" key. All other key are catched...

Marty

2004-09-29, 3:55 pm

If I have another control button that get the focus, will it override
the form's KeyPreview property?
Bob Butler

2004-09-29, 3:55 pm

"Marty" <reply@to.forum> wrote in message
news:rMC6d.2449$eq.443@edtnps84
> Yes the form' KeyPreview = True
>
> I did another test.
>
> sub Form_load()
> btnMyEvent.Default = True 'Command button control
> btnMyEvent.getFocus = True


there is no .GetFocus property on command buttons in VB... are you using
VB.Net or VB 2005 and confusing it with VB6 and/or earlier versions? If so,
you are asking in the wrong newsgroup and need to find one with 'dotnet' in
the name.

--
Reply to the group so all can participate
VB.Net... just say "No"

Marty

2004-09-29, 3:55 pm

My mistake,

I mean :
btnMyEvent.SetFocus

instead of
btnMyEvent.GetFocus



Bob Butler wrote:

> "Marty" <reply@to.forum> wrote in message
> news:rMC6d.2449$eq.443@edtnps84
>
>
>
> there is no .GetFocus property on command buttons in VB... are you using
> VB.Net or VB 2005 and confusing it with VB6 and/or earlier versions? If so,
> you are asking in the wrong newsgroup and need to find one with 'dotnet' in
> the name.
>

Bob Butler

2004-09-29, 3:55 pm

"Marty" <reply@to.forum> wrote in message news:cYC6d.2452$eq.10@edtnps84
> My mistake,
>
> I mean :
> btnMyEvent.SetFocus
>
> instead of
> btnMyEvent.GetFocus


No problem; I just tried it and apparently when a command button has the
focus the KeyXXXX events are not fired for ENTER. It also doesn't fire the
default command button unless that's the one that has the focus. I don't
remember running into that before.

--
Reply to the group so all can participate
VB.Net... just say "No"

Marty

2004-09-29, 3:55 pm

Ok, I'll try to give the focus to the form and see if I can make it work.



Bob Butler wrote:

> "Marty" <reply@to.forum> wrote in message news:cYC6d.2452$eq.10@edtnps84
>
>
>
> No problem; I just tried it and apparently when a command button has the
> focus the KeyXXXX events are not fired for ENTER. It also doesn't fire the
> default command button unless that's the one that has the focus. I don't
> remember running into that before.
>

Jeff Johnson [MVP: VB]

2004-09-29, 3:55 pm


"Bob Butler" <tiredofit@nospam.com> wrote in message
news:ObTSTGlpEHA.3708@TK2MSFTNGP10.phx.gbl...

> No problem; I just tried it and apparently when a command button has the
> focus the KeyXXXX events are not fired for ENTER.


I'm surprised you don't remember. I've seen that issue in here before
several times, and I was about to post that suggestion but for a change I
read other replies first....

> It also doesn't fire the
> default command button unless that's the one that has the focus. I don't
> remember running into that before.


Standard Windows behavior. You'll notice the heavy "default" border will
move from a Default button to any other button that gets focus. That button
will then respond to both the Enter key and the Spacebar. Once you move
focus off of a push button the default border will return to the Default
button and Enter will activate it again...unless you're in a multiline text
box or you trigger some other exception to the rule!


Marty

2004-09-29, 9:27 pm

Finally I set all button control 's property Tabstop to False and get
the focus on my form.

Then I have the "Enter" key keypress event catched in the form's
keypress event.

Marty
Marty

2004-09-29, 9:27 pm

Thanks very much for all your help :)

Marty
Bob Butler

2004-09-29, 9:27 pm

"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:OZp5FglpEHA.1952@TK2MSFTNGP12.phx.gbl
> "Bob Butler" <tiredofit@nospam.com> wrote in message
> news:ObTSTGlpEHA.3708@TK2MSFTNGP10.phx.gbl...
>
>
> I'm surprised you don't remember. I've seen that issue in here before
> several times,


I've probably seen it before; it's just never registered. I guess I just
usually have a Default command button and don't often use the KeyXXX events
so it's not something that I run into.

> and I was about to post that suggestion but for a
> change I read other replies first....
>
>
> Standard Windows behavior. You'll notice the heavy "default" border
> will move from a Default button to any other button that gets focus.
> That button will then respond to both the Enter key and the Spacebar.
> Once you move focus off of a push button the default border will
> return to the Default button and Enter will activate it
> again...unless you're in a multiline text box or you trigger some
> other exception to the rule!


Can't say I ever looked that closely!

--
Reply to the group so all can participate
VB.Net... just say "No"

Jeff Johnson [MVP: VB]

2004-09-29, 9:27 pm


"Marty" <reply@to.forum> wrote in message
news:YwE6d.2497$eq.1932@edtnps84...

> Finally I set all button control 's property Tabstop to False and get the
> focus on my form.


Great. You have just screwed people who prefer to use the keyboard. I
SERIOUSLY hope you've at least got accelerators on these buttons.


Jeff Johnson [MVP: VB]

2004-09-29, 9:27 pm


"Bob Butler" <tiredofit@nospam.com> wrote in message
news:eARCQLmpEHA.3464@tk2msftngp13.phx.gbl...

>
> Can't say I ever looked that closely!


Not everyone is as keyboard-obsessed as I am....


John K.Eason

2004-09-29, 9:27 pm

In article <qPC6d.2450$eq.1158@edtnps84>, reply@to.forum (Marty) wrote:

> If I have another control button that get the focus, will it override
> the form's KeyPreview property?


Although it's talking about VB3, the following Knowledgebase article
still applies with VB5/6:

http://support.microsoft.com/defaul...KB;EN-US;113328

Regards
John (john@jeasonNoSpam.cix.co.uk) Remove the obvious to reply...
Marty

2004-09-29, 9:27 pm

Thanks, I'll read it!

Marty



John K.Eason wrote:
> In article <qPC6d.2450$eq.1158@edtnps84>, reply@to.forum (Marty) wrote:
>
>
>
>
> Although it's talking about VB3, the following Knowledgebase article
> still applies with VB5/6:
>
> http://support.microsoft.com/defaul...KB;EN-US;113328
>
> Regards
> John (john@jeasonNoSpam.cix.co.uk) Remove the obvious to reply...



Stefan Berglund

2004-09-30, 4:44 am

On Wed, 29 Sep 2004 18:49:36 -0400, "Jeff Johnson [MVP: VB]"
<i.get@enough.spam> wrote:
in <uCmTWanpEHA.592@TK2MSFTNGP11.phx.gbl>

>
>"Bob Butler" <tiredofit@nospam.com> wrote in message
>news:eARCQLmpEHA.3464@tk2msftngp13.phx.gbl...
>
>
>Not everyone is as keyboard-obsessed as I am....


No, but once carpal tunnel begins to set in due to excessive
mouse usage, it begins to become more apparent. :-)


---
Stefan Berglund
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com