Computer Hope
Software => Computer programming => Topic started by: gamerx365 on February 01, 2009, 07:31:33 PM
-
I need to put keyboard input into my program, mostly letters and numbers. I have been using "If GetAsyncKeyState(65) Then" under a time but it's definitely not going to work out on this one. Sometimes I get a short delay and sometimes i get more than one instance. Anybody know how to fix this easily?
Thank you!
-
just use the Key_Press event?
-
ok. can i have an example quick please? i never learned that one.
-
sure.
Public Class Form1
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
MsgBox(e.KeyChar)
End Sub
End Class
With this, when you press a keyboard key, it puts it in a messagebox ;D
Good luck.
-
Thanx. Do I need an imports line or anything? I get the error : "Event Keypress can not be found" under the last part of the private sub
-
you are using 2008? I am not using any imports statments, however, you could try Imports System.Windows.Forms
-
System.Windows.Forms.KeyPressEventArgs
Ye, it's using System.Windows.Forms so you need to add that line at the top of VB.
-
Ive tried them and I'm still getting the same error. Anything else?
-
where are you pasting it?
-
tried top line, and second line.
-
what file.... Is there anything in the file already?
-
A couple lines of the old code and one section of the new... like this:
'Imports System.Windows.Forms.KeyPressEventArgs
Imports System.Windows.Forms
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal keyCode As Integer) As Short
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'If GetAsyncKeyState(90) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_1.wav", AudioPlayMode.Background)
If GetAsyncKeyState(88) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_2.wav", AudioPlayMode.Background)
If GetAsyncKeyState(67) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_3.wav", AudioPlayMode.Background)
If GetAsyncKeyState(65) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_4.wav", AudioPlayMode.Background)
If GetAsyncKeyState(83) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_5.wav", AudioPlayMode.Background)
If GetAsyncKeyState(68) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_6.wav", AudioPlayMode.Background)
If GetAsyncKeyState(70) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_7.wav", AudioPlayMode.Background)
If GetAsyncKeyState(71) Then My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_8.wav", AudioPlayMode.Background)
End Sub
Public Class Form1
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal z As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
My.Computer.Audio.Play("C:\Documents and Settings\HP_Owner\Desktop\Samples\drum_1.wav", AudioPlayMode.Background)
End Sub
End Class
End Class
-
don't paste the "Public Class Form1" or "End Class". Just the Sub.
-
oh lol. i'm dumb. I don't even know how that got there. thanx lol.
-
well i read what was above and how you pick out the left, right, up, down arrows??
-
oh nvm i got it now. i found a list of characters
-
Please, in future, post your own question. Thanks