软件模拟键盘(为研究外挂而做)

本文探讨了使用不同方法进行软件模拟键盘输入的过程和技术细节,包括 SendMessage、SendKeys 和 SendInput 的应用,并通过实例展示了如何实现对特定窗口的键盘事件模拟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做了以后,发现现在的游戏都要硬件模拟才能做到,如果早几年做这个可能还有用,但是软件模拟键盘也可用来刷屏之类的,试了几种方式,发现软件模拟始终不行,还是要硬件模拟

最后还是用按键精灵写了一个脚本

下面是我做的软件模拟键盘 ,主流的三种方式的试了,通宵了两天,这方面的资料不是很多

SendMessage

 SendKeys

SendInput

引用的许多DLL,比较换,需要朋友自己整理整理吧

 private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam); 
        [DllImport(
"user32.dll")] 
        
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam); 
        
//[DllImport("user32.dll")] 
        
//private static extern IntPtr FindWindowW(string lpClassName, string lpWindowName); 
        [DllImport("user32.dll")] 
        
private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); 
        [DllImport(
"user32.dll")] 
        
private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); 

        
public struct WindowInfo 
        

             
public IntPtr hWnd; 
            
public string szWindowName; 
            
public string szClassName; 
        }
 
//寻找系统的全部窗口
        public WindowInfo[] GetAllDesktopWindows() 
        
{
            List
<WindowInfo> wndList = new List<WindowInfo>();


            EnumWindows(
delegate(IntPtr hWnd, int lParam)
            
{
                WindowInfo wnd 
= new WindowInfo();
                StringBuilder sb 
= new StringBuilder(256);
                
//get hwnd 
                wnd.hWnd = hWnd;
                
//get window name 
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.szWindowName 
= sb.ToString();
                
//get window class 
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.szClassName 
= sb.ToString();
                
//add it into list 
                wndList.Add(wnd);
                
return true;
            }
,0);

        
return wndList.ToArray(); 
        }
 



        
const Int32 _WIN32_WINNT=0x0501;
        
public struct KEYDBINPUT
        
{
            
            
public Int16 wVk;
            
public Int16 wScan;
            
public Int32 dwFlags;
            
public Int32 time;
            
public Int32 dwExtraInfo;
            
public Int32 __filler1;
            
public Int32 __filler2;
        }

        
public struct INPUT
        
{
            
public Int32 type;
            
public KEYDBINPUT ki;
        }

 
        
        [DllImport(
"USER32.DLL")]
        
public static extern IntPtr FindWindow(string lpClassName,
            
string lpWindowName);

        
// Activate an application window.DF
       

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

        [DllImport(
"User32.dll")]
        
public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);
        [DllImport(
"USER32.DLL ")]
        
public static extern int SendInput(int a, ref INPUT b, int c);
        [DllImport(
"user32.dll", EntryPoint = "SendMessage")]
        
public static extern void SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

        [DllImport(
"user32.dll")]
        
private static extern IntPtr GetForegroundWindow();

private IntPtr Game;

private void button2_Click(object sender, EventArgs e)
        
{
            WindowInfo[] a 
= GetAllDesktopWindows();
            
int i=0;
            
int index=0;
            
for (i = 0; i < a.Length; i++)
            
{
                
if (a[i].hWnd.ToString().Contains("525"))
                
{
                    MessageBox.Show(a[i].szClassName.ToString());
                    index 
= i;
                }

            }
                /*Game= FindWindow(null, "计算器"); 下面是我找特定窗口,大家试这个,找的是WINDOWS的计算器*/
           Game
= new IntPtr(a[index].hWnd); //这里设置窗口句柄
            timer1.Enabled 
= true;

            IntPtr calculatorHandle 
=Game;
            SendMessage(calculatorHandle, 
256, (IntPtr)(68), (IntPtr)(1));
            SendMessage(calculatorHandle, 
256, (IntPtr)(68), (IntPtr)(1));
            SendMessage(calculatorHandle, 
258, (IntPtr)(100), (IntPtr)(1));
            SendMessage(calculatorHandle, 
258, (IntPtr)(100), (IntPtr)(1));
            SendMessage(calculatorHandle, 
257, (IntPtr)(68), (IntPtr)(1));


            SendKeys.SendWait(
"d");

            INPUT inputDown 
= new INPUT();
            inputDown.type 
= 1;
            inputDown.ki.dwFlags 
= 0;

            INPUT inputUp 
= new INPUT();
            inputUp.type 
= 1;
            inputUp.ki.dwFlags 
= 2;


            inputDown.ki.wVk 
= (short)'D';
            SendInput(
1ref inputDown, Marshal.SizeOf(inputDown));

            inputUp.ki.wVk 
= (short)'D';
            SendInput(
1ref inputUp, Marshal.SizeOf(inputUp));
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值