DirectInput
DirectInput
DirectInput
DirectInput
DirectInput
DirectInput DirectInput DirectInput DirectInput DirectInput DirectInput DirectInput DirectInput
DirectInput DirectInput
DirectInput
Go Back  Xtreme Visual Basic Talk > > > > DirectInput


Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2006, 07:18 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default DirectInput


Hi I just found this code on some wiki website (http://gpwiki.org/index.php/DirectX:Tutorials) about DirectX9.

Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput.CooperativeLevelFlags
Imports Microsoft.DirectX.DirectInput
 
Public Class Form1
    Inherits System.Windows.Forms.Form
 
    Dim KB As New DirectInput.Device(DirectInput.SystemGuid.Keyboard)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KB.SetCooperativeLevel(Me, Background Or NonExclusive)
        Me.Show()
        KB.Acquire()
    End Sub
 
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        KB.Dispose()
        KB = Nothing
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim State As DirectInput.KeyboardState
        State = KB.GetCurrentKeyboardState
 
        If State.Item(Key.Escape) Then Me.Close()
        If State.Item(Key.A) Then Label1.Text &= "a"
        If State.Item(Key.B) Then Label1.Text &= "b"
        If State.Item(Key.C) Then Label1.Text &= "c"
 
    End Sub
End Class
Now I have a form which is going to be where the user determines their control settings. This form has a few textboxes and 2 command buttons. I'd like to change the above code so it depends on what textbox currently has focus (that the text changes). I've tried putting the code under the timer in a KeyDown event for the textbox and it doesn't work.

Please help, how can I get this to do what I want???
Reply With Quote
  #2  
Old 06-22-2006, 03:09 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

Using a Timer won't work unless you are holding the key down and the you'll only get those that will work when the Timer event is fired.

You'll have to write your own sub which will allow you to obtain the Keyboard State and also get which Text box has focus too. Put a While Me.Created() loop in your Form_Load event which calls this new sub and you should be fine.
__________________
Cheers
Reply With Quote
  #3  
Old 06-26-2006, 03:19 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default

I get an exception:
Quote:
An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
Form Load Event
Code:
While Me.Created
            Dim controlHelperThread As New Threading.Thread(AddressOf ControlHelper)
            controlHelperThread.Priority = Threading.ThreadPriority.BelowNormal
            controlHelperThread.Start()
        End While
My Sub
Code:
    Public Sub ControlHelper()
        Dim State As DirectInput.KeyboardState
        State = KB.GetCurrentKeyboardState
        'If State.Item(Key.Escape) Then Me.Close()
        If State.Item(Key.A) Then controlFocused.Text &= "A"
        If State.Item(Key.B) Then controlFocused.Text &= "B"
        If State.Item(Key.C) Then controlFocused.Text &= "C"
        'etc etc
    End Sub
Control_GotFocus Events
Code:
controlFocused = txtUpControl
Thanks.
Reply With Quote
  #4  
Old 06-26-2006, 03:25 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

You are creating multiple threads in your Form_Load event. You keep creating a new thread while the application is running. Cannot be good for your application. Remove the creation of the thread from the While Loop. You shouldn't need to have a While Me.Created() loop.
__________________
Cheers
Reply With Quote
  #5  
Old 06-26-2006, 03:47 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default

Ok well I've just made it call the Sub, I put it on a new thread because the program seemed to have froze and the debugger wouldn't come up, didn't realise that it would keep spawning threads.

Anyway I get this exception now:
Quote:
An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll

Additional information: Object variable or With block variable not set.
And "If kbState.Item(Key.B) Then" is the code highlighted in the break.
Reply With Quote
  #6  
Old 06-26-2006, 04:00 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

So it goes past the State.Item(Key.A) line?
__________________
Cheers
Reply With Quote
  #7  
Old 06-26-2006, 04:03 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default

apparently, but the form doesn't actually show, the previous one locks up for ages.
Reply With Quote
  #8  
Old 06-26-2006, 04:32 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

Before you create anything in your Form_Load event add Me.Show(): Me.Focus()
__________________
Cheers
Reply With Quote
  #9  
Old 06-26-2006, 06:44 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default

didn't fix it, the form partially shows and the app freezes.
Reply With Quote
  #10  
Old 06-26-2006, 07:12 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

Ok, restructure your application. In a While Me.Created() loop call the procedure ControlHelper.
Code:
While Me.Created()
    ' Let Windows catch up
    Application.DoEvents
End While
__________________
Cheers
Reply With Quote
  #11  
Old 06-27-2006, 01:08 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default

Ok I'm totally lost, is it possible you could make a quick app with just 1 form in it that shows me what I have to do please?

It doesn't need everything maybe just 2 textboxes, enough to get me started, thanks.

Last edited by ben_dover; 06-27-2006 at 01:41 AM.
Reply With Quote
  #12  
Old 06-27-2006, 02:03 AM
ardman ardman is offline
Contributor
 
Join Date: Jan 2002
Posts: 126
Default

Post your code and I'll amend that for you.
__________________
Cheers
Reply With Quote
  #13  
Old 06-27-2006, 02:23 AM
ben_dover ben_dover is offline
Freshman
 
Join Date: Apr 2006
Location: Australia
Posts: 38
Default

Ok.. but I kinda tried a different approach (which didn't work) and I've removed most of the code. Also I'll leave out unrelated code obviously.

Code:
Public Class frmControls
    Inherits System.Windows.Forms.Form
    Private controlFocused
    Private kbState As DirectInput.KeyboardState
    Dim KB As New DirectInput.Device(DirectInput.SystemGuid.Keyboard)
    Private Sub frmControls_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KB.SetCooperativeLevel(Me, NonExclusive Or Background)
        KB.Acquire()
    End Sub
    Private Sub frmControls_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        KB.Dispose()
        KB = Nothing
        Application.Exit()
    End Sub
Yeah as you can see I have removed most of it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DirectInput
DirectInput
DirectInput DirectInput
DirectInput
DirectInput
DirectInput DirectInput DirectInput DirectInput DirectInput DirectInput DirectInput
DirectInput
DirectInput
 
DirectInput
DirectInput
 
-->