directinput not working!

I'm trying to get directinput working but for some reason it just does not work.

Why doesn't the following code cause any visible results in the textbox keyboardData? I had a previous program i wrote that used directinput,, but it was much more complex and for the most part i just copied the basic idea behind what i did then and that works, but for some reason this doesn't work :(

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DXInput = Microsoft.DirectX.DirectInput;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectInput
{
public partial class Form1 : Form
{
public DXInput.Device myKeyboardDevice;

public Form1()
{
InitializeComponent();
InitializeKeyboard();
}

public void InitializeKeyboard()
{
myKeyboardDevice = new DXInput.Device(DXInput.SystemGuid.Keyboard);
myKeyboardDevice.SetCooperativeLevel(this, DXInput.CooperativeLevelFlags.Background | DXInput.CooperativeLevelFlags.NonExclusive);
myKeyboardDevice.Acquire();
}

public void ReadKeyboard()
{
DXInput.KeyboardState keys = myKeyboardDevice.GetCurrentKeyboardState();

if (keys[DXInput.Key.T])
keyboardData.Text = "T";
}

protected override void OnPaint(PaintEventArgs e)
{
ReadKeyboard();
}
}
}

And here is the main program file:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace DirectInput
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Any idea what is wrong, this has been bugging me for days now, and i just can't fix it.

[2094 byte] By [Ceres629] at [2007-12-25]
# 1

You are polling for keyboard input in the OnPaint method. How do you know if OnPaint method is called at all ? Doing something else than painting in the OnPaint method is a bad idea because you have no way of knowing when and if the system will call it. You should use a Timer to check for keyboard input from DInput.

MikeDanes at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 2

i set a break point to make sure the program calls the readkeyboard function and it does call it.

I also tried setting a timer but that didn't work.

Ceres629 at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 3

Setting a breakpoint and seeing that is hit once is not a guarantee that under normal usage OnPaint will be called periodically. OnPaint will be called once when the form is shown and then it will only be called if the window is resized or it is invalidated. That won't happen periodically.

I tested with a timer and it works fine. Have you enabled the timer ?

MikeDanes at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 4

Thanks a lot, the solution and problem was exactly as you mentioned. I forgot OnPaint won't be called untill the window is invalidated, and I did forget to enable the timer!

Everything works now.

Ceres629 at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...