Computer Hope

Software => Computer programming => Topic started by: gamerx365 on February 01, 2009, 07:31:33 PM

Title: VB express 2008 keyboard input.
Post 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!
Title: Re: VB express 2008 keyboard input.
Post by: Bones92 on February 03, 2009, 09:19:44 PM
just use the Key_Press event?
Title: Re: VB express 2008 keyboard input.
Post by: gamerx365 on February 04, 2009, 04:49:13 AM
ok. can i have an example quick please? i never learned that one.
Title: Re: VB express 2008 keyboard input.
Post by: Bones92 on February 04, 2009, 07:51:18 PM
sure.

Code: [Select]
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.
Title: Re: VB express 2008 keyboard input.
Post by: gamerx365 on February 05, 2009, 07:52:42 PM
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
Title: Re: VB express 2008 keyboard input.
Post by: Bones92 on February 05, 2009, 08:38:27 PM
you are using 2008? I am not using any imports statments, however, you could try Imports System.Windows.Forms
Title: Re: VB express 2008 keyboard input.
Post by: ultimatum on February 06, 2009, 08:09:21 PM
Quote
System.Windows.Forms.KeyPressEventArgs

Ye, it's using System.Windows.Forms so you need to add that line at the top of VB.
Title: Re: VB express 2008 keyboard input.
Post by: gamerx365 on February 08, 2009, 04:07:42 PM
Ive tried them and I'm still getting the same error. Anything else?
Title: Re: VB express 2008 keyboard input.
Post by: BC_Programmer on February 08, 2009, 04:16:30 PM
where are you pasting it?

Title: Re: VB express 2008 keyboard input.
Post by: gamerx365 on February 08, 2009, 04:37:41 PM
tried top line, and second line.
Title: Re: VB express 2008 keyboard input.
Post by: BC_Programmer on February 08, 2009, 04:38:30 PM
what file.... Is there anything in the file already?
Title: Re: VB express 2008 keyboard input.
Post by: gamerx365 on February 08, 2009, 05:33:20 PM
A couple lines of the old code and one section of the new... like this:

Code: [Select]
'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
Title: Re: VB express 2008 keyboard input.
Post by: BC_Programmer on February 08, 2009, 06:18:36 PM
don't paste the "Public Class Form1" or "End Class". Just the Sub.
Title: Re: VB express 2008 keyboard input.
Post by: gamerx365 on February 08, 2009, 06:39:03 PM
oh lol. i'm dumb. I don't even know how that got there. thanx lol.
Title: Re: VB express 2008 keyboard input.
Post by: JokermanVB on March 20, 2009, 03:31:24 PM
well i read what was above and how you pick out the left, right, up, down arrows??
Title: Re: VB express 2008 keyboard input.
Post by: JokermanVB on March 20, 2009, 05:38:15 PM
oh nvm i got it now. i found a list of characters
Title: Re: VB express 2008 keyboard input.
Post by: Bones92 on March 23, 2009, 02:52:45 AM
Please, in future, post your own question. Thanks