简体   繁体   English

C# 如何使 ProcessCmdKey 作为公共方法

[英]C# how to make ProcessCmdKey as public method

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.NumPad0:
                TheAddingResult(0);
                return true;

            default:
                return base.ProcessCmdKey(ref msg, keyData);
        }
    }

i'm making a calculator and i wanna assign every button to certain hotkey but i can't do it i have to use this way which is not that much useful is there is a way that i can use this method like if it's any public method ?我正在制作一个计算器,我想将每个按钮分配给某个热键,但我不能这样做方法 ? like in button_click event keydata = keys.numpad0 or type for example processcmdkey(keys.numpad0) is that possible ?就像在 button_click 事件中 keydata = keys.numpad0 或输入例如 processcmdkey(keys.numpad0) 可能吗?

edit : someone wrote this code and then deleted it i liked it but i just want to know what to type in the first argument when summoning it编辑:有人写了这段代码,然后删除了它,我喜欢它,但我只想知道在调用它时在第一个参数中输入什么

     public bool hotkeys(ref Message msg, Keys keyData)
    {
        return ProcessCmdKey(ref msg, keyData);
    }
     private void button17_Click(object sender, EventArgs e)
    {
        hotkeys(ref  ,Keys.Back);
        erasingall();
    }

what should i type in the first argument when summoning ?召唤时我应该在第一个参数中输入什么?

If you click directly in your button use ClickEvent and inside the subscriber execute your method with the appropriate parameters.如果您直接单击按钮,请使用 ClickEvent 并在订阅者内部使用适当的参数执行您的方法。 Probably you will have to inject as dependency the class holding he method, or make the method static static, or create it directly in the partial class.可能您必须将持有该方法的类作为依赖项注入,或者将方法设为静态,或者直接在分部类中创建它。 For Example, something like:例如,类似于:

 private void ButtonPlusClick(object sender, EventArgs e)
    {
        this.operationsContainer.Execute(Constants.PlusSign);
    }

where operationsContainer is the class that holds the method for operations.其中 operationsContainer 是保存操作方法的类。

If you want to enter input also directly from your keyboard, you can make KeyPreview True from the properties of your form and then use event for keypress like FormDetectKeyPress.如果您还想直接从键盘输入输入,您可以根据表单的属性使 KeyPreview 为 True,然后使用事件进行按键,如 FormDetectKeyPress。 For Example:例如:

private void FormDetectKeyPress(object sender, KeyPressEventArgs e)
    {
        var pressedKeyValue = e.KeyChar;
        switch (pressedKeyValue)
        {
            case '+':
                {
                    this.ButtonPlusClick(sender, null);
                }

                break;
           default:
                return;

          }

You can add any operation with button click event in similar way and corresponding button from keyboard in the FormDetectKeyPress subscriber.您可以以类似的方式添加带有按钮单击事件的任何操作,以及 FormDetectKeyPress 订阅者中来自键盘的相应按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM