Automating Playstation 4 COD Login for Daily Rewards.

I’ve updated the code here with the older below for some increased improvements. I may add OCR to the mix if I get some additional time this week. The improvements include launching PS4 and turning the machine on out of Rest mode and going back into Rest once completed.

using PS4RemotePlayInterceptor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharpPS4RemotePlayInterceptor
{

    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void bringToFront(string title)
        {
            // Get a handle to the Calculator application.
            IntPtr handle = FindWindow(null, title);

            // Verify that Calculator is a running process.
            if (handle == IntPtr.Zero)
            {
                return;
            }

            // Make Calculator the foreground application
            SetForegroundWindow(handle);
        }
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;

        static IntPtr FindWindowByIndex(IntPtr hWndParent, int index)
        {
            if (index == 0)
                return hWndParent;
            else
            {
                int ct = 0;
                IntPtr result = IntPtr.Zero;
                do
                {
                    result = FindWindowEx(hWndParent, result, "Button", null);
                    if (result != IntPtr.Zero)
                        ++ct;
                }
                while (ct < index && result != IntPtr.Zero);
                return result;
            }
        }


        Process PlayStationProcess;
        private void Form1_Load(object sender, EventArgs e)
        {
            PlayStationProcess = Process.Start(@"C:\Program Files (x86)\Sony\PS4 Remote Play\RemotePlay.exe");
            Debug.WriteLine(PlayStationProcess.Id);
            System.Threading.Thread t = new System.Threading.Thread(Start);
            t.IsBackground = true;
            t.Start();
           
        }

        private void Inject()
        {
            // Setup callback to interceptor
            Interceptor.Callback = new InterceptionDelegate(OnReceiveData);
            // Emulate controller (BETA)
            Interceptor.EmulateController = true;

            // Start watchdog to automatically inject when possible
            Interceptor.Watchdog.Start();
            // Notify watchdog events
            Interceptor.Watchdog.OnInjectionSuccess = () => Console.WriteLine("Watchdog OnInjectionSuccess");
            Interceptor.Watchdog.OnInjectionFailure = () => Console.WriteLine("Watchdog OnInjectionFailure");

            // Or inject manually and handle exceptions yourself
            Interceptor.InjectionMode = InjectionMode.Compatibility;
            //Interceptor.Inject();
        }

        public System.Diagnostics.Stopwatch BootKeeper;
        public System.Diagnostics.Stopwatch TimeKeeper;
        private void Start()
        {
            Inject();
            BootKeeper = new System.Diagnostics.Stopwatch();
            TimeKeeper = new System.Diagnostics.Stopwatch();
            
            PointerToMethodToCall = StartCOD;
            while (true)
            {
                if (Keyboard.IsKeyDown(Keys.Z))
                {
                    Debug.WriteLine("z");
                }    

                byte[] keys = new byte[256];

                Keyboard.GetKeyboardState(keys);

                if ((keys[(int)Keys.Up] & keys[(int)Keys.Right] & 128) == 128)
                {
                    Debug.WriteLine("Up Arrow key and Right Arrow key down.");
                }
                Application.DoEvents();
                System.Threading.Thread.Sleep(50);
            }
        }

        private delegate void HumanForPS4(ref DualShockState state);
        HumanForPS4 PointerToMethodToCall;

        bool ActionDuringTime(Stopwatch Keeper, double StartTime,  double Duration = 250)
        {
            if (Keeper.Elapsed.TotalMilliseconds > (StartTime * 1000) && (Keeper.Elapsed.TotalMilliseconds < Duration + (StartTime * 1000)))
            {
                //Debug.WriteLine("Returning true for Action");
                return true;
            }
            else
            {
                return false;
            }
        }

        const uint WM_KEYDOWN = 0x100;
        const uint WM_KEYUP = 0x101;
        const uint Key_Down = 0x0001;
        const uint Key_Up = 0x0002;

        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        bool step1 = false, step2 = false, step3 = false, step4 = false;
        private void StartCOD(ref DualShockState state)
        {

            if (ActionDuringTime(BootKeeper, 3))
            {
                bringToFront(PlayStationProcess.MainWindowTitle);
            }

            if (ActionDuringTime(BootKeeper, 5))
            {
                if (!step1)
                {
                    if (false) //failed attenpt to send control send.
                    {
                    }
                    IntPtr hWndAbout = FindWindow(null/* TODO Change to default(_) if this is not a reference type */, PlayStationProcess.MainWindowTitle);

                    //keybd_event((byte)Keys.LControlKey, 0, Key_Down, 0);
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    //keybd_event((byte)Keys.LControlKey, 0, Key_Up, 0);
                    IntPtr hWndOkButton = FindWindowEx(hWndAbout, IntPtr.Zero, "WindowsForms10.STATIC.app.0.141b42a_r9_ad1", "Start");
                    step1 = true;
                    Debug.WriteLine("Tab sent");
                   SendKeys.SendWait("{TAB}");
                }
            }

            /*
            if (ActionDuringTime(BootKeeper, 6))
            {
                if (!step1)
                {
                    IntPtr hWndAbout = FindWindow(null, PlayStationProcess.MainWindowTitle);

                    //keybd_event((byte)Keys.LControlKey, 0, Key_Down, 0);
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    //keybd_event((byte)Keys.LControlKey, 0, Key_Up, 0);
                    IntPtr hWndOkButton = FindWindowEx(hWndAbout, IntPtr.Zero, "WindowsForms10.STATIC.app.0.141b42a_r9_ad1", "Start");
                    step1 = true;
                    Debug.WriteLine("Tab sent2");
                    //SendKeys.SendWait("{TAB}");
                }
            }
            */

            if (ActionDuringTime(BootKeeper, 7))
            {
                if (!step2)
                {
                    step2 = true;
                    SendKeys.SendWait("{ENTER}");
                }
            }

            if (BootKeeper.Elapsed.TotalSeconds > 70 && !TimeKeeper.IsRunning)
            {
                TimeKeeper.Start();
            }

            if (ActionDuringTime(TimeKeeper, 1, 2000)) //Move alll the way to the left
            {
                state.LX = 0;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 5)) //Move Right
            {
                state.LX = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 7)) //Move Right
            {
                state.LX = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 10)) //Hit X, start game
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 23)) //Hit X, Skip Intro
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 28))  //Hit X, Hit X to start game into main menu
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 30)) // Down one menu
            {
                state.LY = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 33)) //Down to zombies
            {
                state.LY = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 36)) //Hit X Enter Zombies
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 40)) //Hit X, Ack the rewards
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 42, 2000)) //Hold the button to quit
            {
                state.PS = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 45, 2000)) //Move up to close application
            {
                state.LY = 0;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 52)) //Confirm to close application
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 56)) //Confirm to close application
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 66)) //Move left
            {
                state.LX = 0;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 70, 2000)) //Hold the button to quit
            {
                state.PS = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 76)) //Move down (we assume we are already at sound/devices)
            {
                state.LY = 255;
                return;
            }


            if (ActionDuringTime(TimeKeeper, 80)) //Move doown
            {
                state.LY = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 83)) //Move right
            {
                state.LX = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 85)) //Select to enter rest mode
            {
                state.Cross = true;
                return;
            }


            if (ActionDuringTime(TimeKeeper, 87)) //Kill our connection
            {
                if (!step3)
                {
                    step3 = true;
                    PlayStationProcess.Kill();
                }
                return;
            }


            if (ActionDuringTime(TimeKeeper, 95)) //Kill our connection
            {
                if (!step4)
                {
                    step4 = true;
                    Application.Exit();
                }
                return;
            }

            state.LX = 128;
            state.LY = 128;
            state.Cross = false;
            state.Square = false;
            state.Circle = false;
            state.Triangle = false;
            state.PS = false;
        }


        bool HookedAndSending = false;
        private void OnReceiveData(ref DualShockState state)
        {
            if (!HookedAndSending)
            {
                HookedAndSending = true;
                Debug.WriteLine("Hooked");
                BootKeeper.Start();
            }
            PointerToMethodToCall(ref state);


            return;
            state.Cross = Keyboard.IsKeyDown(Keys.X);
            state.Square = Keyboard.IsKeyDown(Keys.A);
            state.Circle = Keyboard.IsKeyDown(Keys.D);
            state.Triangle = Keyboard.IsKeyDown(Keys.W);

            if (state.Cross || state.Square || state.Triangle || state.Circle)
            {
                Debug.WriteLine(state.Cross + " " + state.Square + " " + state.Triangle + " " + state.Circle);
            }

            if (Keyboard.IsKeyDown(Keys.Up))
            {
                state.LY = 0;
            }

            if (Keyboard.IsKeyDown(Keys.Down))
            {
                state.LY = 255;
            }

            if (Keyboard.IsKeyDown(Keys.Left))
            {
                state.LX = 0;
            }

            if (Keyboard.IsKeyDown(Keys.Right))
            {
                state.LX = 255;
            }

            if (!Keyboard.IsKeyDown(Keys.Right) && !Keyboard.IsKeyDown(Keys.Left))
            {
                state.LX = 128;
            }

            if (!Keyboard.IsKeyDown(Keys.Up) && !Keyboard.IsKeyDown(Keys.Down))
            {
                state.LY = 128;
            }

            /* -- Modify the controller state here -- */

            // Force press X


            // Force left analog upwards


            // Force left analog downwards
            // state.LY = 255;

            // Force left analog to center
            // state.LX = 128;
            // state.LY = 128;
        }

        public abstract class Keyboard
        {
            [Flags]
            private enum KeyStates
            {
                None = 0,
                Down = 1,
                Toggled = 2
            }

            [DllImport("user32.dll")]
            public static extern int GetKeyboardState(byte[] keystate);

            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            private static extern short GetKeyState(int keyCode);

            private static KeyStates GetKeyState(Keys key)
            {
                KeyStates state = KeyStates.None;

                short retVal = GetKeyState((int)key);

                //If the high-order bit is 1, the key is down
                //otherwise, it is up.
                if ((retVal & 0x8000) == 0x8000)
                    state |= KeyStates.Down;

                //If the low-order bit is 1, the key is toggled.
                if ((retVal & 1) == 1)
                    state |= KeyStates.Toggled;

                return state;
            }

            public static bool IsKeyDown(Keys key)
            {
                return KeyStates.Down == (GetKeyState(key) & KeyStates.Down);
            }

            public static bool IsKeyToggled(Keys key)
            {
                return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Interceptor.StopInjection();
        }
    }
}
using PS4RemotePlayInterceptor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharpPS4RemotePlayInterceptor
{

    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void bringToFront(string title)
        {
            // Get a handle to the Calculator application.
            IntPtr handle = FindWindow(null, title);

            // Verify that Calculator is a running process.
            if (handle == IntPtr.Zero)
            {
                return;
            }

            // Make Calculator the foreground application
            SetForegroundWindow(handle);
        }
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;

        static IntPtr FindWindowByIndex(IntPtr hWndParent, int index)
        {
            if (index == 0)
                return hWndParent;
            else
            {
                int ct = 0;
                IntPtr result = IntPtr.Zero;
                do
                {
                    result = FindWindowEx(hWndParent, result, "Button", null);
                    if (result != IntPtr.Zero)
                        ++ct;
                }
                while (ct < index && result != IntPtr.Zero);
                return result;
            }
        }


        Process PlayStationProcess;
        private void Form1_Load(object sender, EventArgs e)
        {
            PlayStationProcess = Process.Start(@"C:\Program Files (x86)\Sony\PS4 Remote Play\RemotePlay.exe");
            Debug.WriteLine(PlayStationProcess.Id);
            System.Threading.Thread t = new System.Threading.Thread(Start);
            t.IsBackground = true;
            t.Start();
           
        }

        private void Inject()
        {
            // Setup callback to interceptor
            Interceptor.Callback = new InterceptionDelegate(OnReceiveData);
            // Emulate controller (BETA)
            Interceptor.EmulateController = true;

            // Start watchdog to automatically inject when possible
            Interceptor.Watchdog.Start();
            // Notify watchdog events
            Interceptor.Watchdog.OnInjectionSuccess = () => Console.WriteLine("Watchdog OnInjectionSuccess");
            Interceptor.Watchdog.OnInjectionFailure = () => Console.WriteLine("Watchdog OnInjectionFailure");

            // Or inject manually and handle exceptions yourself
            Interceptor.InjectionMode = InjectionMode.Compatibility;
            //Interceptor.Inject();
        }

        public System.Diagnostics.Stopwatch BootKeeper;
        public System.Diagnostics.Stopwatch TimeKeeper;
        private void Start()
        {
            Inject();
            BootKeeper = new System.Diagnostics.Stopwatch();
            TimeKeeper = new System.Diagnostics.Stopwatch();
            
            PointerToMethodToCall = StartCOD;
            while (true)
            {
                if (Keyboard.IsKeyDown(Keys.Z))
                {
                    Debug.WriteLine("z");
                }    

                byte[] keys = new byte[256];

                Keyboard.GetKeyboardState(keys);

                if ((keys[(int)Keys.Up] & keys[(int)Keys.Right] & 128) == 128)
                {
                    Debug.WriteLine("Up Arrow key and Right Arrow key down.");
                }
                Application.DoEvents();
                System.Threading.Thread.Sleep(50);
            }
        }

        private delegate void HumanForPS4(ref DualShockState state);
        HumanForPS4 PointerToMethodToCall;

        bool ActionDuringTime(Stopwatch Keeper, double StartTime,  double Duration = 250)
        {
            if (Keeper.Elapsed.TotalMilliseconds > (StartTime * 1000) && (Keeper.Elapsed.TotalMilliseconds < Duration + (StartTime * 1000)))
            {
                //Debug.WriteLine("Returning true for Action");
                return true;
            }
            else
            {
                return false;
            }
        }

        const uint WM_KEYDOWN = 0x100;
        const uint WM_KEYUP = 0x101;
        const uint Key_Down = 0x0001;
        const uint Key_Up = 0x0002;

        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        bool step1 = false, step2 = false, step3 = false, step4 = false;
        private void StartCOD(ref DualShockState state)
        {

            if (ActionDuringTime(BootKeeper, 3))
            {
                bringToFront(PlayStationProcess.MainWindowTitle);
            }

            if (ActionDuringTime(BootKeeper, 5))
            {
                if (!step1)
                {
                    if (false) //failed attenpt to send control send.
                    {
                    }
                    IntPtr hWndAbout = FindWindow(null/* TODO Change to default(_) if this is not a reference type */, PlayStationProcess.MainWindowTitle);

                    //keybd_event((byte)Keys.LControlKey, 0, Key_Down, 0);
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    //keybd_event((byte)Keys.LControlKey, 0, Key_Up, 0);
                    IntPtr hWndOkButton = FindWindowEx(hWndAbout, IntPtr.Zero, "WindowsForms10.STATIC.app.0.141b42a_r9_ad1", "Start");
                    step1 = true;
                    Debug.WriteLine("Tab sent");
                   SendKeys.SendWait("{TAB}");
                }
            }

            /*
            if (ActionDuringTime(BootKeeper, 6))
            {
                if (!step1)
                {
                    IntPtr hWndAbout = FindWindow(null, PlayStationProcess.MainWindowTitle);

                    //keybd_event((byte)Keys.LControlKey, 0, Key_Down, 0);
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    PostMessage(PlayStationProcess.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.Tab, (IntPtr)(0));
                    Thread.Sleep(50);
                    //keybd_event((byte)Keys.LControlKey, 0, Key_Up, 0);
                    IntPtr hWndOkButton = FindWindowEx(hWndAbout, IntPtr.Zero, "WindowsForms10.STATIC.app.0.141b42a_r9_ad1", "Start");
                    step1 = true;
                    Debug.WriteLine("Tab sent2");
                    //SendKeys.SendWait("{TAB}");
                }
            }
            */

            if (ActionDuringTime(BootKeeper, 7))
            {
                if (!step2)
                {
                    step2 = true;
                    SendKeys.SendWait("{ENTER}");
                }
            }

            if (BootKeeper.Elapsed.TotalSeconds > 70 && !TimeKeeper.IsRunning)
            {
                TimeKeeper.Start();
            }

            if (ActionDuringTime(TimeKeeper, 1, 2000)) //Move alll the way to the left
            {
                state.LX = 0;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 5)) //Move Right
            {
                state.LX = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 7)) //Move Right
            {
                state.LX = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 10)) //Hit X, start game
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 23)) //Hit X, Skip Intro
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 28))  //Hit X, Hit X to start game into main menu
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 30)) // Down one menu
            {
                state.LY = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 33)) //Down to zombies
            {
                state.LY = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 36)) //Hit X Enter Zombies
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 40)) //Hit X, Ack the rewards
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 42, 2000)) //Hold the button to quit
            {
                state.PS = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 45, 2000)) //Move up to close application
            {
                state.LY = 0;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 52)) //Confirm to close application
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 56)) //Confirm to close application
            {
                state.Cross = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 66)) //Move left
            {
                state.LX = 0;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 70, 2000)) //Hold the button to quit
            {
                state.PS = true;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 76)) //Move down (we assume we are already at sound/devices)
            {
                state.LY = 255;
                return;
            }


            if (ActionDuringTime(TimeKeeper, 80)) //Move doown
            {
                state.LY = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 83)) //Move right
            {
                state.LX = 255;
                return;
            }

            if (ActionDuringTime(TimeKeeper, 85)) //Select to enter rest mode
            {
                state.Cross = true;
                return;
            }


            if (ActionDuringTime(TimeKeeper, 87)) //Kill our connection
            {
                if (!step3)
                {
                    step3 = true;
                    PlayStationProcess.Kill();
                }
                return;
            }


            if (ActionDuringTime(TimeKeeper, 87)) //Kill our connection
            {
                if (!step4)
                {
                    step4 = true;
                    Application.Exit();
                }
                return;
            }

            state.LX = 128;
            state.LY = 128;
            state.Cross = false;
            state.Square = false;
            state.Circle = false;
            state.Triangle = false;
            state.PS = false;
        }


        bool HookedAndSending = false;
        private void OnReceiveData(ref DualShockState state)
        {
            if (!HookedAndSending)
            {
                HookedAndSending = true;
                Debug.WriteLine("Hooked");
                BootKeeper.Start();
            }
            PointerToMethodToCall(ref state);


            return;
            state.Cross = Keyboard.IsKeyDown(Keys.X);
            state.Square = Keyboard.IsKeyDown(Keys.A);
            state.Circle = Keyboard.IsKeyDown(Keys.D);
            state.Triangle = Keyboard.IsKeyDown(Keys.W);

            if (state.Cross || state.Square || state.Triangle || state.Circle)
            {
                Debug.WriteLine(state.Cross + " " + state.Square + " " + state.Triangle + " " + state.Circle);
            }

            if (Keyboard.IsKeyDown(Keys.Up))
            {
                state.LY = 0;
            }

            if (Keyboard.IsKeyDown(Keys.Down))
            {
                state.LY = 255;
            }

            if (Keyboard.IsKeyDown(Keys.Left))
            {
                state.LX = 0;
            }

            if (Keyboard.IsKeyDown(Keys.Right))
            {
                state.LX = 255;
            }

            if (!Keyboard.IsKeyDown(Keys.Right) && !Keyboard.IsKeyDown(Keys.Left))
            {
                state.LX = 128;
            }

            if (!Keyboard.IsKeyDown(Keys.Up) && !Keyboard.IsKeyDown(Keys.Down))
            {
                state.LY = 128;
            }

            /* -- Modify the controller state here -- */

            // Force press X


            // Force left analog upwards


            // Force left analog downwards
            // state.LY = 255;

            // Force left analog to center
            // state.LX = 128;
            // state.LY = 128;
        }

        public abstract class Keyboard
        {
            [Flags]
            private enum KeyStates
            {
                None = 0,
                Down = 1,
                Toggled = 2
            }

            [DllImport("user32.dll")]
            public static extern int GetKeyboardState(byte[] keystate);

            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            private static extern short GetKeyState(int keyCode);

            private static KeyStates GetKeyState(Keys key)
            {
                KeyStates state = KeyStates.None;

                short retVal = GetKeyState((int)key);

                //If the high-order bit is 1, the key is down
                //otherwise, it is up.
                if ((retVal & 0x8000) == 0x8000)
                    state |= KeyStates.Down;

                //If the low-order bit is 1, the key is toggled.
                if ((retVal & 1) == 1)
                    state |= KeyStates.Toggled;

                return state;
            }

            public static bool IsKeyDown(Keys key)
            {
                return KeyStates.Down == (GetKeyState(key) & KeyStates.Down);
            }

            public static bool IsKeyToggled(Keys key)
            {
                return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Interceptor.StopInjection();
        }
    }
}

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *