做了以后,发现现在的游戏都要硬件模拟才能做到,如果早几年做这个可能还有用,但是软件模拟键盘也可用来刷屏之类的,试了几种方式,发现软件模拟始终不行,还是要硬件模拟
最后还是用按键精灵写了一个脚本
下面是我做的软件模拟键盘 ,主流的三种方式的试了,通宵了两天,这方面的资料不是很多
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;
}
}
[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(1, ref inputDown, Marshal.SizeOf(inputDown));
inputUp.ki.wVk = (short)'D';
SendInput(1, ref inputUp, Marshal.SizeOf(inputUp));
}
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(1, ref inputDown, Marshal.SizeOf(inputDown));
inputUp.ki.wVk = (short)'D';
SendInput(1, ref inputUp, Marshal.SizeOf(inputUp));
}