NEWS

[C#] Hướng dẫn viết app Auto Typer từ clipboard

[C#] Hướng dẫn viết app Auto Typer từ clipboard
Đăng bởi: Thảo Meo - Lượt xem: 3585 13:33:30, 11/11/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục các bạn cách viết ứng dụng Auto Typer từ Copied Clipboard trong lập trình C#, Winform.

[C#] Auto Typer From Copied Clipboard - SendKeys

Dưới đây, là giao diện demo ứng dụng:

auto_copy_data

Ở đây, các bạn có thể chỉnh tốc độ để write text từ clipboard

Các bạn copy text xong chọn editor hay bất cứ chỗ nào cần send text, rồi bấm phím F1 để bắt đầu.

Các bạn có thể xem video demo Auto Typer C#:

Source code C#:

using System;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WindowsInput;
using WindowsInput.Native;

namespace AutoTyper
{
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        int speed = 10;
     
        enum KeyModifier
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            WinKey = 8
        }
        public Form1()
        {
            InitializeComponent();
           // int id = 0;
            RegisterHotKey(this.Handle, 0, (int)KeyModifier.None, Keys.F1.GetHashCode());
        

        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == 0x0312)
            {
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
                KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
                int id = m.WParam.ToInt32();
              
                TyperingText();
               
                
            }
        }
        public void TyperingText()
        {           
            var dataText = Clipboard.GetText();
            var resultString = Regex.Replace(dataText, @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline);
            simulateTypingText(resultString, typingDelay: speed);           
        }
        private void simulateTypingText(string Text, int typingDelay = 100, int startDelay = 0)
        {
            InputSimulator sim = new InputSimulator();
            sim.Keyboard.Sleep(1000);
            sim.Keyboard.Sleep(startDelay);
            string[] lines = Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            int maximum = lines.Length;
            int current = 1;

            foreach (string line in lines)
            {
                
                char[] words = line.ToCharArray();
                foreach (char word in words)
                {
                    sim.Keyboard.TextEntry(word).Sleep(typingDelay);
                }
                float percentage = ((float)current / (float)maximum) * 100;
                current++;
                sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
                sim.Keyboard.KeyPress(VirtualKeyCode.HOME);
                Console.WriteLine("Percent : {0}", percentage.ToString());
            }
        }
       
        private void txtSpeed_TextChanged(object sender, EventArgs e)
        {           
            speed = Convert.ToInt32(txtSpeed.Text);
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn viết app Auto Typer từ clipboard
Đăng bởi: Thảo Meo - Lượt xem: 3585 13:33:30, 11/11/2022DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.