private bool TryFindKeyboardDeviceId(out int deviceId) { for (int i = 1; i <= 20; i++) { if (ManagedWrapper.IsKeyboard(i) <= 0) { continue; } string name = ManagedWrapper.GetHardwareStr(context, i); if (!string.IsNullOrWhiteSpace(name)) { // Console.WriteLine($"Found device\nid = {i}\nname = {name}"); deviceId = i; return(true); } } deviceId = -1; return(false); }
public void RefreshConnectedDevices() { _deviceHandleToId = new Dictionary <string, List <int> >(); _deviceReports = new List <DeviceReport>(); string handle; for (var i = 1; i < 11; i++) { if (ManagedWrapper.IsKeyboard(i) != 1) { continue; } handle = ManagedWrapper.GetHardwareStr(_deviceContext, i, 1000); if (handle == "") { continue; } int vid = 0, pid = 0; GetVidPid(handle, ref vid, ref pid); var name = ""; if (vid != 0 && pid != 0) { name = DeviceHelper.GetDeviceName(vid, pid); } if (name == "") { name = handle; } handle = $@"Keyboard\{handle}"; if (!_deviceHandleToId.ContainsKey(handle)) { _deviceHandleToId.Add(handle, new List <int>()); } var instance = _deviceHandleToId[handle].Count; _deviceHandleToId[handle].Add(i); name = $"K: {name}"; if (instance > 0) { name += $" #{instance + 1}"; } _deviceReports.Add(new DeviceReport { DeviceName = name, DeviceDescriptor = new DeviceDescriptor { DeviceHandle = handle, DeviceInstance = instance }, Nodes = new List <DeviceReportNode> { _keyboardList } }); //Log(String.Format("{0} (Keyboard) = VID: {1}, PID: {2}, Name: {3}", i, vid, pid, name)); } for (var i = 11; i < 21; i++) { if (ManagedWrapper.IsMouse(i) != 1) { continue; } handle = ManagedWrapper.GetHardwareStr(_deviceContext, i, 1000); if (handle == "") { continue; } int vid = 0, pid = 0; GetVidPid(handle, ref vid, ref pid); var name = ""; if (vid != 0 && pid != 0) { name = DeviceHelper.GetDeviceName(vid, pid); } if (name == "") { name = handle; } handle = $@"Mouse\{handle}"; if (!_deviceHandleToId.ContainsKey(handle)) { _deviceHandleToId.Add(handle, new List <int>()); } var instance = _deviceHandleToId[handle].Count; _deviceHandleToId[handle].Add(i); name = $"M: {name}"; if (instance > 0) { name += $" #{instance + 1}"; } _deviceReports.Add(new DeviceReport { DeviceName = name, DeviceDescriptor = new DeviceDescriptor { DeviceHandle = handle, DeviceInstance = instance }, Nodes = new List <DeviceReportNode> { _mouseButtonList, StaticData.MouseAxisList } }); //Log(String.Format("{0} (Mouse) = VID/PID: {1}", i, handle)); //Log(String.Format("{0} (Mouse) = VID: {1}, PID: {2}, Name: {3}", i, vid, pid, name)); } _providerReport = new ProviderReport { Title = "Interception (Core)", Description = "Supports per-device Keyboard and Mouse Input/Output, with blocking\nRequires custom driver from http://oblita.com/interception", API = "Interception", ProviderDescriptor = _providerDescriptor, Devices = _deviceReports }; }
// public static void GetVidPid(string str, ref int vid, ref int pid) // { // var matches = Regex.Matches(str, @"VID_(\w{4})&PID_(\w{4})"); // if (matches.Count <= 0 || matches[0].Groups.Count <= 1) return; // vid = Convert.ToInt32(matches[0].Groups[1].Value, 16); // pid = Convert.ToInt32(matches[0].Groups[2].Value, 16); // } public void start() { // var stroke = new ManagedWrapper.Stroke(); // stroke.key.code = (ushort)Keys.J; // stroke.key.state = (ushort)ManagedWrapper.KeyState.Down; // int devId = 1; // ManagedWrapper.Send(_deviceContext, devId, ref stroke, 1); //use this code to determine keys // ManagedWrapper.SetFilter(deviceContext, ManagedWrapper.IsKeyboard, ManagedWrapper.Filter.All); // var stroke = new ManagedWrapper.Stroke(); // int device = 0; // while (ManagedWrapper.Receive(deviceContext, device = ManagedWrapper.Wait(deviceContext), ref stroke, 1) > 0) // { // if(ManagedWrapper.IsKeyboard(device) > 0) { // Console.WriteLine(stroke.key.code); // if(stroke.key.code == 1) // break; // } // } // ManagedWrapper.SetFilter(deviceContext, ManagedWrapper.IsKeyboard, ManagedWrapper.Filter.None); // var ret = new List<DeviceInfo>(); for (var i = 1; i < 21; i++) { var handle = ManagedWrapper.GetHardwareStr(deviceContext, i, 1000); if (handle == "") { continue; } // int foundVid = 0, foundPid = 0; // GetVidPid(handle, ref foundVid, ref foundPid); //if (foundVid == 0 || foundPid == 0) continue; // Console.WriteLine(i + " " + handle); // ret.Add(new DeviceInfo {Id = i, IsMouse = i > 10, Handle = handle}); } // foreach (var device in ret) // { // Console.WriteLine(device); // } var controllers = new[] { new Controller(), new Controller(UserIndex.One), new Controller(UserIndex.Two), new Controller(UserIndex.Three), new Controller(UserIndex.Four) }; // Get 1st controller available Controller controller = controllers[0]; foreach (var selectControler in controllers) { // Console.WriteLine(selectControler); if (selectControler.IsConnected) { controller = selectControler; break; } } if (controller == null) { Console.WriteLine("No XInput controller installed"); } else { Console.WriteLine("KeyMapper loaded"); // Console.WriteLine("Found a XInput controller available"); // Console.WriteLine("Press buttons on the controller to display events or escape key to exit... "); // Poll events from joystick SharpDX.XInput.State previousControllerState = controller.GetState(); Dictionary <Button, bool> lastSimpleGamepadState = determineSimpleButtonState(previousControllerState); StateTransition lastState = null; while (!ModuleData.getInstance().cancellationToken.IsCancellationRequested) { if (controller.IsConnected) { SharpDX.XInput.State controllerState = controller.GetState(); if (previousControllerState.PacketNumber != controllerState.PacketNumber) { // Console.WriteLine(controllerState.Gamepad); Dictionary <Button, bool> simpleGamepadState = determineSimpleButtonState(controllerState); Dictionary <Button, Settings.Action> changedState = determineStateDifferences(lastSimpleGamepadState, simpleGamepadState); printStateChanges(changedState); // Console.WriteLine(GetActiveWindowTitle()); //determine if we are transitioning to a new 'state' // this is based on the current state, the game state and the keys pressed/not pressed // NOTE: the first state wins, for speed StateTransition newState = getNewState(simpleGamepadState, ModuleData.getInstance().companionSettings.stateTransitions); if (newState == null && lastState == null) { //nothing to do } else if (newState != null && lastState == null) { activateState(newState); } else if (newState == null && lastState != null) { deactivateState(lastState); } else if (newState != null && lastState != null && !newState.transitionName.Equals(lastState.transitionName)) { deactivateState(lastState); activateState(newState); } //now that we have the state name determined, load the correct mappings StateControllerMapping stateControllerMappings = findStateControllerMappings(newState, ModuleData.getInstance().companionSettings.stateMappings); //apply button presses and such foreach (Button key in changedState.Keys) { Settings.Action action = changedState[key]; foreach (ControllerMapping controllerMapping in stateControllerMappings.controllerMappings) { if (controllerMapping.button.button == key && action == controllerMapping.button.action) { handleCommand(controllerMapping); } } } lastState = newState; lastSimpleGamepadState = simpleGamepadState; } Thread.Sleep(10); previousControllerState = controllerState; } } } }