Prevent Repetitive KeyDown Code From Running (vb.net)
up vote
0
down vote
favorite
If you hold down the key in a KeyDown Sub, it repeats the code until it is released.
Is there any way to prevent the code from continuously running and keep it so that it only runs once? Thanks.
vb.net
add a comment |
up vote
0
down vote
favorite
If you hold down the key in a KeyDown Sub, it repeats the code until it is released.
Is there any way to prevent the code from continuously running and keep it so that it only runs once? Thanks.
vb.net
1
On KeyDown, you could store, say, the KeyCode in a HashSet, and remove it on KeyUp. Before doing anything in the KeyDown handler, check if it is already in the HashSet and if it is then do nothing.
– Andrew Morton
Nov 10 at 21:28
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
If you hold down the key in a KeyDown Sub, it repeats the code until it is released.
Is there any way to prevent the code from continuously running and keep it so that it only runs once? Thanks.
vb.net
If you hold down the key in a KeyDown Sub, it repeats the code until it is released.
Is there any way to prevent the code from continuously running and keep it so that it only runs once? Thanks.
vb.net
vb.net
asked Nov 10 at 20:40
Dylan
81
81
1
On KeyDown, you could store, say, the KeyCode in a HashSet, and remove it on KeyUp. Before doing anything in the KeyDown handler, check if it is already in the HashSet and if it is then do nothing.
– Andrew Morton
Nov 10 at 21:28
add a comment |
1
On KeyDown, you could store, say, the KeyCode in a HashSet, and remove it on KeyUp. Before doing anything in the KeyDown handler, check if it is already in the HashSet and if it is then do nothing.
– Andrew Morton
Nov 10 at 21:28
1
1
On KeyDown, you could store, say, the KeyCode in a HashSet, and remove it on KeyUp. Before doing anything in the KeyDown handler, check if it is already in the HashSet and if it is then do nothing.
– Andrew Morton
Nov 10 at 21:28
On KeyDown, you could store, say, the KeyCode in a HashSet, and remove it on KeyUp. Before doing anything in the KeyDown handler, check if it is already in the HashSet and if it is then do nothing.
– Andrew Morton
Nov 10 at 21:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.
add a comment |
up vote
1
down vote
accepted
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.
answered Nov 10 at 22:19
Christos
2,27131830
2,27131830
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243210%2fprevent-repetitive-keydown-code-from-running-vb-net%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
On KeyDown, you could store, say, the KeyCode in a HashSet, and remove it on KeyUp. Before doing anything in the KeyDown handler, check if it is already in the HashSet and if it is then do nothing.
– Andrew Morton
Nov 10 at 21:28