Results 1 to 5 of 5

Thread: [RESOLVED] [2008] KeyDown and KeyPress events not responding

  1. #1

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Resolved [RESOLVED] [2008] KeyDown and KeyPress events not responding

    In the application I'm working on I use F1 and also a right mouse click to close the application since the graphics display full screen and there's no control in the upper right corner to close with. I use a good number of different keypresses to control things and everything works fine. When I switch away from the form that the applications starts in to a different form I'm not getting any response from either a KeyDown or a KeyPress event. Maybe someone knows what's going on. The ButtonClick and MouseDown events are responding just fine but for some reason the KeyDown and KeyPress events aren't. Below is code for one of the Forms that is unresponsive in this way. I put Close() outside the If Thens below so that any
    KeyPress or KeyDown would close but nothing happens.

    Code:
    Option Strict On
    Public Class InstructionsForRayRover
        Inherits System.Windows.Forms.Form
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TellAboutwOn = 0
            StartForm1 = 1
            mainForm = New DisplayGraphics
            Me.Close()
        End Sub
    
        Private Sub InstructionsForRayRover_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            If mainForm Is Me Then
                mainForm = Nothing
            End If
        End Sub
    
        Private Sub InstructionsForRayRover_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Close()
            If e.KeyCode = Keys.F1 Then
    
            End If
        End Sub
    
        Private Sub InstructionsForRayRover_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
            Close()
            If e.KeyChar = "a"c Then
    
            End If
        End Sub
    
        Private Sub InstructionsForRayRover_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' This is just to center the text at the top of the form. There might be a more efficient way to to this
            st2 = My.Computer.Info.OSFullName
            b = st2.IndexOf("XP") ' b will = -1 if any version of Windows XP isn't being used
            If b >= 0 Then
                a = CInt((Hxlimit - 96) / 4.07)
            Else
                a = CInt((Hxlimit - 96) / 3)
            End If
            st1 = ""
            For i = 0 To a
                st1 = st1 & " "
            Next
            Me.Text = st1 & "Instructions On Using Ray Rover"
            Label1.Left = HxlimitI - 364
            Label3.Left = HxlimitI - 364
            Button1.Left = HxlimitI - 60
            ButtonUpdate.Left = HxlimitI - 106
            Label2.Width = XlimitI - 25
        End Sub
    
        Private Sub InstructionsForRayRover_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            If MouseButtons = MouseButtons.Right Then
                Close()
            End If
        End Sub
    
    End Class
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2008] KeyDown and KeyPress events not responding

    Set the form KeyPreview property to true at design time or in your Form_Load event.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: [2008] KeyDown and KeyPress events not responding

    Thanks very much, that took care of the problem. What's interesting is that KeyPreview for my starting form was set to false but it does respond to KeyPress events and my Form_Load event doesn't specify anything about KeyPreview. Anyway problem fixed.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  4. #4
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: [2008] KeyDown and KeyPress events not responding

    Quote Originally Posted by EntityX
    Thanks very much, that took care of the problem. What's interesting is that KeyPreview for my starting form was set to false but it does respond to KeyPress events and my Form_Load event doesn't specify anything about KeyPreview. Anyway problem fixed.
    That's because as long as the focus is on your form, you don't need KeyPreview on. If you have anything that draws immediate focus, such as a Button or TextBox, then the form won't be getting it. But if your first form was just labels and such, you wouldn't need the preview on.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  5. #5

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: [RESOLVED] [2008] KeyDown and KeyPress events not responding

    That explains it. My starting form doesn't have any textbox or similar objects, just labels.
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

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