• C#中使用WinIO模拟键盘鼠标(转)


    原文地址在哪忘了,对不起额。


      public class WinIOLab
        {
            private const int KBC_KEY_CMD = 0x64;
            private const int KBC_KEY_DATA = 0x60;
            [DllImport("winio32.dll")]
            private static extern bool InitializeWinIo();
            [DllImport("winio32.dll")]
            private static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
            [DllImport("winio32.dll")]
            private static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
            [DllImport("winio32.dll")]
            private static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
            [DllImport("winio32.dll")]
            private static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
            [DllImport("winio32.dll")]
            private static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
            [DllImport("winio32.dll")]
            private static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
            [DllImport("winio32.dll")]
            private static extern void ShutdownWinIo();
            [DllImport("user32.dll")]
            private static extern int MapVirtualKey(uint Ucode, uint uMapType);


            private WinIOLab()
            {
                IsInitialize = true;
            }
            public static void Initialize()
            {
                if (InitializeWinIo())
                {
                    KBCWait4IBE();
                    IsInitialize = true;
                }
            }
            public static void Shutdown()
            {
                if (IsInitialize)
                    ShutdownWinIo();
                IsInitialize = false;
            }

            private static bool IsInitialize { getset; }

            ///等待键盘缓冲区为空
            private static void KBCWait4IBE()
            {
                int dwVal = 0;
                do
                {
                    bool flag = GetPortVal((IntPtr)0x64out dwVal, 1);
                }
                while ((dwVal & 0x2) > 0);
            }
            /// 模拟键盘标按下
            public static void KeyDown(Keys vKeyCoad)
            {
                if (!IsInitialize) return;

                int btScancode = 0;
                btScancode = MapVirtualKey((uint)vKeyCoad, 0);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_CMD, (IntPtr)0xD21);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_DATA, (IntPtr)0x601);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_CMD, (IntPtr)0xD21);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_DATA, (IntPtr)btScancode, 1);
            }
            /// 模拟键盘弹出
            public static void KeyUp(Keys vKeyCoad)
            {
                if (!IsInitialize) return;

                int btScancode = 0;
                btScancode = MapVirtualKey((uint)vKeyCoad, 0);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_CMD, (IntPtr)0xD21);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_DATA, (IntPtr)0x601);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_CMD, (IntPtr)0xD21);
                KBCWait4IBE();
                SetPortVal(KBC_KEY_DATA, (IntPtr)(btScancode | 0x80), 1);
            }
        }

    调用


     WinIOLab.Initialize(); // 注册
     WinIOLab.KeyDown(Keys.A); // 按下A
     WinIOLab.KeyUp(Keys.A); // 松开A
     WinIOLab.Shutdown(); // 用完后注销


  • 相关阅读:
    浏览器缓存学习
    文件上传
    compass与css sprite(雪碧图)
    记录一下删除过长目录的方法
    JavaScript学习之 倒计时
    HTML/CSS学习之 三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化
    JavaScript学习之setTimeout
    JavaScript实现,控制一个文本框只能输入正整数,如输入不符合条件则文本框全部字体标红
    关于本地文件请求json文件
    CSS3+HTML5特效9
  • 原文地址:https://www.cnblogs.com/zhuiyi/p/2584645.html
Copyright © 2020-2023  润新知