Mouse Example - Soju06/Input GitHub Wiki
๋ง์ฐ์ค ์์
-
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; }
์๋ณธ ์์ ๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
-
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); }
์๋ณธ ์์ ๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.