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; } }
public ControllerIdType GetNewGamepadInstance(int KeyUp, int KeyDown, int KeyLeft, int KeyRight, int KeyAttack) { System.Collections.Hashtable KeyTable = new System.Collections.Hashtable(); KeyTable[InputDeviceAccess.GameKeys.Up] = KeyUp; KeyTable[InputDeviceAccess.GameKeys.Down] = KeyDown; KeyTable[InputDeviceAccess.GameKeys.Left] = KeyLeft; KeyTable[InputDeviceAccess.GameKeys.Right] = KeyRight; KeyTable[InputDeviceAccess.GameKeys.Attack] = KeyAttack; InputDeviceAccess.TypeOfDevices TypeOfDevice = InputDeviceAccess.TypeOfDevices.Gamepad; return(this.InitializeDevice(TypeOfDevice, KeyTable)); }
public InputDeviceAccess(ScreenAccess ParentForm, InputDeviceAccess.TypeOfDevices TypeOfDevice) { if (TypeOfDevice == InputDeviceAccess.TypeOfDevices.Keyboard) { this.InitializeAsKeyboard(ParentForm, TypeOfDevice); } else if (TypeOfDevice == InputDeviceAccess.TypeOfDevices.Gamepad) { this.InitializeAsGamePad(ParentForm, TypeOfDevice); } else if (TypeOfDevice == InputDeviceAccess.TypeOfDevices.Mouse) { throw new System.Exception("mouse not yet added to InputDeviceAccess"); } }
private ControllerIdType InitializeDevice(InputDeviceAccess.TypeOfDevices TypeOfDevice, System.Collections.Hashtable NewKeyTable) { InputDeviceAccess CurrDevice = null; ControllerIdType NewControllerId = new ControllerIdType(); //If the desired device is a keyboard and another keyboard has available users // assign this user to the same keyboard. if (TypeOfDevice == InputDeviceAccess.TypeOfDevices.Keyboard) { foreach (InputDeviceAccess ExistingDevice in this.Devices) { if (ExistingDevice.TypeOfDevice == InputDeviceAccess.TypeOfDevices.Keyboard && ExistingDevice.DeviceUserCount < ExistingDevice.DeviceUserMax) { CurrDevice = ExistingDevice; } } } //create device if (CurrDevice == null) { CurrDevice = new InputDeviceAccess(this.ParentForm, TypeOfDevice); } //controller info: guid & device user id NewControllerId.DeviceGuid = CurrDevice.Guid; NewControllerId.DeviceUserId = CurrDevice.GetNextUserId(); //Save ControllerId and Device this.ControllerIds.Add(NewControllerId); this.Devices.Add(CurrDevice); //Send device new keytable CurrDevice.SetKeyTable(NewKeyTable, NewControllerId.DeviceUserId); //Poll the device to give it some keys to check CurrDevice.PollInput(); return(NewControllerId); }