Sign in to follow this  

[MDX] DirectInput in windowed app processing keys globally

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this