DirectInput

ben_dover
06-21-2006, 07:18 AM
Hi I just found this code on some wiki website (http://gpwiki.org/index.php/DirectX:Tutorials) about DirectX9.

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???

ardman
06-22-2006, 03:09 AM
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.

ben_dover
06-26-2006, 03:19 AM
I get an exception: An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll

Form Load Event
While Me.Created
Dim controlHelperThread As New Threading.Thread(AddressOf ControlHelper)
controlHelperThread.Priority = Threading.ThreadPriority.BelowNormal
controlHelperThread.Start()
End While

My Sub
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
controlFocused = txtUpControl

Thanks.

ardman
06-26-2006, 03:25 AM
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.

ben_dover
06-26-2006, 03:47 AM
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:
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.

ardman
06-26-2006, 04:00 AM
So it goes past the State.Item(Key.A) line?

ben_dover
06-26-2006, 04:03 AM
apparently, but the form doesn't actually show, the previous one locks up for ages.

ardman
06-26-2006, 04:32 AM
Before you create anything in your Form_Load event add Me.Show(): Me.Focus()

ben_dover
06-26-2006, 06:44 AM
didn't fix it, the form partially shows and the app freezes.

ardman
06-26-2006, 07:12 AM
Ok, restructure your application. In a While Me.Created() loop call the procedure ControlHelper.

While Me.Created()
' Let Windows catch up
Application.DoEvents
End While

ben_dover
06-27-2006, 01:08 AM
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.

ardman
06-27-2006, 02:03 AM
Post your code and I'll amend that for you.

ben_dover
06-27-2006, 02:23 AM
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.

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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum