dinput.cs

来自「Beginning C# Game Programming 的源代码」· CS 代码 · 共 45 行

CS
45
字号
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

/// <summary>
/// This class is where the DirectInput routines
/// for the application resides.
/// </summary>
public class InputClass {

	private Control owner = null;
	private Device localDevice = null;

	public InputClass(Control owner) {
		this.owner = owner;

		localDevice = new Device(SystemGuid.Keyboard);
		localDevice.SetDataFormat(DeviceDataFormat.Keyboard);
		localDevice.SetCooperativeLevel(owner, CooperativeLevelFlags.Foreground | CooperativeLevelFlags.NonExclusive);         
	}
    
	public KeyboardState GetInputState() {
		KeyboardState kbState = null;

		try {
			kbState = localDevice.GetCurrentKeyboardState();
		}
		catch(InputException) {
			do {
				Application.DoEvents();
				try{ localDevice.Acquire(); }
				catch (InputLostException) {
				  continue; }
				catch(OtherApplicationHasPriorityException) {
				  continue; }

				break;

			}while( true );
		}
		return kbState;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?