C# SendKeys.Send

前端 未结 9 1403
闹比i
闹比i 2021-01-13 01:17

I am running on an issue using C# SendKeys.Send method. I am trying to replace keyboard keys with other keys, for example when I press \"a\" in keyboard I want that key to b

9条回答
  •  忘掉有多难
    2021-01-13 01:37

    I might be making a bad assumption, but if you are using this with a text box, you could:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 'a')
        {
            e.Handled = true;
        }
    }
    
    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A)
        {
            e.Handled = true;
            SendKeys.Send("s");
        }
    }
    

    Or even simpler:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 'a')
        {
            e.Handled = true;
            SendKeys.Send("s");
        }
    }
    

    Or if this isn't for use just with a text box, then can't you just revers your backspace and s key sends?

    if ((Keys)keyCode== Keys.A)
    {                    
        SendKeys.Send("{BS}"); // remove A
        SendKeys.Send("s");    // add S
    }
    

提交回复
热议问题