public static List <BindingUpdate> StrokeToMouseMove(ManagedWrapper.Stroke stroke) { var bindingUpdates = new List <BindingUpdate>(); if (stroke.mouse.x != 0) { bindingUpdates.Add(new BindingUpdate { Binding = new BindingDescriptor { Index = 0, SubIndex = 0 }, Value = stroke.mouse.x }); } if (stroke.mouse.y != 0) { bindingUpdates.Add(new BindingUpdate { Binding = new BindingDescriptor { Index = 1, SubIndex = 0 }, Value = stroke.mouse.y }); } return(bindingUpdates); }
/// <summary> /// Sends Relative Mouse Movement /// </summary> /// <param name="x">X movement</param> /// <param name="y">Y movement</param> /// <returns></returns> public void SendMouseMoveRelative(int x, int y) { var stroke = new ManagedWrapper.Stroke { mouse = { x = x, y = y, flags = (ushort)ManagedWrapper.MouseFlag.MouseMoveRelative } }; ManagedWrapper.Send(DeviceContext, DeviceId, ref stroke, 1); }
public static ManagedWrapper.Stroke ToStroke(this K key) { var stroke = new ManagedWrapper.Stroke(); ushort keyCode = (ushort)key; if (keyCode >= 256) { keyCode -= 256; stroke.key.state = 2; } stroke.key.code = keyCode; return(stroke); }
public void MmbPress() { var stroke = new ManagedWrapper.Stroke { mouse = new ManagedWrapper.MouseStroke { state = 16 } }; var res = HelperFunctions.StrokeToMouseButtonAndState(stroke); res.Should().BeEquivalentTo( HelperFunctions.ButtonStateLookupTable[16] ); }
public void Xb2Release() { var stroke = new ManagedWrapper.Stroke { mouse = new ManagedWrapper.MouseStroke { state = 512 } }; var res = HelperFunctions.StrokeToMouseButtonAndState(stroke); res.Should().BeEquivalentTo( HelperFunctions.ButtonStateLookupTable[512] ); }
public void ReleaseLeftAndRightMouse() { var stroke = new ManagedWrapper.Stroke { mouse = new ManagedWrapper.MouseStroke { state = 10 } }; var res = HelperFunctions.StrokeToMouseButtonAndState(stroke); res.Should().BeEquivalentTo( HelperFunctions.ButtonStateLookupTable[2], HelperFunctions.ButtonStateLookupTable[8] ); }
public static ButtonState[] StrokeToMouseButtonAndState(ManagedWrapper.Stroke stroke) { int state = stroke.mouse.state; // Buttons var buttonStates = new List <ButtonState>(); foreach (var buttonState in ButtonStateLookupTable) { if (state < buttonState.Key) { break; } if ((state & buttonState.Key) != buttonState.Key) { continue; } buttonStates.Add(buttonState.Value); state -= buttonState.Key; } // Wheel if ((state & 0x400) == 0x400) // Wheel up / down { buttonStates.Add( new ButtonState { Button = (ushort)(stroke.mouse.rolling > 0 ? 5 : 6), State = 1 } ); } else if ((state & 0x800) == 0x800) // Wheel left / right { buttonStates.Add( new ButtonState { Button = (ushort)(stroke.mouse.rolling > 0 ? 8 : 7), State = 1 } ); } return(buttonStates.ToArray()); }
public void LmbPressAndWheelUp() { var stroke = new ManagedWrapper.Stroke { mouse = new ManagedWrapper.MouseStroke { state = 1025 } }; var res = HelperFunctions.StrokeToMouseButtonAndState(stroke); res.Should().BeEquivalentTo( HelperFunctions.ButtonStateLookupTable[1], new HelperFunctions.ButtonState { Button = 5, State = 1 } ); }
public void WheelRight() { var stroke = new ManagedWrapper.Stroke { mouse = new ManagedWrapper.MouseStroke { state = 2048, rolling = 1 } }; var res = HelperFunctions.StrokeToMouseButtonAndState(stroke); res.Should().BeEquivalentTo( new HelperFunctions.ButtonState { Button = 6, State = 1 } ); }
private void handleKeyPress(KeyPress kp) { var stroke = new ManagedWrapper.Stroke(); stroke.key.code = (ushort)kp.key; if (kp.action == Settings.Action.PRESSED) { stroke.key.state = (ushort)ManagedWrapper.KeyState.Down; } else { stroke.key.state = (ushort)ManagedWrapper.KeyState.Up; } int devId = 4; // Console.WriteLine(stroke.key.code + " " + kp.action); ManagedWrapper.Send(deviceContext, devId, ref stroke, 1); System.Threading.Thread.Sleep(10); }
public void EchoInput() { Console.WriteLine("Echo mode activated. Press Ctrl-C to exit."); Console.WriteLine("code\tstate\tname"); ManagedWrapper.SetFilter(context, ManagedWrapper.IsKeyboard, ManagedWrapper.Filter.All); try { while (true) { int device; var stroke = new ManagedWrapper.Stroke(); if (ManagedWrapper.Receive(context, device = ManagedWrapper.WaitWithTimeout(context, 5), ref stroke, 1) > 0) { if (ManagedWrapper.IsKeyboard(device) > 0) { int scancode = stroke.key.code; if ((stroke.key.state & 2) != 0) { scancode += 256; } Console.WriteLine($"{stroke.key.code}\t" + $"{(ManagedWrapper.KeyState) stroke.key.state}\t" + $"{KeyNameHelper.GetNameFromScanCode(scancode)}"); ManagedWrapper.Send(context, device, ref stroke, 1); } } } } catch (Exception e) { Console.Error.WriteLine(e); } }
public ManagedWrapper.Stroke ProcessUpdate(ManagedWrapper.Stroke stroke) { // Process Mouse Buttons if (stroke.mouse.state > 0) { var buttonsAndStates = HelperFunctions.StrokeToMouseButtonAndState(stroke); foreach (var btnState in buttonsAndStates) { var bindingUpdate = new BindingUpdate { Binding = new BindingDescriptor() { Type = BindingType.Button, Index = btnState.Button }, Value = btnState.State }; if (_detectionMode == DetectionMode.Subscription) { var blockingRequestedByUi = _subHandler.FireCallbacks(bindingUpdate.Binding, (short)bindingUpdate.Value); if (_blockingEnabled) { // Block enabled if (blockingRequestedByUi) { // Blocking controlled by UI and requested by UI, OR blocking not controlled by UI // Remove the event for this button from the stroke, leaving other button events intact stroke.mouse.state -= btnState.Flag; // If we are removing a mouse wheel event, then set rolling to 0 if no mouse wheel event left if (btnState.Flag == 0x400 || btnState.Flag == 0x800) { if ((stroke.mouse.state & 0x400) != 0x400 && (stroke.mouse.state & 0x800) != 0x800) { //Debug.WriteLine("UCR| Removing rolling flag from stroke"); stroke.mouse.rolling = 0; } } //Debug.WriteLine($"UCR| Removing flag {btnState.Flag} from stoke, leaving state {stroke.mouse.state}"); } } } else { _bindModeHandler?.Invoke(this, new BindModeUpdate { Device = _deviceDescriptor, Binding = _deviceLibrary.GetInputBindingReport(_deviceDescriptor, bindingUpdate.Binding), Value = (short)bindingUpdate.Value }); } } } // Process Relative Mouse Move if ((stroke.mouse.flags & (ushort)ManagedWrapper.MouseFlag.MouseMoveRelative) == (ushort)ManagedWrapper.MouseFlag.MouseMoveRelative) { var bindingUpdates = HelperFunctions.StrokeToMouseMove(stroke); if (bindingUpdates.Count > 0) { foreach (var bindingUpdate in bindingUpdates) { if (_detectionMode == DetectionMode.Subscription) { var blockingRequestedByUi = _subHandler.FireCallbacks(bindingUpdate.Binding, (short)bindingUpdate.Value); if (_blockingEnabled) { if (blockingRequestedByUi) { if (bindingUpdate.Binding.Index == 0) { stroke.mouse.x = 0; } else { stroke.mouse.y = 0; } } } } else { _bindModeHandler?.Invoke(this, new BindModeUpdate { Device = _deviceDescriptor, Binding = _deviceLibrary.GetInputBindingReport(_deviceDescriptor, bindingUpdate.Binding), Value = (short)bindingUpdate.Value }); } } } } // Forward on the stroke if required if (stroke.mouse.x != 0 || stroke.mouse.y != 0 || stroke.mouse.state != 0) { return(stroke); } return(default(ManagedWrapper.Stroke)); }
private void DoPoll(object sender, EventArgs e) { _pollThreadRunning = true; var stroke = new ManagedWrapper.Stroke(); // Process Keyboard input for (var i = 1; i < 11; i++) { var isMonitoredKeyboard = _monitoredKeyboards.ContainsKey(i); while (ManagedWrapper.Receive(_deviceContext, i, ref stroke, 1) > 0) { if (isMonitoredKeyboard) { var blockingRequestedByUi = _monitoredKeyboards[i].ProcessUpdate(stroke); // Block for keyboard either blocks whole stroke or allows whole stroke through if (_blockingEnabled && (!_blockingControlledByUi || blockingRequestedByUi)) { continue; // Block input } } // Pass through stroke if (_fireStrokeOnThread) { var threadStroke = stroke; var deviceId = i; ThreadPool.QueueUserWorkItem(cb => ManagedWrapper.Send(_deviceContext, deviceId, ref threadStroke, 1)); } else { ManagedWrapper.Send(_deviceContext, i, ref stroke, 1); } } } // Process Mouse input // As a mouse stroke can contain multiple updates (buttons, movement etc) per stroke... // ...blocking is handled in ProcessUpdate, so if part of the stroke is blocked, it will be removed from the stroke for (var i = 11; i < 21; i++) { var isMonitoredMouse = _monitoredMice.ContainsKey(i); while (ManagedWrapper.Receive(_deviceContext, i, ref stroke, 1) > 0) { if (isMonitoredMouse) { stroke = _monitoredMice[i].ProcessUpdate(stroke); // Handle blocking - if all updates have been removed from stroke, do not bother to pass the stroke through if (stroke.mouse.x == 0 && stroke.mouse.y == 0 && stroke.mouse.state == 0) { continue; } } // Pass through stroke if (_fireStrokeOnThread) { var threadStroke = stroke; var deviceId = i; ThreadPool.QueueUserWorkItem(cb => ManagedWrapper.Send(_deviceContext, deviceId, ref threadStroke, 1)); } else { ManagedWrapper.Send(_deviceContext, i, ref stroke, 1); } } } _pollThreadRunning = false; }
public bool SetOutputState(OutputSubscriptionRequest subReq, BindingDescriptor bindingDescriptor, int state) { int devId; try { devId = _deviceLibrary.GetInputDeviceIdentifier(subReq.DeviceDescriptor); } catch { return(false); } //Log("SetOutputState. Type: {0}, Index: {1}, State: {2}, Device: {3}", inputType, inputIndex, state, devId); var stroke = new ManagedWrapper.Stroke(); if (HelperFunctions.IsKeyboard(devId)) { var st = (ushort)(1 - state); var code = (ushort)(bindingDescriptor.Index + 1); if (code > 255) { st += 2; code -= 256; } stroke.key.code = code; stroke.key.state = st; } else { switch (bindingDescriptor.Type) { case BindingType.Axis: var mouse = new ManagedWrapper.MouseStroke { //ToDo: This only implements mouse relative mode - can we allow absolute mode too? flags = (ushort)ManagedWrapper.MouseFlag.MouseMoveRelative }; if (bindingDescriptor.Index != 0) { if (bindingDescriptor.Index == 1) { mouse.y = state; } else { throw new NotImplementedException(); } } else { mouse.x = state; } stroke.mouse = mouse; break; case BindingType.Button: var btn = bindingDescriptor.Index; var flag = (int)ManagedWrapper.MouseButtonFlags[btn]; if (btn < 5) { // Regular buttons if (state == 0) { flag *= 2; } } else { // Wheel stroke.mouse.rolling = (short)((btn == 5 || btn == 8) ? 120 : -120); } stroke.mouse.state = (ushort)flag; break; case BindingType.POV: default: throw new NotImplementedException(); } } ManagedWrapper.Send(_deviceContext, devId, ref stroke, 1); return(true); }