简体   繁体   English

在 C# 中更改密钥

[英]Change key in c#

I need to change keys in c#.我需要更改 C# 中的密钥。

Example : if press on "a" then "b" pressing.例如:如果按“a”然后按“b”。

Something like this:像这样的东西:

Key.change("a","b"); // a = key , b = value

Please note this : key is static and value is dynamic, it's meaning : key = (always) "a" , value = (can be change) "b" ||请注意:key 是静态的,value 是动态的,意思是:key = (always) "a" , value = (can be change) "b" || "c" || "c" || "d",etc “d”等

So if this problem solving with simply way i really glad.所以如果这个问题以简单的方式解决,我真的很高兴。

Thanks and sorry for poor english.感谢和抱歉糟糕的英语。

On the Form class, add this method & variable :在 Form 类上,添加此方法和变量:

char change = 'b';
private void ChangeKey(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar=='a')
    {
            e.KeyChar = change;
    }
}

Then either on Load or Form constructor, subscribe to each of input control you have, manually然后在 Load 或 Form 构造函数上,手动订阅您拥有的每个输入控件

textBox1.KeyPress += ChangeKey;
textBox2.KeyPress += ChangeKey;

or all of them或全部

 foreach (var item in Controls.OfType<Control>())
 {
    item.KeyPress += ChangeKey;
 }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar > (char)001 )
        {
            e.KeyChar++;

        }
    }

also works.也有效。

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

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