예제 #1
0
        static void Main()
        {
            using (VulcanKeyboard keyboard = VulcanKeyboard.Initialize())
            {
                if (keyboard == null)
                {
                    Console.WriteLine("Did not find vulcan!");
                    Console.ReadLine();
                    return;
                }

                Console.WriteLine("Found Vulcan!");
                keyboard.SetColor(Color.Red);
                keyboard.SetKeyColor(Key.W, Color.Blue);
                keyboard.SetKeyColor(Key.A, Color.Blue);
                keyboard.SetKeyColor(Key.S, Color.Blue);
                keyboard.SetKeyColor(Key.D, Color.Blue);

                var watch = new Stopwatch();
                watch.Start();
                bool success = keyboard.Update();
                Console.WriteLine("Set colors: " + success + ", took :" + watch.ElapsedMilliseconds + "ms");
                watch.Stop();
                Console.ReadLine();
                Console.WriteLine("Disconnecting...");
            }
        }
예제 #2
0
        public static void Main()
        {
            using VulcanKeyboard keyboard = VulcanKeyboard.Initialize();

            if (keyboard == null)
            {
                Console.WriteLine("Did not find vulcan!");
                Console.ReadLine();
                return;
            }
            var pos = GetKeyPositions();
            //Thread.Sleep(3000);

            //Cursor.Position = new Point(screenLeft, screenTop);
            //Thread.Sleep(200);
            //Cursor.Position = new Point(screenLeft + screenWidth, screenTop);
            //Thread.Sleep(200);
            //Cursor.Position = new Point(screenLeft + screenWidth, screenTop + screenHeight);
            //Thread.Sleep(200);
            //Cursor.Position = new Point(screenLeft, screenTop + screenHeight);
            //Thread.Sleep(200);


            bool initKeyboard = true;
            bool fnPressed    = false;
            bool easyPressed  = false;

            Dictionary <int, byte[]> mapping            = new Dictionary <int, byte[]>();
            Dictionary <int, Color>  alreadyClickedKeys = new Dictionary <int, Color>();

            if (initKeyboard)
            {
                var are  = new AutoResetEvent(false);
                var are2 = new AutoResetEvent(false);
                keyboard.SetColor(Color.Black);
                keyboard.SetKeyColor(0, Color.Green);
                keyboard.Update();

                int i = 0;
                Dictionary <string, byte[]> keys = new Dictionary <string, byte[]>()
                {
                    { "esc", new byte[] { 0x03, 0x00, 0xFB, 0x11 } },
                    { "fn", new byte[] { 0x03, 0x00, 0xFB, 0x77 } },
                    { "easyshift", new byte[] { 0x03, 0x00, 0x0A, 0xFF } },
                    { "oemminus", new byte[] { 0x03, 0x00, 0xFB, 0x5E } },
                };
                ByteEventArgs args = null;

                keyboard.KeyPressedReceived += (object sender, ByteEventArgs even) =>
                {
                    args = even;
                    are2.Set();
                    try
                    {
                        if (args.Bytes.Take(4).SequenceEqual(keys["fn"]))
                        {
                            return;
                        }
                        if (args.Bytes.Take(4).SequenceEqual(keys["easyshift"]))
                        {
                            return;
                        }

                        if (fnPressed && args.Bytes.Take(4).SequenceEqual(keys["oemminus"]) && args.Bytes[4] > 0)
                        {
                            Console.WriteLine("You have made a custom fn hotkey :)");
                        }

                        if (easyPressed && args.Bytes.Take(4).SequenceEqual(keys["oemminus"]) && args.Bytes[4] > 0)
                        {
                            Console.WriteLine("You have made a custom easy hotkey :)");
                        }

                        if (even.Bytes.Take(4).SequenceEqual(keys["esc"]))
                        {
                            i++;
                            keyboard.SetColor(Color.Black);
                            //keyboard.SetKeyColor(i, Color.Green);
                            keyboard.SetKeyColor(i / 2, Color.Green);
                            keyboard.Update();
                            return;
                        }

                        //i++;

                        keyboard.SetColor(Color.Black);
                        //keyboard.SetKeyColor(i, Color.Green);
                        keyboard.SetKeyColor(i / 2, Color.Green);
                        keyboard.Update();

                        //mapping.Add(i, even.Bytes.Take(20).ToArray());

                        //if (i > 300)
                        //    are.Set();
                    }
                    catch (Exception e)
                    {
                    }
                };;
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        are2.WaitOne();
                        if (args.Bytes.Take(4).SequenceEqual(keys["fn"]))
                        {
                            var block = args.Bytes[4] > 0;
                            if (block)
                            {
                                keyboard.SetColor(Color.Red);
                            }
                            else
                            {
                                keyboard.SetColor(Color.Green);
                            }
                            keyboard.Update();
                            fnPressed = block;
                            try
                            {
                                BlockInput(block);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                                BlockInput(false);
                            }
                        }
                        else if (args.Bytes.Take(4).SequenceEqual(keys["easyshift"]))
                        {
                            var block = args.Bytes[4] < 1;
                            if (block)
                            {
                                keyboard.SetColor(Color.Yellow);
                            }
                            else
                            {
                                keyboard.SetColor(Color.Green);
                            }
                            keyboard.Update();
                            easyPressed = block;
                            try
                            {
                                BlockInput(block);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                                BlockInput(false);
                            }
                        }
                    }
                });
                are.WaitOne();
            }



            CudafyTranslator.GenerateDebug = true;
            CudafyModule km = CudafyTranslator.Cudafy(ePlatform.x64, eArchitecture.OpenCL);//, Version.Parse("9.0"), false, new Type[]{N.GetType()});

            km.GenerateDebug = true;
            GPGPU gpu = CudafyHost.GetDevice(eGPUType.OpenCL, 0);// CudafyModes.Target, CudafyModes.DeviceId);

            //GPGPU gpu = new OpenCLDevice(2);

            gpu.LoadModule(km);
            double frameTime = 1000d / 500d;

            GPUColorBGRA[] colors = new GPUColorBGRA[] {
                //new GPUColorRGB { Red = 0xAD, Green = 0x4A, Blue =0x00},
                //new GPUColorRGB { Red = 0xCE, Green = 0x73, Blue =0x00},
                //new GPUColorRGB { Red = 0x00, Green = 0xAD, Blue =0xF7},
                //new GPUColorRGB { Red = 0xD2, Green = 0xCC, Blue =0x2B},
                //new GPUColorRGB { Red = 0x8C, Green = 0x7A, Blue =0x8B},
                //new GPUColorRGB { Red = 0xEC, Green = 0x00, Blue =0x00},
                //new GPUColorRGB { Red = 0x39, Green = 0x03, Blue =0x03},
                //new GPUColorRGB { Red = 0x4A, Green = 0x4A, Blue = 0x4A },

                ////Minecraft
                new GPUColorBGRA {
                    Red = 43, Green = 113, Blue = 119
                },
                //new GPUColorRGB { Red = 43, Green = 119, Blue = 113 },
                //new GPUColorRGB { Red = 113, Green = 43, Blue = 119 },
                //new GPUColorRGB { Red = 113, Green = 119, Blue = 43 },
                //new GPUColorRGB { Red = 119, Green = 113, Blue = 43 },
                //new GPUColorRGB { Red = 119, Green = 43, Blue = 113 },
            };

            //var ret = gpu.CopyToDevice(colors);

            float[,,] output = new float[keysWidth, keysHeight, 3];
            //float[] debugOutput = new float[screenWidth * screenHeight];
            float[,] outputs = new float[keysWidth, keysHeight];
            //float[] debugOutputs = new float[debugOutput.Length];
            float[,,] devoutput = gpu.Allocate <float>(keysWidth, keysHeight, 3);
            //float[] deboutput = gpu.Allocate<float>(debugOutput.Length);
            //int[] devindices = gpu.Allocate<int>(blockSize.x);
            List <float>  res    = new List <float>();
            HashSet <int> often  = new HashSet <int>();
            HashSet <int> often2 = new HashSet <int>();

            int[]         indices     = new int[blockSize.x];
            byte          clearOftens = 0;
            int           x           = 0;
            int           y           = 0;
            HashSet <int> ys          = new HashSet <int>();
            byte          b           = 0;
            //Thread.Sleep(2000);
            int count = 120;

            var keyColors = new Color[] { Color.Red, Color.Green, Color.Blue };
            var enumVals  = System.Enum.GetValues(typeof(Key)).Cast <int>().ToArray();

            Random r = new Random();

            using (var screenShot = new DirectScreenshot(gpu, screenWidth, screenHeight))
            {
                while (true)
                {
                    //for (int loops = 0; loops < 150; loops++)
                    //{
                    watch.Restart();
                    //(int x, int y) pointerPos = ;
                    screenShot.Capture();

                    //gpu.CopyToDevice(indices, devindices);
                    //gpu.Set(devindices);
                    //gpu.Launch(gridSize, blockSize).ScaleImageKernel(screenShot.rgbValues, ret, devindices, devoutput, deboutput);
                    _ = gpu.Launch(new dim3(keysWidth / 4, keysHeight / 3), new dim3(4, 3)).EnhancedScaleImageKernel(screenShot.rgbValues, screenWidth, screenHeight, devoutput);


                    ////Thread.Sleep(500);
                    gpu.CopyFromDevice(devoutput, output);

                    //var bitmap = new Bitmap(100, 16, PixelFormat.Format24bppRgb);
                    //unsafe
                    //{
                    //    var bmpData = bitmap.LockBits(new Rectangle(new Point(), bitmap.Size), ImageLockMode.WriteOnly, bitmap.PixelFormat);
                    //    var ptr = (byte*)bmpData.Scan0;

                    //    for (int y1 = 0; y1 < 16; y1++)
                    //    {
                    //        for (int x1 = 0; x1 < 100; x1++)
                    //        {

                    //            ptr[(y1 * 100 + x1) * 3 + 0] = (byte)output[x1, y1, 2];
                    //            ptr[(y1 * 100 + x1) * 3 + 1] = (byte)output[x1, y1, 1];
                    //            ptr[(y1 * 100 + x1) * 3 + 2] = (byte)output[x1, y1, 0];
                    //        }
                    //    }
                    //    bitmap.UnlockBits(bmpData);
                    //}
                    //bitmap.Save("TestImage.bmp");
                    ;
                    //gpu.CopyFromDevice(deboutput, debugOutput);
                    //gpu.CopyFromDevice(devindices, indices);
                    //var asdq = indices.Select((x, i) => x==0 ? -1 : (i * gridSize.x)).Where(x=>x!=-1).ToList();
                    //Console.Error.WriteLine("Milliseconds Array Sort begin: " + watch.Elapsed.TotalMilliseconds);

                    //for (int i = 0; i < output.Length; i+=gridSize.x)
                    //{
                    //    for (int oz = i; oz < gridSize.x + i; oz++)
                    //    {
                    //        var item = (int)output[oz];
                    //        if (item <= 0)
                    //            break;
                    //        count++;
                    //        //Console.WriteLine(/* LeftClick*/(item % screenWidth, item / screenWidth));
                    //        LeftClick(item % screenWidth, item / screenWidth);
                    //        //Thread.Sleep(333);
                    //    }
                    //}
                    //var zerosCount = debugOutput.Count(z => z != 0);
                    //watch.Restart();
                    //var onesAndOnlys = output.Count(z => z == 0);
                    //outputs = output.Where(f => f != 0).ToArray();
                    //Console.WriteLine(watch.Elapsed + " took where");
                    //watch.Restart();

                    //Array.Sort(output);
                    //output = output.Reverse().ToArray();
                    //Console.WriteLine(watch.Elapsed + " took sort");
                    //watch.Stop();
                    //for (int i = 0; i < outputs.Length/*&& count < 200*/; i++)
                    //{
                    //    var item = (int)outputs[i];
                    //    if (item != 0)
                    //        count++;
                    //    if (Control.IsKeyLocked(Keys.CapsLock))
                    //        break;
                    //    if (item == 0)
                    //        break;
                    //    x = item % screenWidth;
                    //    y = item / screenWidth;
                    //    //if (x == pointerPos.x && y == pointerPos.y)
                    //    //    Console.WriteLine("Pointer in Array");
                    //    //if (often2.Contains(item))
                    //    //    continue;
                    //    //if (often.Contains(item))
                    //    //{
                    //    //    if (!often2.Contains(item))
                    //    //    {
                    //    if (x > ourscreenLeft && x < ourscreenWidth + ourscreenLeft
                    //        && y > ourscreenTop && y < ourscreenTop + ourscreenHeight
                    //        && x == 666
                    //        && false)
                    //    {
                    //        //ys.Add(y);
                    //        //count++;
                    //        //Console.WriteLine($"{x}:{y}");
                    //        LeftClick(item % screenWidth + 243, item / screenWidth + 20); //Insaniquarium
                    //    }
                    //    //else
                    //    {
                    //        ;
                    //    }
                    //    //if ((item % screenWidth) == 26)
                    //    //LeftClick((item % screenWidth) + screenLeft + 243, (item / screenWidth) + 20);
                    //    //often2.Add(item);
                    //    //    }
                    //    //}
                    //    //else
                    //    //{
                    //    //    b++;

                    //    //    LeftClick(item % screenWidth, item / screenWidth);
                    //    //    often.Add(item);
                    //    //}

                    //    count++;
                    //    //LeftClick(item % screenWidth, item / screenWidth);
                    //    //Thread.Sleep(1);


                    //    //break;
                    //    //count++;
                    //    //Console.WriteLine(/* LeftClick*/(item % screenWidth, item / screenWidth));
                    //}
                    //b++;
                    //if (!Control.IsKeyLocked(Keys.CapsLock))
                    //{
                    //    //mouse_event((int)MouseEventFlags.MOUSEEVENTF_WHEEL, 0, 0, -120, 0);
                    //    //Thread.Sleep(50);
                    //}

                    watch.Stop();
                    //Console.WriteLine("Found: " + count);
                    //count = 0;
                    //var l = output.Select((x,i) => (x,i)).Where(x => x.x > 0).Select(x=>((float)x.i) / 2025).ToList();
                    //Console.WriteLine(count + " | " + l.Count);
                    //Console.Write("Found: " +  + " | ");
                    double elapsed = watch.Elapsed.TotalMilliseconds;
                    //Console.Error.WriteLine("Milliseconds: " + watch.Elapsed.TotalMilliseconds);


                    var color = keyColors[count % 3];
                    keyboard.SetColor(Color.Orange);
                    //keyboard.SetKeyColor((Key)enumVals[(count / 3)], color);
                    //keyboard.SetKeyColor((Key)enumVals[(((count / 3)+ enumVals.Length-1) % enumVals.Length)], Color.Black);
                    //count = (count + 1)%(enumVals.Length* 3);
                    //keyboard.SetColor(Color.FromArgb((count >> 16) & 0xff, (count >> 8) & 0xff, count & 0xff));
                    //count += r.Next(0, 255);
                    //keyboard.SetColor(Color.Green);
                    //for (int i = 0; i < 131; i++)
                    //{
                    //    keyboard.SetKeyColor((Key)i, Color.FromArgb(255 << 24 | r.Next(0, 255 << 16)));
                    //}

                    SetKeyColorsForAmbilight(keyboard, output);


                    //watch.Restart();
                    keyboard.Update();
                    //watch.Stop();
                    //Console.WriteLine("Set colors took:" + watch.ElapsedMilliseconds + "ms");
                    elapsed += watch.Elapsed.TotalMilliseconds;


                    //Thread.Sleep((int)Math.Max((frameTime - watch.Elapsed.TotalMilliseconds), 0));
                    Thread.Sleep(1);
                    //Thread.Sleep(Math.Min(33, Math.Max(0, 33 - (int)elapsed)));
                    //clearOftens++;
                    //if (clearOftens % 20 == 0)
                    //    often.Clear();
                    //else if (clearOftens == 101)
                    //{
                    //    often.Clear();
                    //    often2.Clear();
                    //    clearOftens = 0;
                    //}
                    //mouse_event((int)MouseEventFlags.MOUSEEVENTF_WHEEL, 0, 0, -120, 0);
                    //}
                    //Console.ReadKey();
                }
            }
            gpu.FreeAll();
            Console.Read();
        }