Results 1 to 10 of 10

Thread: [RESOLVED] Can't detect keypresses when a button is present

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    35

    Resolved [RESOLVED] Can't detect keypresses when a button is present

    I've started building a small game where you move around a PictureBox control with keystrokes using the arrow keys. Everything was working fine....until I added a button.

    Now the button steals the focus so that every time I hit an arrow key hoping to move my image, it just selects the button.

    Is there any way to make the button selectable only by mouse click??

    Thanks in advance for any help!!!

    BTW - I'm using VB and VS2008.

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Can't detect keypresses when a button is present

    I dont completely understand your problem, but do you use a Key event to determine what the arrow keys do? If so, you should be able to override that behaviour. What is your KeyDown/Up/Press code?

    If I do this for example on a form with a myriad of controls, I dont lose the function of the keys;
    Code:
       Private Sub Form1_KeyDown(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles MyBase.KeyDown
            'Note: I have Me.KeyPreview = True
            Select Case e.KeyCode
                Case Keys.Left : PictureBox1.Left -= 1
                Case Keys.Right : PictureBox1.Left += 1
                Case Keys.Up : PictureBox1.Top -= 1
                Case Keys.Down : PictureBox1.Top += 1
            End Select
        End Sub
    Last edited by Bulldog; Mar 31st, 2009 at 05:16 PM.

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

    Re: Can't detect keypresses when a button is present

    You can't make the button not focusable, but if you set your Forms KeyPreview property to True, then the your Form1 KeyDown event will still work even if the button has the focus.
    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)

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    35

    Re: Can't detect keypresses when a button is present

    That's exactly how mine is coded and it works when the button control is not on the form. As soon as I add the button though, the key down event never gets handled.

    When the form loads, the button is not selected, but when I hit one of the arrow keys, the button becomes selected and is highlighted. The arrow keys are acting almost like a Tab key press - wanting to cycle through the button controls. In fact, I added a second button and every time I hit the right arrow, it selects the other button.

    I've tried every property I thought would work, and no luck... It seems like there would be a simple answer to this, but I'm not finding it.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    35

    Re: Can't detect keypresses when a button is present

    Thanks Vectris - I just saw your post and tested that, but unfortunately it still doesn't work.

    To make things simple, I started a new project, added 2 buttons to the form, and then set the form KeyDown event to cause a message box to pop up when the right arrow key is pressed. Unfortunately, even with the form KeyPreview property set to True, pressing the right arrow key just cycles through the 2 buttons.

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Can't detect keypresses when a button is present

    I can reproduce that problem, but this seems to work as a fix;

    Code:
        Protected Overrides Function ProcessCmdKey( _
        ByRef msg As System.Windows.Forms.Message, _
        ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.Left
                    PictureBox1.Left -= 1
                Case Keys.Right
                    PictureBox1.Left += 1
                Case Keys.Up
                    PictureBox1.Top -= 1
                Case Keys.Down
                    PictureBox1.Top += 1
            End Select
            PictureBox1.Focus()
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function

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

    Re: Can't detect keypresses when a button is present

    Just turn the TabStop property of all your buttons to False and keep KeyPreview on. It worked for me with 5 buttons, no coding necessary .
    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)

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Can't detect keypresses when a button is present

    Didnt work for me. I created a new project as the OP described and get exactly the same problem. Setting tabstops didnt help.

    This was my test code, I had the PictureBox and two buttons on the form;
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.KeyPreview = True
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As System.Object, _
         ByVal e As System.Windows.Forms.KeyEventArgs) _
         Handles MyBase.KeyDown
            Select Case e.KeyCode
                Case Keys.Left : PictureBox1.Left -= 1
                Case Keys.Right : PictureBox1.Left += 1
                Case Keys.Up : PictureBox1.Top -= 1
                Case Keys.Down : PictureBox1.Top += 1
            End Select
        End Sub
    
    End Class
    This code will work if I add a TextBox.

    If I remove the TextBox, I can only fix it using this code instead of the KeyDown event;
    Code:
        Protected Overrides Function ProcessCmdKey( _
        ByRef msg As System.Windows.Forms.Message, _
        ByVal keyData As System.Windows.Forms.Keys) As Boolean
            Select Case keyData
                Case Keys.Left
                    PictureBox1.Left -= 1
                Case Keys.Right
                    PictureBox1.Left += 1
                Case Keys.Up
                    PictureBox1.Top -= 1
                Case Keys.Down
                    PictureBox1.Top += 1
            End Select
            PictureBox1.Focus()
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function

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

    Re: Can't detect keypresses when a button is present

    Ya I had a textbox on my form, I guess that messed it up.

    There is one other way though, don't use buttons. Instead make labels and you can even make them look better by customizing the MouseOver backcolor etc. That way would be harder than BullDogs though, so his is probably the best.
    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)

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2006
    Posts
    35

    Re: Can't detect keypresses when a button is present

    Thanks Bulldog - I just tested your solution out and it works great! I would've never gotten that on my own. I appreciate it!

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