Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using WindowsInput;
  8. using SharpDX.XInput;
  9.  
  10. namespace PS4_Test
  11. {
  12.     class ps4controller
  13.     {
  14.         private const int MovementDivider = 2_000;
  15.         private const int ScrollDivider = 10_000;
  16.         private const int RefreshRate = 60;
  17.  
  18.         private Timer _timer;
  19.         private Controller _controller;
  20.         private IMouseSimulator _mouseSimulator;
  21.  
  22.         private bool _wasADown;
  23.         private bool _wasBDown;
  24.  
  25.         public ps4controller()
  26.         {
  27.             _controller = new Controller(UserIndex.One);
  28.             _mouseSimulator = new InputSimulator().Mouse;
  29.             _timer = new Timer(obj => Update());
  30.         }
  31.  
  32.         public void Start()
  33.         {
  34.             _timer.Change(0, 1000 / RefreshRate);
  35.         }
  36.  
  37.         private void Update()
  38.         {
  39.             _controller.GetState(out var state);
  40.             Movement(state);
  41.             Scroll(state);
  42.             LeftButton(state);
  43.             RightButton(state);
  44.         }
  45.  
  46.         private void RightButton(State state)
  47.         {
  48.             var isBDown = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.B);
  49.             if (isBDown && !_wasBDown) _mouseSimulator.RightButtonDown();
  50.             if (!isBDown && _wasBDown) _mouseSimulator.RightButtonUp();
  51.             _wasBDown = isBDown;
  52.         }
  53.  
  54.         private void LeftButton(State state)
  55.         {
  56.             var isADown = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.A);
  57.             if (isADown && !_wasADown) _mouseSimulator.LeftButtonDown();
  58.             if (!isADown && _wasADown) _mouseSimulator.LeftButtonUp();
  59.             _wasADown = isADown;
  60.         }
  61.  
  62.         private void Scroll(State state)
  63.         {
  64.             var x = state.Gamepad.RightThumbX / ScrollDivider;
  65.             var y = state.Gamepad.RightThumbY / ScrollDivider;
  66.             _mouseSimulator.HorizontalScroll(x);
  67.             _mouseSimulator.VerticalScroll(y);
  68.         }
  69.  
  70.         private void Movement(State state)
  71.         {
  72.             var x = state.Gamepad.LeftThumbX / MovementDivider;
  73.             var y = state.Gamepad.LeftThumbY / MovementDivider;
  74.             _mouseSimulator.MoveMouseBy(x, -y);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement