如何调整 Cheet.NET 以绑定到我的 C# 表单?

所以我在设置一个按键绑定来在我的项目中执行某些操作时遇到问题(右 Alt + 左 Control)

我尝试使用一个名为 Cheet.NET 的 API,它表示它可以轻松地调整项目并允许制作可以调用函数的自定义“konami”代码。


因此,我使用 Visual Studio 中的 NuGet 包管理器安装了它,保存并重新加载了我的项目。

回来后,尝试编写一个简单的按键绑定,我已经从初始化代码中得到了很多错误:


// Initialization

var cheet = new Cheet();

myUIElement.PreviewKeyDown += cheet.OnKeyDown;


cheet.Map("↑ ↑ ↓ ↓", () => { Debug.WriteLine("Voilà!"); } );


这是我放置代码的地方:


namespace WindowsFormsApp1

{

    public partial class Form1 : Form

    {


        // Initialization

        var cheet = new Cheet();

        myUIElement.PreviewKeyDown += cheet.OnKeyDown;


        cheet.Map("↑ ↑ ↓ ↓", () => { Debug.WriteLine("Voilà!"); });


        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {


        }

    }

}

它无法识别很多代码,并且也给了我很多计算错误:


类、结构或接口成员声明中的标记“+=”无效

预期

元组必须包含至少两个元素 类、

结构或接口成员声明中的标记“+=”无效


我听说我应该将包“绑定”到我的项目,但我不知道该怎么做。


感谢任何未来的帮助。


海绵宝宝撒
浏览 117回答 3
3回答

红颜莎娜

最简单的是,您几乎可以从 github 复制/粘贴 Wpf 实现,并更改为使用适用于 Windows 表单的正确事件处理程序/枚举:using CheetNET.Core;using System;using System.Text.RegularExpressions;using System.Windows.Forms;public class Cheet : Cheet<System.Windows.Forms.Keys>{&nbsp; &nbsp; private static readonly Regex LetterKeysNamePattern = new Regex(@"^[a-z]$");&nbsp; &nbsp; private static readonly Regex NumberKeysNamePattern = new Regex(@"^[0-9]$");&nbsp; &nbsp; private static readonly Regex KeyspadNumberKeysNamePattern = new Regex(@"^kp_[0-9]$");&nbsp; &nbsp; private static readonly Regex FunctionKeysNamePattern = new Regex(@"^(?:f[1-9]|f1[0-2])$");&nbsp; &nbsp; private PreviewKeyDownEventArgs lastHandledEvent;&nbsp; &nbsp; public virtual void OnKeyDown(object sender, PreviewKeyDownEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (e == lastHandledEvent)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; OnKeyDown(e.KeyCode);&nbsp; &nbsp; &nbsp; &nbsp; lastHandledEvent = e;&nbsp; &nbsp; }&nbsp; &nbsp; protected override Keys GetKey(string KeysName)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (LetterKeysNamePattern.IsMatch(KeysName))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ParseKey(KeysName.ToUpper());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (NumberKeysNamePattern.IsMatch(KeysName))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ParseKey("D" + KeysName);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (KeyspadNumberKeysNamePattern.IsMatch(KeysName))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ParseKey(KeysName.Replace("kp_", "NumPad"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (FunctionKeysNamePattern.IsMatch(KeysName))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ParseKey(KeysName.ToUpper());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; switch (KeysName)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "left":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "L":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "←":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Left;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "up":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "U":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "↑":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Up;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "right":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "R":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "→":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Right;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "down":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "D":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "↓":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Down;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "backspace":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Back;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "tab":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Tab;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "enter":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Enter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "return":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "shift":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "⇧":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.LShiftKey;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "control":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "ctrl":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "^":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.LControlKey;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "alt":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "option":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "⌥":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Alt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "command":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "⌘":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.LWin;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "pause":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Pause;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "capslock":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.CapsLock;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "esc":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Escape;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "space":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Space;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "pageup":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.PageUp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "pagedown":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.PageDown;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "end":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.End;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "home":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Home;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "insert":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Insert;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "delete":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Delete;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "equal":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "=":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Oemplus;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "comma":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ",":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Oemcomma;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "minus":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "-":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.OemMinus;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "period":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ".":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.OemPeriod;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "kp_multiply":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Multiply;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "kp_plus":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Add;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "kp_minus":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Subtract;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "kp_decimal":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Decimal;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "kp_divide":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Keys.Divide;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException(String.Format("Could not map Keys named '{0}'.", KeysName));&nbsp; &nbsp; }&nbsp; &nbsp; private static Keys ParseKey(string KeysName)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return (Keys)Enum.Parse(typeof(Keys), KeysName);&nbsp; &nbsp; }}然后,只要将代码放入表单加载处理程序中,它就会按预期工作:protected override void OnLoad(EventArgs e){&nbsp; &nbsp; var cheet = new Cheet();&nbsp; &nbsp; PreviewKeyDown += cheet.OnKeyDown;&nbsp; &nbsp; cheet.Map("c h e a t", () => { MessageBox.Show("Voilà!"); });&nbsp; &nbsp; base.OnLoad(e);}

PIPIONE

更新(因为您已将错误输出添加到问题中):预期类、结构或接口成员声明类型中的标记“+=”无效你不能只将 C# 代码放在类的顶层,你必须遵循一个最小的结构。您可以将此类代码放置在类的构造函数方法或其他方法或静态初始化块中(如果您仅在代码中引用静态内容)下次更新(新错误之后):使用泛型类型“Cheet”需要 1 个类型参数您正在使用通用包而不是 Wpf 包:public class Cheet : Cheet<Key>, ICheet很不幸我认为他们使用相同的类名

繁星coding

存储库中有一个 WPF 演示应用程序,您可以在 Window 构造函数中进行初始化:public MainWindow(){&nbsp; &nbsp; var cheet = new Cheet();&nbsp; &nbsp; PreviewKeyDown += cheet.OnKeyDown;&nbsp; &nbsp; cheet.Map("↑ ↑ ↓ ↓ ← → ← → b a", () => { WriteLine("Voilà!"); });&nbsp; &nbsp; cheet.Map("i d d q d", () => {&nbsp; &nbsp; &nbsp; &nbsp; WriteLine("god mode enabled");&nbsp; &nbsp; });&nbsp; &nbsp; [etc.]因此,您可以使用 处理PreviewKeyDown事件cheet.OnKeyDown,并且OnKeyDownCheet 中可能会循环遍历其映射并寻找合适的映射。我设置了一个测试 WinForms 项目并添加了 Cheet.NET,如果您想将它与 WinForms 一起使用,您似乎需要做一些工作。Cheet.Core有一个Cheet<T>类,但它是抽象的。看起来T是打算成为“钥匙”类型的。Cheet.Wpf 库有一个Cheet继承自 的类Cheet<Key>,使用 WPFKey类型。看来您需要创建自己的Cheet类(很可能)继承自Cheet<System.Windows.Forms.Keys>.我想现在的问题是哪个工作量更大:为 WinForms 实现 Cheet,还是在 WPF 中重新开始您的项目
打开App,查看更多内容
随时随地看视频慕课网APP