【C#】字符串转换为键盘按键键码值,模拟键盘按键输入,附按键键码表

文章目录

  • 前言
  • 1. 键盘键码值
  • 2. 转换方法
  • 3. 测试
  • 4. 模拟键盘按键输入
  • 5. 附:按键键码表


前言

网上搜了一圈没现成的,干脆写一个方便使用好了。
环境:.Net Framework 4.0,VS2010

键盘键码值参考了:
C#窗体如何通过keybd_event()函数模拟键盘按键(组合键)产生事件


1. 键盘键码值

为了调用windows系统的keybd_event,键码的值都是byte类型

/// 
/// 枚举类型,键盘按键的键码值
/// 
public enum KeyValueEnum : byte
{
    //常用键:字母键A到Z
    vbKeyA = 65 ,
    vbKeyB = 66 ,
    vbKeyC = 67 ,
    vbKeyD = 68 ,
    vbKeyE = 69 ,
    vbKeyF = 70 ,
    vbKeyG = 71 ,
    vbKeyH = 72 ,
    vbKeyI = 73 ,
    vbKeyJ = 74 , 
    vbKeyK = 75 ,
    vbKeyL = 76 ,
    vbKeyM = 77 ,
    vbKeyN = 78 ,
    vbKeyO = 79 ,
    vbKeyP = 80 ,
    vbKeyQ = 81 ,
    vbKeyR = 82 ,
    vbKeyS = 83 ,
    vbKeyT = 84 ,
    vbKeyU = 85 ,
    vbKeyV = 86 ,
    vbKeyW = 87 ,
    vbKeyX = 88 ,
    vbKeyY = 89 ,
    vbKeyZ = 90 ,

    // 数字键盘:0 - 9
    vbKey0 = 48 ,    // 0 键
    vbKey1 = 49 ,    // 1 键
    vbKey2 = 50 ,    // 2 键
    vbKey3 = 51 ,    // 3 键
    vbKey4 = 52 ,    // 4 键
    vbKey5 = 53 ,    // 5 键
    vbKey6 = 54 ,    // 6 键
    vbKey7 = 55 ,    // 7 键
    vbKey8 = 56 ,    // 8 键
    vbKey9 = 57 ,    // 9 键

     // 小键盘按键
     vbKeyNumpad0 = 0x60 ,    // 0 键
     vbKeyNumpad1 = 0x61 ,    // 1 键
     vbKeyNumpad2 = 0x62 ,    // 2 键
     vbKeyNumpad3 = 0x63 ,    // 3 键
     vbKeyNumpad4 = 0x64 ,    // 4 键
     vbKeyNumpad5 = 0x65 ,    // 5 键
     vbKeyNumpad6 = 0x66 ,    // 6 键
     vbKeyNumpad7 = 0x67 ,    // 7 键
     vbKeyNumpad8 = 0x68 ,    // 8 键
     vbKeyNumpad9 = 0x69 ,    // 9 键
     vbKeyMultiply = 0x6A ,   // MULTIPLICATIONSIGN(*)键
     vbKeyAdd = 0x6B ,        // PLUS SIGN(+) 键
     vbKeySeparator = 0x6C ,  // ENTER 键
     vbKeySubtract = 0x6D ,   // MINUS SIGN(-) 键
     vbKeyDecimal = 0x6E ,    // DECIMAL POINT(.) 键
     vbKeyDivide = 0x6F ,     // DIVISION SIGN(/) 键


     // F1到F12按键
     vbKeyF1 = 0x70 ,   // F1 键
     vbKeyF2 = 0x71 ,   // F2 键
     vbKeyF3 = 0x72 ,   // F3 键
     vbKeyF4 = 0x73 ,   // F4 键
     vbKeyF5 = 0x74 ,   // F5 键
     vbKeyF6 = 0x75 ,   // F6 键
     vbKeyF7 = 0x76 ,   // F7 键
     vbKeyF8 = 0x77 ,   // F8 键
     vbKeyF9 = 0x78 ,   // F9 键
     vbKeyF10 = 0x79 ,  // F10 键
     vbKeyF11 = 0x7A ,  // F11 键
     vbKeyF12 = 0x7B ,  // F12 键

     // 其他常用按键
     vbKeyLButton = 0x1,    // 鼠标左键
     vbKeyRButton = 0x2,    // 鼠标右键
     vbKeyCancel = 0x3,     // CANCEL 键
     vbKeyMButton = 0x4,    // 鼠标中键
     vbKeyBack = 0x8,       // BACKSPACE 键
     vbKeyTab = 0x9,        // TAB 键
     vbKeyClear = 0xC,      // CLEAR 键
     vbKeyReturn = 0xD,     // ENTER 键
     vbKeyShift = 0x10,     // SHIFT 键
     vbKeyControl = 0x11,   // CTRL 键
     vbKeyAlt = 18,         // Alt 键  (键码18)
     vbKeyMenu = 0x12,      // MENU 键
     vbKeyPause = 0x13,     // PAUSE 键
     vbKeyCapital = 0x14,   // CAPS LOCK 键
     vbKeyEscape = 0x1B,    // ESC 键
     vbKeySpace = 0x20,     // SPACEBAR 键
     vbKeyPageUp = 0x21,    // PAGE UP 键
     vbKeyEnd = 0x23,       // End 键
     vbKeyHome = 0x24,      // HOME 键
     vbKeyLeft = 0x25,      // LEFT ARROW 键
     vbKeyUp = 0x26,        // UP ARROW 键
     vbKeyRight = 0x27,     // RIGHT ARROW 键
     vbKeyDown = 0x28,      // DOWN ARROW 键
     vbKeySelect = 0x29,    // Select 键
     vbKeyPrint = 0x2A,     // PRINT SCREEN 键
     vbKeyExecute = 0x2B,   // EXECUTE 键
     vbKeySnapshot = 0x2C,  // SNAPSHOT 键
     vbKeyDelete = 0x2E,    // Delete 键
     vbKeyHelp = 0x2F,      // HELP 键
     vbKeyNumlock = 0x90   // NUM LOCK 键
}


2. 转换方法

遍历字符串的每个字符:
先把每个字符都转换为大写:str[i].ToString().ToUpper()
再给字符加上前缀"vbKey":string.Concat(“vbKey”, str[i].ToString().ToUpper())
然后根据每个生成的字符串(比如“a”变成了“vbKeyA”)到枚举类型KeyValueEnum中查找对应键码:(byte)Enum.Parse(typeof(KeyValueEnum), str)
键码添加到集合kvList:kvList.Add
最后返回包含了字符串中每个字符的键码的集合kvList。
C#中如何将字符串string转换成枚举类型 – typeof()以及Enum.Parse()的用法
*另:try…catch之类的请先加在这个方法外部,检测待转换字符串内容的那些还是别放进来吧。

/// 
/// 字符串转换为键码值的集合
/// 
/// 待转换的字符串
/// 返回键码集合
public ArrayList GetKeyValues(string str)
{
    ArrayList kvList = new ArrayList();
    for (int i = 0; i < str.Length; i++ )
    {
        kvList.Add((byte)Enum.Parse(
            typeof(KeyValueEnum), string.Concat("vbKey", str[i].ToString().ToUpper())));
    }
    return kvList;
}

3. 测试

在WPF使用,直接弹窗口查看查找到的键码。在Form或其他使用的话请自己修改弹窗语句。

// 待转换的字符串
string str = "ab123t";
// 遍历字符串的每个字符
for (int i = 0; i < str.Length; i++)
{
	MessageBox.Show(str[i] + ": " + GetKeyValues(str)[i].ToString());
}

4. 模拟键盘按键输入

即代替手动按键盘按键。

string userName = "user123";
for (int i = 0; i < userName.Length; i++)
{
    InputOneKey((byte)GetKeyValues(userName)[i]);
}

/// 
/// 输入键盘上的一个字符,即按下一次键盘上的一个按键
/// 
/// 用键码表示
public void InputOneKey(byte keyValue) {
	// 按下
	keybd_event(keyValue, 0, 0, 0); 
	// 释放      
	keybd_event(keyValue, 0, 2, 0);        
}

模拟键盘输入API:keybd_event:

/// 
/// 模拟键盘输入API:keybd_event
/// 
/// 虚拟键值
/// 一般为0
/// 整数类型:按下0,释放2
/// 整数类型:一般情况下设成为0
[DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void keybd_event(
	byte bVk,    //虚拟键值
	byte bScan,  // 一般为0
	int dwFlags,  //这里是整数类型  0 为按下,2为释放
	int dwExtraInfo  //这里是整数类型 一般情况下设成为 0
);

5. 附:按键键码表

网上的不够完整,这里加了剩下的符号按键进去(WIN键之类)。

微软在线文档:Keycode constants
微软在线文档:Virtual-Key Codes

#region 键码 bVk参数 常量定义
/// 
/// 鼠标左键
/// 
public const byte vbKeyLButton = 0x1;    // 鼠标左键
/// 
/// 鼠标右键
/// 
public const byte vbKeyRButton = 0x2;    // 鼠标右键
/// 
/// CANCEL 键
/// 
public const byte vbKeyCancel = 0x3;     // CANCEL 键
/// 
/// 鼠标中键
/// 
public const byte vbKeyMButton = 0x4;    // 鼠标中键
/// 
/// BACKSPACE 键
/// 
public const byte vbKeyBack = 0x8;       // BACKSPACE 键
/// 
/// TAB 键
/// 
public const byte vbKeyTab = 0x9;        // TAB 键
public const byte vbKeyClear = 0xC;      // CLEAR 键
/// 
/// ENTER 键
/// 
public const byte vbKeyReturn = 0xD;     // ENTER 键
public const byte vbKeyShift = 0x10;     // SHIFT 键
public const byte vbKeyControl = 0x11;   // CTRL 键
public const byte vbKeyAlt = 18;         // Alt 键  (键码18)
public const byte vbKeyMenu = 0x12;      // MENU 键
public const byte vbKeyPause = 0x13;     // PAUSE 键
/// 
/// CAPS LOCK 键
/// 
public const byte vbKeyCapital = 0x14;   // CAPS LOCK 键
public const byte vbKeyEscape = 0x1B;    // ESC 键
public const byte vbKeySpace = 0x20;     // SPACEBAR 键
public const byte vbKeyPageUp = 0x21;    // PAGE UP 键
public const byte vbKeyEnd = 0x23;       // End 键
public const byte vbKeyHome = 0x24;      // HOME 键
public const byte vbKeyLeft = 0x25;      // LEFT ARROW 键
public const byte vbKeyUp = 0x26;        // UP ARROW 键
public const byte vbKeyRight = 0x27;     // RIGHT ARROW 键
public const byte vbKeyDown = 0x28;      // DOWN ARROW 键
public const byte vbKeySelect = 0x29;    // Select 键
public const byte vbKeyPrint = 0x2A;     // PRINT SCREEN 键
public const byte vbKeyExecute = 0x2B;   // EXECUTE 键
public const byte vbKeySnapshot = 0x2C;  // SNAPSHOT 键
public const byte vbKeyDelete = 0x2E;    // Delete 键
public const byte vbKeyHelp = 0x2F;      // HELP 键
public const byte vbKeyNumlock = 0x90;   // NUM LOCK 键

/// 
/// 左边的 WIN 键
/// 
public const byte vbKeyLWin = 0x91;     // 左边的 WIN 键
/// 
/// 右边的 WIN 键
/// 
public const byte vbKeyRWin = 0x91;     // 右边的 WIN 键
/// 
/// 右Ctrl左边键,点击相当于点击鼠标右键,会弹出快捷菜单
/// 
public const byte vbKeyApps = 0x93;     // 右Ctrl左边键,点击相当于点击鼠标右键,会弹出快捷菜单
/// 
/// 分号 ; 
/// 
public const byte vbKeySemicolon = 0xBA;    // ;(分号)
/// 
/// 等号/加号 键
/// 
public const byte vbKeyEqual = 0xBB;        // 等号/加号键

// 常用键:字母键 A-Z
public const byte vbKeyA = 65;
public const byte vbKeyB = 66;
public const byte vbKeyC = 67;
public const byte vbKeyD = 68;
public const byte vbKeyE = 69;
public const byte vbKeyF = 70;
public const byte vbKeyG = 71;
public const byte vbKeyH = 72;
public const byte vbKeyI = 73;
public const byte vbKeyJ = 74;
public const byte vbKeyK = 75;
public const byte vbKeyL = 76;
public const byte vbKeyM = 77;
public const byte vbKeyN = 78;
public const byte vbKeyO = 79;
public const byte vbKeyP = 80;
public const byte vbKeyQ = 81;
public const byte vbKeyR = 82;
public const byte vbKeyS = 83;
public const byte vbKeyT = 84;
public const byte vbKeyU = 85;
public const byte vbKeyV = 86;
public const byte vbKeyW = 87;
public const byte vbKeyX = 88;
public const byte vbKeyY = 89;
public const byte vbKeyZ = 90;

// 数字键盘:0 - 9
public const byte vbKey0 = 48;    // 0 键
public const byte vbKey1 = 49;    // 1 键
public const byte vbKey2 = 50;    // 2 键
public const byte vbKey3 = 51;    // 3 键
public const byte vbKey4 = 52;    // 4 键
public const byte vbKey5 = 53;    // 5 键
public const byte vbKey6 = 54;    // 6 键
public const byte vbKey7 = 55;    // 7 键
public const byte vbKey8 = 56;    // 8 键
public const byte vbKey9 = 57;    // 9 键

// 小键盘
public const byte vbKeyNumpad0 = 0x60;    //0 键
public const byte vbKeyNumpad1 = 0x61;    //1 键
public const byte vbKeyNumpad2 = 0x62;    //2 键
public const byte vbKeyNumpad3 = 0x63;    //3 键
public const byte vbKeyNumpad4 = 0x64;    //4 键
public const byte vbKeyNumpad5 = 0x65;    //5 键
public const byte vbKeyNumpad6 = 0x66;    //6 键
public const byte vbKeyNumpad7 = 0x67;    //7 键
public const byte vbKeyNumpad8 = 0x68;    //8 键
public const byte vbKeyNumpad9 = 0x69;    //9 键
/// 
/// 小键盘,MULTIPLICATIONSIGN(*)键
/// 
public const byte vbKeyMultiply = 0x6A;   // MULTIPLICATIONSIGN(*)键
/// 
/// 小键盘,PLUS SIGN(+) 键
/// 
public const byte vbKeyAdd = 0x6B;        // PLUS SIGN(+) 键
/// 
/// 小键盘,ENTER 键
/// 
public const byte vbKeySeparator = 0x6C;  // ENTER 键
/// 
/// 小键盘,MINUS SIGN(-) 键
/// 
public const byte vbKeySubtract = 0x6D;   // MINUS SIGN(-) 键
/// 
/// 小键盘,DECIMAL POINT(.) 键
/// 
public const byte vbKeyDecimal = 0x6E;    // DECIMAL POINT(.) 键
/// 
/// 小键盘,DIVISION SIGN(/) 键
/// 
public const byte vbKeyDivide = 0x6F;     // DIVISION SIGN(/) 键
/// 
/// ,键(逗号)
/// 
public const byte vbKeyComma = 0xBC;      // ,键(逗号)
/// 
/// -键(减号)
/// 
public const byte vbKeyMinus = 0xBD;      // -键(减号)
/// 
/// .键(句号)
/// 
public const byte vbKeyPeriod = 0xBE;   // .键(句号)
/// 
/// 斜杠/和问号? 键
/// 
public const byte vbKeySlash = 0xBF;    // 斜杠/和问号? 键
/// 
/// `键(Esc下面)
/// 
public const byte vbKeyTilde = 0xC0;    // `键(Esc下面)
/// 
/// [ 键
/// 
public const byte vbKeyLeftBracket = 0xDB;  // [ 键
/// 
/// ] 键
/// 
public const byte vbKeyRightBracket = 0xDD;  // ] 键
/// 
/// 反斜杠\ 键
/// 
public const byte vbKeyBackSlash = 0xDC;    // 反斜杠\键
/// 
/// 单/双引号 ' " 键
/// 
public const byte vbKeyQuote = 0xDE;        // 单/双引号 ' " 键


//F1到F12按键
public const byte vbKeyF1 = 0x70;   //F1 键
public const byte vbKeyF2 = 0x71;   //F2 键
public const byte vbKeyF3 = 0x72;   //F3 键
public const byte vbKeyF4 = 0x73;   //F4 键
public const byte vbKeyF5 = 0x74;   //F5 键
public const byte vbKeyF6 = 0x75;   //F6 键
public const byte vbKeyF7 = 0x76;   //F7 键
public const byte vbKeyF8 = 0x77;   //F8 键
public const byte vbKeyF9 = 0x78;   //F9 键
public const byte vbKeyF10 = 0x79;  //F10 键
public const byte vbKeyF11 = 0x7A;  //F11 键
public const byte vbKeyF12 = 0x7B;  //F12 键

#endregion

你可能感兴趣的:(C#,WPF,.NET)