Results 1 to 7 of 7

Thread: Keypress & Keydown Event Handling

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    SC
    Posts
    4

    Keypress & Keydown Event Handling

    I am new to VB.Net and would like to create keystroke event handling at the Form level. I understand that the Form's KeyPreview property must be set to True. I want to intercept the <Enter> key even when Button objects are on the form. I create two event handlers within the form's code something like this:

    Sub KPress(ByVal o as Object, ByVal e as KeyPressEventArgs) Handles MyBase.KeyPress

    e.Handled = True
    msgbox("Form Intercepted the KeyPress")

    End Sub

    and

    Sub KDown(ByVal o as Object, ByVal e as KeyEventArgs) Handles MyBase.KeyDown


    e.Handled = True
    msgbox("Form Intercepted the KeyDown")

    End Sub

    However, when a Button has the Focus, the Button's Click event handles the keystroke, not the Form.

    Thanks for your help...in advance,
    MarkR
    North Augusta, SC

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Isn't this helpful. . .
    VB Code:
    1. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    2.         If Asc(e.KeyChar()) = 13 Then
    3.  
    4.             'Do something
    5. e.Handled = False
    6.  
    7.         End If
    8.     End Sub
    Last edited by Pirate; Jul 19th, 2003 at 12:20 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    SC
    Posts
    4
    Thanks, Pirate, but that doesn't work if a Button object has the focus. The code that I had written works so long as the object that has the focus is NOT a button. It works for text boxes and other controls that normally allow keyboard activity, but not for Buttons. The default behaviour for a button object is, of course, to execute the OnClick event when the <Enter> key is pressed. Maybe the solution is to override the default behaviour for the button, but I don't know how.
    MarkR
    North Augusta, SC

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    As an example for overriding events , it's done this way :
    (dunno what you are trying to do though )
    VB Code:
    1. Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    2.  
    3.         MyBase.OnKeyPress(e)
    4.         If Asc(e.KeyChar()) = 13 Then
    5.             'do this
    6.         End If
    7.  
    8. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    SC
    Posts
    4
    Sorry, Pirate,

    According to your quote, I am becoming a better man every day, because I must be making alot of mistakes!! Anyway, I had already tried the overrides, but I still cannot get it to work. I have found a work-around, but am still curious as to how to do this. Are you saying that you have created a form, placed a button on the form as the only control and when the button has the focus and you press the <Enter> key, that the button's Click event does not get triggered, but the form's keypress or keydown events get triggerered instead?

    Thanks again...
    MarkR
    North Augusta, SC

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    lol ,

    I know the code I posted doesn't solve your problem . You said how to override an event in VB.NET , so basically that was only an example on how to do so . OK , I built a small demo with one form and one button , and yes it triggers the form's event but not the button's keypress . In this situation , APIs are *sometimes* more reliable than .NET ways . Did you try something with APIs ?

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    SC
    Posts
    4
    Pirate,

    Your last posting stated that you were successful, however you go on to say that APIs calls are the way to go. Were you, in fact, successful, or should the words "form" and "button" be swapped in your posting? If you were successful, then why would an API call be necessary?

    However, to answer your question...no, I have not tried any API calls. I decided to place a textbox on the form and insure that the focus always returns to that textbox control after any event. That way, the textbox will handle all key events, not the form.

    Thanks for your time
    MarkR
    North Augusta, SC

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width