[PC VB6] Keyboard issues

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

[PC VB6] Keyboard issues

Post by thegamefreak0134 »

I am working on a project in visual basic, and want to use DirectInput to read the keyboard. Using a tutorial I found online, I constructed a class to get the job done. However, I'm apparently supposed to pass it an argument to start it up, and I can't figure out what I need to give it. This is the New() constructor for the class:

Code: Select all

Public Sub New(ByVal ContainerWindow As Windows.Forms.Control)
        Try
            Dev = New Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Keyboard)
            Dev.SetCooperativeLevel(ContainerWindow, COOPLEVELKB)
            Dev.Acquire()
            KeyboardAquired = True
        Catch
            Console.Write("Could not acquire keyboard:")
                'Console.Write(e.Message)
            If Not (Dev Is Nothing) Then Dev.Dispose()
            Dev = Nothing
            KeyboardAquired = False
        End Try
        End Sub
As you can see, I'm supposed to give it a container window of sorts. Of course, being a little new at classes in general (although understanding them fine) I don't know what a container window is or how I would access it to pass it on to the class. Help?

-thegamefreak
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

DirectX requires a window handle to operate when you're initialising - it should be enough to specify 'this' (Me in VB?) when intialising from a form.

Do note, however, that DirectInput is not really the way to do it any more. Just use the KeyDown and KeyUp events generated by the Form instead - far, far easier, and the 'right way' to do it.
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

Post by thegamefreak0134 »

Would love to, but can't. I have a timer calling an event every 1/1000 of a milisecond (just so it keeps going) and thus the keyDown and keyUp events are never firing. (This is a game, of course.) I will try this. Thanks!

*EDIT* well the defines appear to work alright now, but I can't get the keys to work. I'm using directInput, and have set the container window to Me, which I believe should be the only window in the program. However, whenever I call a poll function, it's returning false meaning that it is unable to aqquire the keyboard. Is there something else I need to do? Like initialize directX or something to that effect?
Last edited by thegamefreak0134 on Fri 19 May, 2006 4:48 pm, edited 1 time in total.
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

1/1000 of a millisecond? Timers are only accurate to ~10ms at best under Windows unless you start using the multimedia timers (and even those are, at best, 1ms).

I assume you are calling Application.DoEvents() in your CPU-intensive loop? This forces your application to process the message queue, which contains (amongst other things) the key press/release messages from Windows. Without it, your application won't process these messages and as such won't respond to keys. (DirectInput is a wrapper around these messages so probably won't work either).
User avatar
thegamefreak0134
Extreme Poster
Posts: 455
Joined: Mon 23 Jan, 2006 10:09 pm
Location: In front of a Computer, coding
Contact:

Post by thegamefreak0134 »

Well that's downright usefull sounding. Thanks! I've been teaching myself VB.NET for a couple of years now. It's easy enough, but it means that basic concepts like this sometimes throw me.
I'm not mad, just a little crazy.

DarkNova - a little side project I run.
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

You can, if it takes your fancy, manually handle window messages by overriding the Form's base method WndProc (which will pass a reference to a message). If you download a useful tool called API Viewer 2004 (an extension of VB6's API viewer) you can copy and paste the defines required into your VB.NET source.

KeyDown and KeyUp are, for the most part, sufficient though for keys.
Post Reply