Results 1 to 6 of 6

Thread: KeyDown for spacebar

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    15

    KeyDown for spacebar

    Hi all,

    I am having trouble using keydown for the spacebar in a form.

    I would like the spacebar to start/stop a StopWatch that I have running. I can get that to work fine but whenever I am focused on another button it also executes that buttons function also.

    I know that is the default behavior for the enter key and spacebar but I would like to override that.

    Could someone point me in the right direction?


    Thanks in advance,
    Major

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: KeyDown for spacebar

    I cant remember if this is a WPF specific thing but in the keydown event of your form can you set e.Handled to True or does that property not even exist?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: KeyDown for spacebar

    If you register the Spacebar as a hotkey then it will no longer do that.
    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
    New Member
    Join Date
    Feb 2009
    Posts
    15

    Re: KeyDown for spacebar

    Quote Originally Posted by Vectris View Post
    If you register the Spacebar as a hotkey then it will no longer do that.
    What do you mean by register?

    This is what I have:

    Code:
        Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    
            If e.KeyCode = Keys.Space Then
                swGame.stop()
            End If
    
        End Sub
    However, when I click another button and then press the spacebar it stops the clock but also 'presses' the button.


    Major

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: KeyDown for spacebar

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.         Me.InitializeComponent()
    5.         Me.KeyPreview = True
    6.     End Sub
    7.  
    8.     Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
    9.         If e.KeyCode = Keys.Space Then
    10.             e.SuppressKeyPress = True
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    15.         If e.KeyCode = Keys.Space Then
    16.             MessageBox.Show("Toggle start/stop")
    17.         End If
    18.     End Sub
    19.  
    20. End Class

    This would stop it, however it puts the Button into a depressed state, although it doesn't get clicked. Not sure why it does that.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    15

    Re: KeyDown for spacebar

    Quote Originally Posted by ForumAccount View Post
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.         Me.InitializeComponent()
    5.         Me.KeyPreview = True
    6.     End Sub
    7.  
    8.     Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
    9.         If e.KeyCode = Keys.Space Then
    10.             e.SuppressKeyPress = True
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    15.         If e.KeyCode = Keys.Space Then
    16.             MessageBox.Show("Toggle start/stop")
    17.         End If
    18.     End Sub
    19.  
    20. End Class

    This would stop it, however it puts the Button into a depressed state, although it doesn't get clicked. Not sure why it does that.
    Ok, I'll try that, thanks!


    Major

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