private void InitializeAsKeyboard(ScreenAccess ParentForm, InputDeviceAccess.TypeOfDevices NewTypeOfDevice) { try { this.device = new DirectInput.Device(DirectInput.SystemGuid.Keyboard); this.device.SetCooperativeLevel ( ParentForm, DirectInput.CooperativeLevelFlags.Background | DirectInput.CooperativeLevelFlags.NonExclusive ); device.Acquire(); this._TypeOfDevice = NewTypeOfDevice; //Determine how many users are suited for this device this._DeviceUserMax = 2; // Get space in the keytable for the number of device users this.KeyTable = new System.Collections.Hashtable[this._DeviceUserMax]; //record guid as used InputDeviceAccess.AssignedGuids.Add(this.device.DeviceInformation.InstanceGuid); } catch (Exception err) { throw err; } }
private void InitializeAsGamePad(ScreenAccess ParentForm, InputDeviceAccess.TypeOfDevices NewTypeOfDevice) { //Check to see if this type of device is available and try to get it for this user try { //Grab first attached gamepad that isnt already assigned foreach (DirectInput.DeviceInstance CurrDeviceInstance in DirectInput.Manager.GetDevices(DirectInput.DeviceClass.GameControl, DirectInput.EnumDevicesFlags.AttachedOnly)) { if (InputDeviceAccess.IsFreeDeviceGuid(CurrDeviceInstance.InstanceGuid) == true) { this.device = new DirectInput.Device(CurrDeviceInstance.InstanceGuid); //If this device is dead throw an exception if (this.device == null) { throw new Exception("found a gamepad GUID, but when we tried to make a device from it it was null"); } else { break; } } } //throw an exception if there is no device if (this.device == null) { throw new Exception("A gamepad was assigned as an input device, but none were found."); } //Setup device this._TypeOfDevice = NewTypeOfDevice; this.device.SetDataFormat(DirectInput.DeviceDataFormat.Joystick); this.device.SetCooperativeLevel(ParentForm, DirectInput.CooperativeLevelFlags.Background | DirectInput.CooperativeLevelFlags.NonExclusive); this.device.Properties.AxisModeAbsolute = true; this.device.Acquire(); //Determine how many users are suited for this device this._DeviceUserMax = 1; // Get space in the keytable for the number of device users this.KeyTable = new System.Collections.Hashtable[this._DeviceUserMax]; //record guid as used InputDeviceAccess.AssignedGuids.Add(this.device.DeviceInformation.InstanceGuid); //Get the keys just so PressedKeys wont be null this.PollInput(); } catch (Exception err) { throw err; } }