亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何調整 Cheet.NET 以綁定到我的 C# 表單?

如何調整 Cheet.NET 以綁定到我的 C# 表單?

C#
海綿寶寶撒 2023-08-20 09:49:39
所以我在設置一個按鍵綁定來在我的項目中執行某些操作時遇到問題(右 Alt + 左 Control)我嘗試使用一個名為 Cheet.NET 的 API,它表示它可以輕松地調整項目并允許制作可以調用函數的自定義“konami”代碼。因此,我使用 Visual Studio 中的 NuGet 包管理器安裝了它,保存并重新加載了我的項目?;貋砗?,嘗試編寫一個簡單的按鍵綁定,我已經從初始化代碼中得到了很多錯誤:// Initializationvar 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)? ? ? ? {? ? ? ? }? ? }}它無法識別很多代碼,并且也給了我很多計算錯誤:類、結構或接口成員聲明中的標記“+=”無效預期元組必須包含至少兩個元素 類、結構或接口成員聲明中的標記“+=”無效我聽說我應該將包“綁定”到我的項目,但我不知道該怎么做。感謝任何未來的幫助。
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

最簡單的是,您幾乎可以從 github 復制/粘貼 Wpf 實現,并更改為使用適用于 Windows 表單的正確事件處理程序/枚舉:


using CheetNET.Core;

using System;

using System.Text.RegularExpressions;

using System.Windows.Forms;


public class Cheet : Cheet<System.Windows.Forms.Keys>

{

    private static readonly Regex LetterKeysNamePattern = new Regex(@"^[a-z]$");

    private static readonly Regex NumberKeysNamePattern = new Regex(@"^[0-9]$");

    private static readonly Regex KeyspadNumberKeysNamePattern = new Regex(@"^kp_[0-9]$");

    private static readonly Regex FunctionKeysNamePattern = new Regex(@"^(?:f[1-9]|f1[0-2])$");


    private PreviewKeyDownEventArgs lastHandledEvent;


    public virtual void OnKeyDown(object sender, PreviewKeyDownEventArgs e)

    {

        if (e == lastHandledEvent)

        {

            return;

        }

        OnKeyDown(e.KeyCode);

        lastHandledEvent = e;

    }


    protected override Keys GetKey(string KeysName)

    {

        if (LetterKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey(KeysName.ToUpper());

        }

        if (NumberKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey("D" + KeysName);

        }

        if (KeyspadNumberKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey(KeysName.Replace("kp_", "NumPad"));

        }

        if (FunctionKeysNamePattern.IsMatch(KeysName))

        {

            return ParseKey(KeysName.ToUpper());

        }

        switch (KeysName)

        {

            case "left":

            case "L":

            case "←":

                return Keys.Left;

            case "up":

            case "U":

            case "↑":

                return Keys.Up;

            case "right":

            case "R":

            case "→":

                return Keys.Right;

            case "down":

            case "D":

            case "↓":

                return Keys.Down;

            case "backspace":

                return Keys.Back;

            case "tab":

                return Keys.Tab;

            case "enter":

                return Keys.Enter;

            case "return":

                return Keys.Return;

            case "shift":

            case "?":

                return Keys.LShiftKey;

            case "control":

            case "ctrl":

            case "^":

                return Keys.LControlKey;

            case "alt":

            case "option":

            case "?":

                return Keys.Alt;

            case "command":

            case "?":

                return Keys.LWin;

            case "pause":

                return Keys.Pause;

            case "capslock":

                return Keys.CapsLock;

            case "esc":

                return Keys.Escape;

            case "space":

                return Keys.Space;

            case "pageup":

                return Keys.PageUp;

            case "pagedown":

                return Keys.PageDown;

            case "end":

                return Keys.End;

            case "home":

                return Keys.Home;

            case "insert":

                return Keys.Insert;

            case "delete":

                return Keys.Delete;

            case "equal":

            case "=":

                return Keys.Oemplus;

            case "comma":

            case ",":

                return Keys.Oemcomma;

            case "minus":

            case "-":

                return Keys.OemMinus;

            case "period":

            case ".":

                return Keys.OemPeriod;

            case "kp_multiply":

                return Keys.Multiply;

            case "kp_plus":

                return Keys.Add;

            case "kp_minus":

                return Keys.Subtract;

            case "kp_decimal":

                return Keys.Decimal;

            case "kp_divide":

                return Keys.Divide;

        }

        throw new ArgumentException(String.Format("Could not map Keys named '{0}'.", KeysName));

    }


    private static Keys ParseKey(string KeysName)

    {

        return (Keys)Enum.Parse(typeof(Keys), KeysName);

    }

}

然后,只要將代碼放入表單加載處理程序中,它就會按預期工作:


protected override void OnLoad(EventArgs e)

{

    var cheet = new Cheet();

    PreviewKeyDown += cheet.OnKeyDown;

    cheet.Map("c h e a t", () => { MessageBox.Show("Voilà!"); });


    base.OnLoad(e);

}


查看完整回答
反對 回復 2023-08-20
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

更新(因為您已將錯誤輸出添加到問題中):

預期類、結構或接口成員聲明類型中的標記“+=”無效

你不能只將 C# 代碼放在類的頂層,你必須遵循一個最小的結構。您可以將此類代碼放置在類的構造函數方法或其他方法或靜態初始化塊中(如果您僅在代碼中引用靜態內容)

下次更新(新錯誤之后):

使用泛型類型“Cheet”需要 1 個類型參數

您正在使用通用包而不是 Wpf 包:

public?class?Cheet?:?Cheet<Key>,?ICheet

很不幸我認為他們使用相同的類名

查看完整回答
反對 回復 2023-08-20
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

存儲庫中有一個 WPF 演示應用程序,您可以在 Window 構造函數中進行初始化:


public MainWindow()

{

    var cheet = new Cheet();

    PreviewKeyDown += cheet.OnKeyDown;


    cheet.Map("↑ ↑ ↓ ↓ ← → ← → b a", () => { WriteLine("Voilà!"); });


    cheet.Map("i d d q d", () => {

        WriteLine("god mode enabled");

    });

    [etc.]

因此,您可以使用 處理PreviewKeyDown事件cheet.OnKeyDown,并且OnKeyDownCheet 中可能會循環遍歷其映射并尋找合適的映射。


我設置了一個測試 WinForms 項目并添加了 Cheet.NET,如果您想將它與 WinForms 一起使用,您似乎需要做一些工作。


Cheet.Core有一個Cheet<T>類,但它是抽象的??雌饋鞹是打算成為“鑰匙”類型的。Cheet.Wpf 庫有一個Cheet繼承自 的類Cheet<Key>,使用 WPFKey類型。


看來您需要創建自己的Cheet類(很可能)繼承自Cheet<System.Windows.Forms.Keys>.


我想現在的問題是哪個工作量更大:為 WinForms 實現 Cheet,還是在 WPF 中重新開始您的項目


查看完整回答
反對 回復 2023-08-20
  • 3 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號