分享

发现:Hook住一个key的消息,用keybd_event模拟键盘 - mY aSp.ne...

 donixli1314 2008-07-08
作者:jamesfay

我做过一个侍魂2   PC版的自动发招程序,大概的方法就是Hook住一个key的消息,然后用keybd_event模拟键盘的消息。我觉得sendMessage不一定有用,因为很多时候directX的程序是不看winMessage的,而是直接读设备。所以还是推荐用keybd_event。  
   
  http://www./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  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多