Mouse Example - Soju06/Input GitHub Wiki

๋งˆ์šฐ์Šค ์˜ˆ์ œ

Hook

  • Simple macro rec - ๊ฐ„๋‹จํ•œ ๋งˆ์šฐ์Šค ๋งคํฌ๋กœ ๋…นํ™” ์ž…๋‹ˆ๋‹ค.

    SimpleMouseMacroSimulation.cs ์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋…นํ™”ํ•ฉ๋‹ˆ๋‹ค.

      using Input;
      using Input.Platforms.Windows;
      using System;
      using System.Runtime.InteropServices;
    
      // ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘์ „ ์ƒ์ˆ˜๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.
    
      // ๋ฒ„ํŠผ ์ค‘๋ณต ๋ฐฉ์ง€ ๋ฐ ์ด๋™์„ ์บก์ฒ˜ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.
      bool is_left_down = false, is_right_down = false;
      // ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ๋Œ€๊ธฐ์—ด์ž…๋‹ˆ๋‹ค.
      var mouse_log = new Queue<(InputButtons button, int x, int y)>();
    
      Console.WriteLine("Hello, World!");
    
      // ๋งˆ์šฐ์Šค ํ›„์ปค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
      // ๋งŒ์•ฝ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํ”Œ๋žซํผ์ธ ๊ฒฝ์šฐ NotSupportedException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      var hook = Inputs.Use<IMouseHook>();
    
      // ๋””๋ฒ„๊ทธ๋ฅผ ํ™œ์„ฑํ™” ํ•ฉ๋‹ˆ๋‹ค.
      // ๋„ค์ดํ‹ฐ๋ธŒ ์˜ค๋ฅ˜๋ฅผ ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ ์ฐฝ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      hook.Debug = true;
    
      // ๋งˆ์šฐ์Šค ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.
      var model = hook.MouseModel;
      // ๋งˆ์šฐ์Šค์˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
      model.State += StateLog;
    
      Console.WriteLine();
      Console.WriteLine("๋งคํฌ๋กœ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.");
      Console.WriteLine();
      Console.WriteLine("์ง€์› ํƒ€์ž… - ๋งˆ์šฐ์Šค ํด๋ฆญ์‹œ ์ด๋™ ์œ„์น˜");
      Console.WriteLine();
    
      LOG_START:
      Console.WriteLine("๊ธฐ๋ก์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด {Space} ํ‚ค๋ฅผ ๋ˆŒ๋ฅด์‹ญ์‹œ์˜ค.");
      Console.WriteLine();
    
      if (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
          goto LOG_START;
    
      Console.WriteLine("๊ธฐ๋ก์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.");
      bool isLogging = true;
      // ํ›„ํ‚น ์‹œ์ž‘
      hook.HookStart();
    
      // ๋กœ๊น… ์ž‘์—…์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
      Task.Factory.StartNew(() => {
          LogTask();
      })
          //๋กœ๊น…์ด ๋๋‚œ ์ž‘์—…์ž…๋‹ˆ๋‹ค.
          .ContinueWith(EndTask);
    
      // ํ”Œ๋žซํผ์ด ์œˆ๋„์šฐ์ธ ๊ฒฝ์šฐ ์œˆ๋„์šฐ ๋ฉ”์‹œ์ง€๋ฅผ ํŽŒํ”„ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
      if (Platform.IsWindows)
          while (isLogging && WindowsMessagePump.Pumping())
              Console.WriteLine("message pump");
      Console.ReadLine();
    
      // ๋กœ๊น…์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
      void LogTask() {
          LOG_STOP:
          Console.WriteLine();
          Console.WriteLine("๊ธฐ๋ก์„ ์ค‘์ง€ํ•˜๋ ค๋ฉด {Space} ํ‚ค๋ฅผ ๋ˆŒ๋ฅด์‹ญ์‹œ์˜ค.");
    
          if (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
              goto LOG_STOP;
    
          var count = mouse_log.Count;
    
          if (count < 1) {
              Console.WriteLine("๊ธฐ๋ก๋œ ๋กœ๊ทธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
              return;
          }
    
          hook.HookStop();
    
          Console.WriteLine($"์ด๋ฒคํŠธ ๊ธฐ๋ก์„ ์ •๋ฆฌ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ๊ฐฏ์ˆ˜: {count} (์ด๋ฒคํŠธ ๊ฐฏ์ˆ˜์— ๋”ฐ๋ผ ์‹œ๊ฐ„์ด ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)");
    
          var block_size = (sizeof(byte) + (sizeof(int) * 2));
          var tt_byte_count = block_size * count;
    
          unsafe {
              var _ptr = (byte*)Marshal.AllocHGlobal(tt_byte_count);
              var _cp = 0;
    
              for (int i = 0; i < count; i++) {
                  var (button, x, y) = mouse_log.Dequeue();
    
                  // button DATA
                  _ptr[_cp] = (byte)button;
                  // LPoint DATA
                  Marshal.Copy(new int[] { x, y }, 0, (IntPtr)(_ptr + _cp + 1), 2);
    
                  _cp += block_size;
              }
    
              var safe_array = new byte[tt_byte_count];
              Marshal.Copy((IntPtr)_ptr, safe_array, 0, tt_byte_count);
              Marshal.FreeHGlobal((IntPtr)_ptr);
          
              if (safe_array == null) throw new ArgumentNullException(nameof(safe_array));
    
              Console.WriteLine("์ธ์ฝ”๋”ฉ๋œ ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ์ค‘..");
              Console.WriteLine(Convert.ToBase64String(safe_array));
              Console.WriteLine("์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
              Console.WriteLine("ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋Š” SimpleMouseMacroSimulation.cs ์˜ˆ์ œ์—์„œ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.");
          }
      }
    
      // ๋กœ๊น…์ด ๋๋‚œ ์ž‘์—…์ž…๋‹ˆ๋‹ค.
      void EndTask(Task log_task) {
          hook.HookStop();
          Environment.Exit(0);
      }
    
      // ๋งˆ์šฐ์Šค์˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
      bool StateLog(object sender, InputButtons button, int x, int y) {
          switch (button) {
              case InputButtons.LeftMouseDown:
                  if (!is_left_down) {
                      is_left_down = true;
                      goto ADD;
                  }
              break;
    
              case InputButtons.LeftMouseUp:
                  if (is_left_down) {
                      is_left_down = false;
                      goto ADD;
                  }
              break;
    
              case InputButtons.RightMouseDown:
                  if (!is_right_down) {
                      is_right_down = true;
                      goto ADD;
                  }
              break;
    
              case InputButtons.RightMouseUp:
                  if (is_right_down) {
                      is_right_down = false;
                      goto ADD;
                  }
              break;
    
              case InputButtons.Move:
                  if (is_left_down || is_right_down)
                      goto ADD;
              break;
    
              default:
              break;
          }
    
          return true;
    
          ADD:
          mouse_log.Enqueue(new(button, x, y));
          return true;
      }

    ์›๋ณธ ์˜ˆ์ œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Simulation

  • Simple macro simulation - ๊ฐ„๋‹จํ•œ ๋งˆ์šฐ์Šค ๋งคํฌ๋กœ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์ž…๋‹ˆ๋‹ค.

    SimpleMouseMacroRec.cs ์˜ˆ์ œ์—์„œ ๋งŒ๋“ค์–ด์ง„ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ถ™์—ฌ๋„ฃ์œผ์‹ญ์‹œ์˜ค. (์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ๊ฐ€๋Šฅ)

      // 2.2 ๋งˆ์šฐ์Šค ๋งคํฌ๋กœ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.
      using Input;
    
      Console.WriteLine("Hello, World!");
    
      // ๋งˆ์šฐ์Šค ์‹œ๋ฎฌ๋ ˆ์ด์…˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
      // ๋งŒ์•ฝ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํ”Œ๋žซํผ์ธ ๊ฒฝ์šฐ NotSupportedException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      var simulation = Inputs.Use<IMouseSimulation>();
    
      // ๋””๋ฒ„๊ทธ๋ฅผ ํ™œ์„ฑํ™” ํ•ฉ๋‹ˆ๋‹ค.
      // ๋„ค์ดํ‹ฐ๋ธŒ ์˜ค๋ฅ˜๋ฅผ ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ ์ฐฝ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      simulation.Debug = true;
    
      INIT_SCRIPT:
      Console.WriteLine();
      Console.WriteLine("SimpleMouseMacroRec.cs ์˜ˆ์ œ์—์„œ ๋งŒ๋“ค์–ด์ง„ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ถ™์—ฌ๋„ฃ์œผ์‹ญ์‹œ์˜ค. (์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ๊ฐ€๋Šฅ)");
      var s_script = Console.ReadLine();
      if (string.IsNullOrWhiteSpace(s_script))
          goto INIT_SCRIPT;
    
      Console.WriteLine("์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋””์ฝ”๋”ฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค..");
    
      if(s_script.Length < 10000 && File.Exists(s_script))
          s_script = File.ReadAllText(s_script);
    
      var script = Convert.FromBase64String(s_script);
      var block_size = (sizeof(byte) + (sizeof(int) * 2));
      var count = script.Length / block_size;
      var cp = 0;
    
      Console.WriteLine($"์ด๋ฒคํŠธ ๊ฐฏ์ˆ˜: {count}");
      await Task.Delay(1000);
    
      for (int i = 0; i < count; i++) {
          var button = (InputButtons)script[cp];
          var x = BitConverter.ToInt32(script, cp + sizeof(byte));
          var y = BitConverter.ToInt32(script, cp + sizeof(byte) + sizeof(int));
    
          Console.WriteLine($"{button}, {x}, {y}");
    
          switch (button) {
              case InputButtons.LeftMouseDown:
                  simulation.AbsoluteDown(InputMouseButtons.Left, x, y);
                  break;
              case InputButtons.LeftMouseUp:
                  simulation.AbsoluteUp(InputMouseButtons.Left, x, y);
                  break;
              case InputButtons.RightMouseDown:
                  simulation.AbsoluteDown(InputMouseButtons.Right, x, y);
                  break;
              case InputButtons.RightMouseUp:
                  simulation.AbsoluteUp(InputMouseButtons.Right, x, y);
                  break;
              case InputButtons.Move:
                  simulation.AbsoluteMove(x, y);
                  break;
          }
    
          cp += block_size;
    
          // ์•ˆ์ „๋ชจ๋“œ
          //await Task.Delay(1);
      }

    ์›๋ณธ ์˜ˆ์ œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ