zoukankan      html  css  js  c++  java
  • 字符串转键盘码

    原内容  http://www.cnblogs.com/ccqin/articles/2317832.html

    整理了 键盘码 

    int KEYEVENTF_KEYUP = 2;
    byte VK_SHIFT = 16;
    byte VK_SPACE = 32;
    /// <summary>
    /// 模拟输入
    /// </summary>
    /// <param name="str"></param>
    public void KeyPress(string str)
    {
    for (int i = 0; i < str.Length; i++)//每一个字符分别转成ASCII
    {
    char key = str[i];
    if (key == ' ')
    {
    keybd_event(VK_SPACE, 0, 0, 0);
    keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (key >= '0' && key <= '9')
    {
    keybd_event((byte)VkKeyScanA(key), 0, 0, 0);
    keybd_event((byte)VkKeyScanA(key), 0, KEYEVENTF_KEYUP, 0);
    }
    else if (key >= 'A' && key <= 'Z')
    {
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)VkKeyScanA(key), 0, 0, 0);
    keybd_event((byte)VkKeyScanA(key), 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (key >= 'a' && key <= 'z')
    {
    keybd_event((byte)VkKeyScanA(key), 0, 0, 0);
    keybd_event((byte)VkKeyScanA(key), 0, KEYEVENTF_KEYUP, 0);
    }
    else if (key == '\t')
    {
    keybd_event((byte)Keys.Tab, 0, 0, 0);
    keybd_event((byte)Keys.Tab, 0, KEYEVENTF_KEYUP, 0);
    }
    else
    {
    switch (key)
    {
    #region keys
    case '\t':
    keybd_event((byte)Keys.Tab, 0, 0, 0);
    keybd_event((byte)Keys.Tab, 0, KEYEVENTF_KEYUP, 0);
    break;
    case ',':
    keybd_event((byte)188, 0, 0, 0);
    keybd_event((byte)188, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '<':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)188, 0, 0, 0);
    keybd_event((byte)188, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '.':
    keybd_event((byte)190, 0, 0, 0);
    keybd_event((byte)190, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '>':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)190, 0, 0, 0);
    keybd_event((byte)190, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '/':
    keybd_event((byte)191, 0, 0, 0);
    keybd_event((byte)191, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '?':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)191, 0, 0, 0);
    keybd_event((byte)191, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '\\':
    keybd_event((byte)220, 0, 0, 0);
    keybd_event((byte)220, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '|':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)220, 0, 0, 0);
    keybd_event((byte)220, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case ';':
    keybd_event((byte)186, 0, 0, 0);
    keybd_event((byte)186, 0, KEYEVENTF_KEYUP, 0);
    break;
    case ':':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)186, 0, 0, 0);
    keybd_event((byte)186, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '\'':
    keybd_event((byte)222, 0, 0, 0);
    keybd_event((byte)222, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '"':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)222, 0, 0, 0);
    keybd_event((byte)222, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '[':
    keybd_event((byte)219, 0, 0, 0);
    keybd_event((byte)219, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '{':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)219, 0, 0, 0);
    keybd_event((byte)219, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case ']':
    keybd_event((byte)219, 0, 0, 0);
    keybd_event((byte)219, 0, KEYEVENTF_KEYUP, 0);
    break;
    case ' ':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)221, 0, 0, 0);
    keybd_event((byte)221, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '-':
    keybd_event((byte)189, 0, 0, 0);
    keybd_event((byte)189, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '_':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)189, 0, 0, 0);
    keybd_event((byte)189, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '=':
    keybd_event((byte)187, 0, 0, 0);
    keybd_event((byte)187, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '+':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)187, 0, 0, 0);
    keybd_event((byte)187, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '`':
    keybd_event((byte)192, 0, 0, 0);
    keybd_event((byte)192, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '~':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)192, 0, 0, 0);
    keybd_event((byte)192, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '!':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)49, 0, 0, 0);
    keybd_event((byte)49, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '@':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)50, 0, 0, 0);
    keybd_event((byte)50, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '#':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)51, 0, 0, 0);
    keybd_event((byte)51, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '$':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)52, 0, 0, 0);
    keybd_event((byte)52, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '%':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)53, 0, 0, 0);
    keybd_event((byte)53, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '^':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)54, 0, 0, 0);
    keybd_event((byte)54, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '&':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)55, 0, 0, 0);
    keybd_event((byte)55, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '*':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)56, 0, 0, 0);
    keybd_event((byte)56, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case '(':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)57, 0, 0, 0);
    keybd_event((byte)57, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    case ')':
    keybd_event(VK_SHIFT, 0, 0, 0);
    keybd_event((byte)48, 0, 0, 0);
    keybd_event((byte)48, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
    break;
    #endregion
    }
    }
    }
    }
  • 相关阅读:
    express开发实例
    node.js操作mongoDB数据库
    Mysql JDBC Url参数说明useUnicode=true&characterEncoding=UTF-8
    Hibernate 使用说明
    mysql 时间戳与日期格式的相互转换
    MOOC即Massive Open Online Course的缩写
    CentOS+Apache+php无法访问redis的解决方法
    CentOS 6.5下Redis安装详细步骤
    Linux进程基础
    CentOS如何查看端口是被哪个应用/进程占用
  • 原文地址:https://www.cnblogs.com/ccqin/p/2318796.html
Copyright © 2011-2022 走看看