[C#]续:利用键代码自动转换生成字母键或其它键信息
补充上一篇利用键代码自动转换生成字母键或其它键信息
上一篇讲到键代码自动转换成字母键,但其它键有些是不好转的,比如F1到F12等,转换完之后把它们放到组合框或在其它地方使用,如图
总结出一个方法如下,还比较好用.
类中的语句:
using System.Collections.ObjectModel;//ObservableCollection<Keys>泛型数组引用空间
using System.Windows .Forms ;//Keys引用空间
namespace mouseclick1
{
//返回26个字母的泛型数组
class class_getKeysCollection
{
public static ObservableCollection<Keys> ReturnKeys()//返回26个字母Keys及F1至F12;
{
ObservableCollection<Keys> KeysCollections = new ObservableCollection<Keys>();
for (int i = 0; i < 26; i++)//转换26个字母键
{
string _str1 = Convert.ToString(i + 65);
Keys onekey1 = (Keys)Enum.Parse(typeof(Keys), _str1);//枚举转换
KeysCollections.Add(onekey1);
}
for (int i = 0; i < 12; i++)//转换F1到F12
{
string _str2 = "F" + (i+1).ToString();
Keys onekey2 = (Keys)Enum.Parse(typeof(Keys), _str2);
KeysCollections.Add(onekey2);
}
string _str3=Convert.ToString (32);//再增加一个空格键
Keys onekey3 = (Keys)Enum.Parse(typeof(Keys), _str3);
KeysCollections.Add(onekey3);
return KeysCollections;
}
}
}
调用:
ObservableCollection<Keys> KeysCollection = new ObservableCollection<Keys>();//快捷键集合
private void fillComboBox()
{
KeysCollection = class_getKeysCollection.ReturnKeys();
for (int i = 0; i < KeysCollection.Count; i++)
{
comboBox2.Items.Add(KeysCollection[i].ToString());
comboBox3.Items.Add(KeysCollection[i].ToString());
}
}
还想转换其它键只需要在类中继续添加即可,