在应用程序上重新映射键

Remapping Keys on Application

在我的程序中,我希望能够在我的键盘上输入 "E" 并将其作为不同的字母输出到文本框,例如"F".

在发送密钥时不发生冲突的最有效方法是什么?

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        switch(e.KeyCode)
        {
            case Keys.E:
                e.SuppressKeyPress = true;
                SendKeys.Send("F".ToLowerInvariant());
                break; 
            case Keys.F:
                e.SuppressKeyPress = true;
                SendKeys.Send("E".ToLowerInvariant());
                break;            
        }
    }

我尝试使用上面的方法,但它最终会发生冲突,最终会发送不同的信件。

您应该为此使用 KeyPress 事件,而不是 KeyDown/KeyUp 事件。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    switch (e.KeyChar.ToString().ToUpper())
    {
        case "E":
            e.KeyChar = 'f';
            break;
        case "F":
            e.KeyChar = 'e';
            break;
    }
}