码迷,mamicode.com
首页 > Windows程序 > 详细

关于WinIO.DLL的键盘输入模拟

时间:2014-11-13 18:45:57      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   使用   sp   strong   div   

关于WinIO.DLL的键盘输入模拟


  最近在找键盘模拟的方式,最后在网上找到了一个WinIO.DLL的IO键盘模拟按键的方式。但是居然那个方法是有问题的。我造了全局的hook监视键盘信息,发现它只是有keyDown和keyPress的消息,keyUp的信息是up了一个zoom的键。
  搞了2天终于找到了正确的方式,以下是代码(使用需要winIo.dll,winIo.vxd,winIo.sys):
bubuko.com,布布扣
bubuko.com,布布扣    public class WinIOLab
bubuko.com,布布扣    {
bubuko.com,布布扣        private const int KBC_KEY_CMD = 0x64;
bubuko.com,布布扣        private const int KBC_KEY_DATA = 0x60;
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern bool InitializeWinIo();
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
bubuko.com,布布扣        [DllImport("winio.dll")]
bubuko.com,布布扣        private static extern void ShutdownWinIo();
bubuko.com,布布扣        [DllImport("user32.dll")]
bubuko.com,布布扣        private static extern int MapVirtualKey(uint Ucode, uint uMapType);
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣        private WinIOLab()
bubuko.com,布布扣        {
bubuko.com,布布扣            IsInitialize = true;
bubuko.com,布布扣        }
bubuko.com,布布扣        public static void Initialize()
bubuko.com,布布扣        {
bubuko.com,布布扣            if (InitializeWinIo())
bubuko.com,布布扣            {
bubuko.com,布布扣                KBCWait4IBE();
bubuko.com,布布扣                IsInitialize = true;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣        public static void Shutdown()
bubuko.com,布布扣        {
bubuko.com,布布扣            if (IsInitialize)
bubuko.com,布布扣                ShutdownWinIo();
bubuko.com,布布扣            IsInitialize = false;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        private static bool IsInitialize { get; set; }
bubuko.com,布布扣
bubuko.com,布布扣        ///等待键盘缓冲区为空
bubuko.com,布布扣        private static void KBCWait4IBE()
bubuko.com,布布扣        {
bubuko.com,布布扣            int dwVal = 0;
bubuko.com,布布扣            do
bubuko.com,布布扣            {
bubuko.com,布布扣                bool flag = GetPortVal((IntPtr)0x64, out dwVal, 1);
bubuko.com,布布扣            }
bubuko.com,布布扣            while ((dwVal & 0x2) > 0);
bubuko.com,布布扣        }
bubuko.com,布布扣        /// 模拟键盘标按下
bubuko.com,布布扣        public static void KeyDown(Keys vKeyCoad)
bubuko.com,布布扣        {
bubuko.com,布布扣            if (!IsInitialize) return;
bubuko.com,布布扣
bubuko.com,布布扣            int btScancode = 0;
bubuko.com,布布扣            btScancode = MapVirtualKey((uint)vKeyCoad, 0);
bubuko.com,布布扣            KBCWait4IBE();
bubuko.com,布布扣            SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1);
bubuko.com,布布扣            KBCWait4IBE();
bubuko.com,布布扣            SetPortVal(KBC_KEY_DATA, (IntPtr)0x60, 1);
bubuko.com,布布扣            KBCWait4IBE(); 
bubuko.com,布布扣            SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1);
bubuko.com,布布扣            KBCWait4IBE();
bubuko.com,布布扣            SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);
bubuko.com,布布扣        }
bubuko.com,布布扣        /// 模拟键盘弹出
bubuko.com,布布扣        public static void KeyUp(Keys vKeyCoad)
bubuko.com,布布扣        {
bubuko.com,布布扣            if (!IsInitialize) return;
bubuko.com,布布扣
bubuko.com,布布扣            int btScancode = 0;
bubuko.com,布布扣            btScancode = MapVirtualKey((uint)vKeyCoad, 0);
bubuko.com,布布扣            KBCWait4IBE(); 
bubuko.com,布布扣            SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1); 
bubuko.com,布布扣            KBCWait4IBE();
bubuko.com,布布扣            SetPortVal(KBC_KEY_DATA, (IntPtr)0x60, 1);
bubuko.com,布布扣            KBCWait4IBE(); 
bubuko.com,布布扣            SetPortVal(KBC_KEY_CMD, (IntPtr)0xD2, 1); 
bubuko.com,布布扣            KBCWait4IBE();
bubuko.com,布布扣            SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode | 0x80), 1);
bubuko.com,布布扣        }
bubuko.com,布布扣    }
bubuko.com,布布扣

 

    keyDown和keyUp的信息都是用0x60,而keyUp则是在Key加上了 or 0x80 而已。

  但是使用winIO.dll的办法在Win7 64x 上用不了的,根本注册不了驱动,不知有没有人研究得出解决办法。有的话请联系我:Anima.My@Gmail.com,谢谢。

 

关于WinIO.DLL的键盘输入模拟

标签:blog   http   io   ar   os   使用   sp   strong   div   

原文地址:http://www.cnblogs.com/honeynm/p/4095428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!