|
-
Feb 4th, 2010, 10:21 PM
#1
Thread Starter
Lively Member
Check if a key is currently pressed?
How would I check if a key is currently being pressed?
Also how would I detect if a key is pressed without using the KeyDown event. The reason I am not using the keydown event is when the user holds down a key it calls the event, waits a second than keeps calling it. I do not want the wait period if a user is holding down a key.
-
Feb 4th, 2010, 10:31 PM
#2
Re: Check if a key is currently pressed?
The KeyDown event is what detects when a key is depressed so that's what you use. If you don't want to detect multiple events then you simply add a bit of logic to ignore all but the first one:
vb.net Code:
Public Class Form1 Private ignoreKeyDown As Boolean = False Private Sub Form1_KeyDown(ByVal sender As Object, _ ByVal e As KeyEventArgs) Handles Me.KeyDown If Not Me.ignoreKeyDown Then Me.ignoreKeyDown = True 'Process the key here. End If End Sub Private Sub Form1_KeyUp(ByVal sender As Object, _ ByVal e As KeyEventArgs) Handles Me.KeyUp Me.ignoreKeyDown = False End Sub End Class
As for the first question, I'm not aware of a way to do that with managed code. I think you'd have to use the GetAsyncKeyState function from the Windows API. If you search the forum you will undoubtedly find examples.
-
Feb 4th, 2010, 10:38 PM
#3
Thread Starter
Lively Member
Re: Check if a key is currently pressed?
Sorry what I meant was I do want it to detect multiple events. I just do not want that wait period in between the detection. Like how you hold down a key in a chat box it prints the key, than waits a second and continues printing the key if it is still pressed. I just don't want that one second wait.
-
Feb 4th, 2010, 11:03 PM
#4
Re: Check if a key is currently pressed?
Use this declaration at the top of your form:
Code:
Private Declare Function GetKeyState Lib "user32" (ByVal keyCode As Integer) As Integer
Then create a Timer with an interval of 1 and use this in the Tick event.
Code:
If GetKeyState(Keys.Left) < 0 Then 'Do Stuff
Keys.Left can be any key you want and whatever action is in 'Do Stuff will happen the instant the key is pressed and will not pause to repeat.
-
Feb 4th, 2010, 11:04 PM
#5
Re: Check if a key is currently pressed?
Sorry what I meant was I do want it to detect multiple events. I just do not want that wait period in between the detection. Like how you hold down a key in a chat box it prints the key, than waits a second and continues printing the key if it is still pressed. I just don't want that one second wait.
In that case you'd have to ignore system settings, because that delay is part of Windows. It helps prevent people accidentally registering multiple key presses if they're a bit slow to release the key.
Start a Timer on the KeyDown event and then Stop it on the KeyUp event, then handle the Tick event of your Timer.
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
|