作者:jamesfay
我做过一个侍魂2 PC版的自动发招程序,大概的方法就是Hook住一个key的消息,然后用keybd_event模拟键盘的消息。我觉得sendMessage不一定有用,因为很多时候directX的程序是不看winMessage的,而是直接读设备。所以还是推荐用keybd_event。
http://www.codeproject.com/csharp/globalhook.asp 这个能帮你实现Hook
至于keybd_event很简单的。
比如这就是天霸封神斩的发招方法:
[DllImport("user32.dll", EntryPoint="keybd_event")]
public static extern void keybd_event (
byte bVk,
byte bScan,
int dwFlags,
int dwExtraInfo
);
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
private const int KEYEVENTF_KEYDOWN = 0x00;
private const int KEY_A = 0x41;
private const int KEY_S = 0x53;
private const int KEY_D = 0x44;
private const int KEY_W = 0x57;
private const int KEY_J = 0x4A;
private const int KEY_K = 0x4B;
private const int KEY_L = 0x4C;
private const int KEY_U = 0x55;
private const int KEY_I = 0x49;
private const int KEY_O = 0x4F;
#region SimulateKey
private void OneKey(byte VirtualKey, int _interval, int KeyUpDown)
{
keybd_event(VirtualKey, 0, KeyUpDown, 0);
Thread.Sleep(_interval);
}
private void TowKey(byte VirtualKey1, byte VirtualKey2, int _interval, int KeyUpDown)
{
keybd_event(VirtualKey1, 0, KeyUpDown, 0);
keybd_event(VirtualKey2, 0, KeyUpDown, 0);
Thread.Sleep(_interval);
}
#endregion SimulateKey
#region Actions
private void ToRight_Action01(int Interval)
{
int _interval = Interval;
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYDOWN);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYUP);
}
private void ToLeft_Action01(int Interval)
{
int _interval = Interval;
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYDOWN);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYUP);
}
#endregion Actions
我做过一个侍魂2 PC版的自动发招程序,大概的方法就是Hook住一个key的消息,然后用keybd_event模拟键盘的消息。我觉得sendMessage不一定有用,因为很多时候directX的程序是不看winMessage的,而是直接读设备。所以还是推荐用keybd_event。
http://www.codeproject.com/csharp/globalhook.asp 这个能帮你实现Hook
至于keybd_event很简单的。
比如这就是天霸封神斩的发招方法:
[DllImport("user32.dll", EntryPoint="keybd_event")]
public static extern void keybd_event (
byte bVk,
byte bScan,
int dwFlags,
int dwExtraInfo
);
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
private const int KEYEVENTF_KEYDOWN = 0x00;
private const int KEY_A = 0x41;
private const int KEY_S = 0x53;
private const int KEY_D = 0x44;
private const int KEY_W = 0x57;
private const int KEY_J = 0x4A;
private const int KEY_K = 0x4B;
private const int KEY_L = 0x4C;
private const int KEY_U = 0x55;
private const int KEY_I = 0x49;
private const int KEY_O = 0x4F;
#region SimulateKey
private void OneKey(byte VirtualKey, int _interval, int KeyUpDown)
{
keybd_event(VirtualKey, 0, KeyUpDown, 0);
Thread.Sleep(_interval);
}
private void TowKey(byte VirtualKey1, byte VirtualKey2, int _interval, int KeyUpDown)
{
keybd_event(VirtualKey1, 0, KeyUpDown, 0);
keybd_event(VirtualKey2, 0, KeyUpDown, 0);
Thread.Sleep(_interval);
}
#endregion SimulateKey
#region Actions
private void ToRight_Action01(int Interval)
{
int _interval = Interval;
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYDOWN);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYUP);
}
private void ToLeft_Action01(int Interval)
{
int _interval = Interval;
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_A, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_S, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_D, _interval, KEYEVENTF_KEYDOWN);
OneKey(KEY_S, _interval, KEYEVENTF_KEYUP);
OneKey(KEY_D, _interval, KEYEVENTF_KEYUP);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYDOWN);
TowKey(KEY_K, KEY_U,_interval, KEYEVENTF_KEYUP);
}
#endregion Actions