daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Mar 12th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Nefarius.ViGEm.Client;
  2. using Nefarius.ViGEm.Client.Targets;
  3. using Nefarius.ViGEm.Client.Targets.DualShock4;
  4. using System;
  5. using System.Collections.ObjectModel;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. using System.Windows.Input;
  9.  
  10. namespace TestPS4
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private ViGEmClient client = new ViGEmClient();
  15.         private ObservableCollection<DualShock4Controller> controllers;
  16.         private DualShock4Controller ds4;
  17.         private DualShock4Report report;
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             controllers = new ObservableCollection<DualShock4Controller>();
  27.             ds4 = new DualShock4Controller(client);
  28.             ds4.Connect();
  29.             lock (controllers) controllers.Add(ds4);
  30.             report = new DualShock4Report();
  31.  
  32.             timer1.Enabled = true;
  33.         }
  34.  
  35.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  36.         {
  37.             ds4.Disconnect();
  38.             lock (controllers) controllers.Remove(ds4);
  39.         }
  40.  
  41.         private void timer1_Tick(object sender, EventArgs e)
  42.         {
  43.             UpdatePS4fromKM();
  44.         }
  45.  
  46.         private byte XtoP(float v)
  47.         {
  48.             if (v == 0) return 128;
  49.             if (v > 0)
  50.                 return (byte)(v * 127 + 128);
  51.             else
  52.                 return (byte)(v * 128 + 128);
  53.         }
  54.  
  55.         private void UpdatePS4fromKM()
  56.         {
  57.             if ((Keyboard.IsKeyDown(Key.NumPad0))) report.SetAxis(DualShock4Axes.RightTrigger, 255);
  58.             if ((Keyboard.IsKeyDown(Key.NumPad1))) report.SetAxis(DualShock4Axes.RightTrigger, 0);
  59.  
  60.             Point p = new Point();
  61.             if ((Keyboard.IsKeyDown(Key.W))) p.Y = 1;
  62.             if ((Keyboard.IsKeyDown(Key.S))) p.Y = -1;
  63.             if ((Keyboard.IsKeyDown(Key.A))) p.X = -1;
  64.             if ((Keyboard.IsKeyDown(Key.D))) p.X = 1;
  65.             report.LeftThumbX = XtoP(p.X);
  66.             report.LeftThumbY = XtoP(-p.Y);
  67.  
  68.             if ((Keyboard.IsKeyDown(Key.Space)))
  69.                 report.SetButtonState(DualShock4Buttons.Cross, true);
  70.             else
  71.                 report.SetButtonState(DualShock4Buttons.Cross, false);
  72.  
  73.             ds4.SendReport(report);
  74.         }
  75.     }
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top