|
-
Aug 17th, 2009, 04:18 PM
#1
Thread Starter
New Member
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
-
Aug 17th, 2009, 04:32 PM
#2
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?
-
Aug 17th, 2009, 05:53 PM
#3
Re: KeyDown for spacebar
If you register the Spacebar as a hotkey then it will no longer do that.
-
Aug 17th, 2009, 06:59 PM
#4
Thread Starter
New Member
Re: KeyDown for spacebar
 Originally Posted by Vectris
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
-
Aug 17th, 2009, 07:16 PM
#5
Re: KeyDown for spacebar
vb.net Code:
Public Class Form1 Public Sub New() Me.InitializeComponent() Me.KeyPreview = True End Sub Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown If e.KeyCode = Keys.Space Then e.SuppressKeyPress = True End If End Sub Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Space Then MessageBox.Show("Toggle start/stop") End If End Sub 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.
-
Aug 17th, 2009, 07:32 PM
#6
Thread Starter
New Member
Re: KeyDown for spacebar
 Originally Posted by ForumAccount
vb.net Code:
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Me.KeyPreview = True
End Sub
Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
If e.KeyCode = Keys.Space Then
e.SuppressKeyPress = True
End If
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Space Then
MessageBox.Show("Toggle start/stop")
End If
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|