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

Form_Keypress and Return key Challenge

Using VB6..

Have a form with KeyPreview set to true.  In the Form_KeyPress event, the only key that does not seem to trigger the event (nor does it trigger Form_Keydown) is the Return (Enter) key.  When I press it, the events don't happen.  When I do Shift + Enter the events do happen.  Obviously I don't want Shift + Enter... I want just Enter.

Anyone know why?
0
alanvranian
Asked:
alanvranian
  • 7
  • 5
  • 4
  • +1
2 Solutions
 
Bob LambersonSoftware EngineerCommented:
Works ok for me. Do you have controls on the form?

Bob
0
 
alanvranianAuthor Commented:
Yes, the form is basically a keyboard for use with a touch screen, so each keyboard key is a command button, along with some other command buttons, a text box and a list control
0
 
Bob LambersonSoftware EngineerCommented:
I notice if you have code in both KeyPress and KeyDown the KeyDown fires but not the KeyPress. Is this maybe part of the problem?

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bob LambersonSoftware EngineerCommented:
Could it be that you inadvertantly have the focus on a key that has the KeyDown coded and fires, bypassing the events in the form?
0
 
alanvranianAuthor Commented:
I've tried with and without the keydown in addition to the keypress.  Whats so odd about my situation is that when I press the Enter key *neither* event fires... not keypress or keydown.  If I do a SHift + Enter then they fire... the keydown when I press the shift key, and without keydown, the keypress will fire with shift+enter.

Its almost like my enter key isn't working... yet other things are responding to it normally...

I'm usually pretty good with this stuff and only come here when its a really odd sitiation :)
0
 
Bob LambersonSoftware EngineerCommented:
I'm baffled. Have you double checked your testing method to be sure you are not missing somethin? Otherwise I'm out of ideas.

Bob
0
 
alanvranianAuthor Commented:
Whats odd is that I wrote this exact same code a couple years ago, and looked it up and made sure what I have here matches that.  It worked back then... doesn't now.  Either something in windows is different, or something in my hardware has changed, I suppose.

I wish I knew a way to trap when the enter key was pressed to see whats happening inside of windows...

For now I've coded around the problem by substituting FunctionKeys and KeyDown to trigger what I had intended to trigger when the enter key was pressed....

Odd, huh?  Thanks for the diligent effort though, Bob
0
 
appariCommented:
in the following cases return key not fires keydown event.
1. if you have a command button with its default property set to TRUE.
2. if the focus is on cpmmand button control.

0
 
appariCommented:

when focus is on commond button control and pressing enter key fires buttons click event.
when a command buttons default property is set to true on pressing enter key is equal to click the default button
0
 
gecko_au2003Commented:
I'm not sure if this is what you are looking for ( Might just be me having a stupid moment right now but ) Did you try this on the key press event :

If KeyAscii = 13 Then
MsgBox "Enter / Return key was pressed"
'Basically the code to go here that executes if the enter key is pressed
Else
End If

Anyways, let me know how that goes.
0
 
alanvranianAuthor Commented:
appari, yes, I'm aware of the  issues with the focus and default settings.  I set the focus on the space bar after each event.  I've tried setting the default to no objects, and that didn't help so I set the command button that represents the enter key as the default and thats not triggering... in fact no event that I can see triggers at all when I press the enter key with or without default set.  Its only triggering when I do a shift+Enter.

Gecko, thanks, but I'm way past that code ;)  The problem I'm having is that the enter key isn't triggering the keypress or keydown events...  only shift+enter works
0
 
alanvranianAuthor Commented:
Follow up comment to my last one... the reason that I set the focus to the space bar after each event is that the space bar will trigger the click event on a command button.  Since all my keyboard keys are represented by command buttons on this form (which is a touch screen keyboard), I have to make sure the focus is always on the space bar so that pressing the space bar triggers the click event for the button that represents the space bar.  If the focus is anywhere else, the act of pressing the space bar could trigger other odd events.  Alan
0
 
appariCommented:

i think you better try handling the key events by using API. there is some API to handle the key hooks.
i will search my library and get back to you in a few minutes.
0
 
appariCommented:

go throught this link it explains about lowlevel keyboard hooks http://www.codeguru.com/vb/gen/vb_system/keyboard/article.php/c4829

copy the code from above article(complete Code is at the end of the article).
modify the bas module little bit as follows

add the following constant to bas module
Public Const VK_RETURN = &HD

add the following code in the IsHooked function in the bas module after first 5 lines.

  If (Hookstruct.vkCode = VK_RETURN) Then

    Call HookedState(True, "Enter Key Pressed????????")
    IsHooked = True
    Exit Function
  End If


you can change     IsHooked = True  to     IsHooked = FALSE if you dont want to process the keyevents.
0
 
alanvranianAuthor Commented:
appari, thanks for this.  I'm going to try it but am swamped with on site client work for a couple days...
0
 
Bob LambersonSoftware EngineerCommented:
appari  gave a solution that will work
0
 
alanvranianAuthor Commented:
Frankly, I haven't had time to try appari's last suggestion...  just went with my work-around.  But both of you gave useful guidance and appari made the effort to find info in external sources so i've weighted the score in his favor.  But I appreciate the help from both of you very much.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now