我的方法是使用a 字典 Key是实际的Keybord 按键 和价值是一个 诠释? 表示可以绑定到自定义输入的函数之一。
Dictionnary<Keys, int?> shortcutDictionnary = new Dictionary<Keys, int?>(); // Add a new Keys shortcutDictionary.Add(Keys.A, 1); // Change a Keys value (change shortcut bounded to it) shortcutDictionary[Keys.A] = 4;
要匹配那些 诠释? 有了这些功能,你只需要使用一个 开关 :
int? num = null; if (this.shortcutDictionary.TryGetValue(keyPressed, out num)) { switch (num) { case 1: attack(); break; case 2: defend(); break; case 3: hide(); break; case 4: dance(); break; default: Console.WriteLine("Key not bounded"); break; } }
我也在下面的代码中使用了一个 枚举 而不是直接使用 按键 为了我的 字典 。 这样,我可以选择哪个 按键 可以是有界的,哪个不可以。 我的代码来自Winform应用程序,作为一个例子,我只用了4 按键 (A,B,C,D)可以绑定,一个可以轻松改变边界(L),但我相信你可以弄清楚如何用任何其他方法轻松改变绑定。此外,当我使用WindowsForm时,我不得不设置 的KeyPreview =真。 这是我的代码:
using System; using System.Collections.Generic; using System.Windows.Forms; namespace Project { public enum UsableKeys { A = Keys.A, B = Keys.B, C = Keys.C, D = Keys.D, } public partial class Form1 : Form { Dictionary<UsableKeys, int?> shortcutDictionary = new Dictionary<UsableKeys, int?>(); public Form1() { InitializeComponent(); foreach (UsableKeys key in Enum.GetValues(typeof(UsableKeys))) { // You may add default shortcut here this.shortcutDictionary.Add(key, null); } } private void Form1_KeyDown(object sender, KeyEventArgs e) { UsableKeys keyPressed = (UsableKeys)e.KeyCode; if (this.shortcutDictionary.ContainsKey(keyPressed)) { executeAction(keyPressed); e.Handled = true; e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.L) { switch (this.shortcutDictionary[UsableKeys.A]) { case 1: this.shortcutDictionary[UsableKeys.A] = 4; this.shortcutDictionary[UsableKeys.B] = 3; this.shortcutDictionary[UsableKeys.C] = 2; this.shortcutDictionary[UsableKeys.D] = 1; break; case null: this.shortcutDictionary[UsableKeys.A] = 1; this.shortcutDictionary[UsableKeys.B] = 2; this.shortcutDictionary[UsableKeys.C] = 3; this.shortcutDictionary[UsableKeys.D] = 4; break; case 4: this.shortcutDictionary[UsableKeys.A] = null; this.shortcutDictionary[UsableKeys.B] = null; this.shortcutDictionary[UsableKeys.C] = null; this.shortcutDictionary[UsableKeys.D] = null; break; } e.Handled = true; e.SuppressKeyPress = true; } } private void executeAction(UsableKeys keyPressed) { int? num = null; if (this.shortcutDictionary.TryGetValue(keyPressed, out num)) { switch (num) { case 1: attack(); break; case 2: defend(); break; case 3: hide(); break; case 4: dance(); break; default: Console.WriteLine("Key not bounded"); break; } } } private void attack() { Console.WriteLine("Player swing his word"); } private void defend() { Console.WriteLine("Player raise his shield"); } private void hide() { Console.WriteLine("Player sneak around"); } private void dance() { Console.WriteLine("Player start to dance"); } } }
使用此代码,输出将如下:
// Press A, B, C or D "Key not bounded" // Press L // Press A "Player swing his word" // Press B "Player raise his shield" // Press C "Player sneak around" // Press D "Player start to dance" // Press L // Press A "Player start to dance" // Press B "Player sneak around" // Press C "Player raise his shield" // Press D "Player swing his sword" // Press L // Press A, B, C or D "Key not bounded"
例如,在运行时更改键绑定:
// Create a new Dictionary for shortcuts Dictionary<UsableKeys, int?> shortcutDictionary = new Dictionary<UsableKeys, int?>(); // Add a pair key/value that bind A to attack() shortcutDictionary.Add(UsableKey.A, 1); // Add a pair Key/value that bind B to defend() shortcutDictionary.Add(UsableKey.B, 2); // Now, if you press A, attack() will be called shortcutDictionary[UsableKey.A] = 2; // Now if you press A or B, defend() will be called shortcutDictionary[UsableKey.B] = null; // Now B isn't bind to any function, so only A is binded to defend();
使用此方法,您无法将多个函数绑定到一个 按键 虽然你可以绑定多个 按键 到一个函数(如果你想反转,只需交换键/值 字典 并调整代码以匹配此)。 我不知道这是否是最佳方式,但它不是意大利面条代码,而且效果很好。