首页 > 开发 > 综合 > 正文

用C#程序实现键盘和鼠标的模拟

2024-07-21 02:18:49
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • using system;
    using system.runtime.interopservices;
    using system.text;

    namespace consoleapplication8{
    class class1{
    [stathread]
    static void main(string[] args){
    // display current status of keys.
    console.writeline(
    "**before**/r/ncap: {0}/r/nscr: {1}/r/nnum: {2}",
    keyboard.getstate(virtualkeys.vk_capital)?"on":"off",
    keyboard.getstate(virtualkeys.vk_scroll)?"on":"off",
    keyboard.getstate(virtualkeys.vk_numlock)?"on":"off"
    );
    // toggle all the keys:
    keyboard.setstate(
    virtualkeys.vk_capital,
    !keyboard.getstate(virtualkeys.vk_capital)
    );
    keyboard.setstate(
    virtualkeys.vk_scroll,
    !keyboard.getstate(virtualkeys.vk_scroll)
    );
    keyboard.setstate(
    virtualkeys.vk_numlock,
    !keyboard.getstate(virtualkeys.vk_numlock)
    );
    // display new status of keys.
    console.writeline(
    "/r/n**after**/r/ncap: {0}/r/nscr: {1}/r/nnum: {2}",
    keyboard.getstate(virtualkeys.vk_capital)?"on":"off",
    keyboard.getstate(virtualkeys.vk_scroll)?"on":"off",
    keyboard.getstate(virtualkeys.vk_numlock)?"on":"off"
    );
    console.readline();
    }
    }
    public enum virtualkeys: byte{
    vk_numlock = 0x90,
    vk_scroll = 0x91,
    vk_capital = 0x14
    }
    class keyboard{
    const uint keyeventf_extendedkey = 0x1;
    const uint keyeventf_keyup = 0x2;
    [dllimport("user32.dll")]
    static extern short getkeystate(int nvirtkey);
    [dllimport("user32.dll")]
    static extern void keybd_event(
    byte bvk,
    byte bscan,
    uint dwflags,
    uint dwextrainfo
    );
    public static bool getstate(virtualkeys key){
    return (getkeystate((int)key)==1);
    }
    public static void setstate(virtualkeys key, bool state){
    if(state!=getstate(key)){
    keybd_event(
    (byte)key,
    0x45,
    keyeventf_extendedkey | 0,
    0
    );
    keybd_event(
    (byte)key,
    0x45,
    keyeventf_extendedkey | keyeventf_keyup,
    0
    );
    }
    }
    }
    }


    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表