[MDX] DirectInput in windowed app processing keys globally

Started by
3 comments, last by Dave Hunt 18 years ago
In my windowed app, input is still processed regardless of which window has focus. I can type in notepad while the app is in the background and the keys will still be processed.

using Microsoft.DirectX.DirectInput;

class Input
{
	Device deviceKeyboard;
	KeyboardState keyState;

	public Input(System.Windows.Forms.Form window)
	{
		deviceKeyboard = new Device(SystemGuid.Keyboard);
		deviceKeyboard.SetDataFormat(DeviceDataFormat.Keyboard);
		deviceKeyboard.SetCooperativeLevel(window.Handle, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
		deviceKeyboard.Acquire();
	}

	public void Update()
	{
		keyState = deviceKeyboard.CurrentKeyboardState;
	}

	public bool GetKey(Key key)
	{
		return keyState[key];
	}

	public void Dispose()
	{
		deviceKeyboard.Unacquire();
		deviceKeyboard.Dispose();
	}
}

I tried different combinations of CooperativeFlags but they produced errors.
Advertisement
You should use CooperativeLevelFlags.Foreground | CooperativeLevelFlags.NonExclusive and make sure that your window is shown before attempting to acquire the device for the first time.
That works, I wasn't showing the form before trying to aquire. There's a new problem now though. When the window loses focus, I get an "InputLostException" because the main loop still tries to update the keyboard state.
On a tangent, you can sometimes get around .Net "window must be visible before using control" by doing Control.CreateControl() immediately before you require a valid handle.

(This is useful in places where it's extremely cumbersome to have to show the control first).
Put a try/catch around your keyboard update. If you catch a InputLostException, try an Acquire. If the Acquire succeeds, attempt another keyboard update. If either fails, just return without reading anything. Once the window regains focus, the acquire should succeed and all will be good.

This topic is closed to new replies.

Advertisement