tags:

views:

3905

answers:

9

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 be "s" for example, when I am doing this in my code:

if ((Keys)keyCode== Keys.A)
{                    
    SendKeys.Send("s");                    

}

Right now I get only "sa" character printed in my notepad, but instead of printing "sa" I need to get only "s" character in this case because when I press "a" on my keyboard, "a" must be replaced with "s".

I tried removing the last character by adding this line:

SendKeys.Send("{BS}");

But all I got is "s" character removed and "a" character was there.

How can I prevent this from happening?

+1  A: 

I get "a" printed because backspace is sent immediately after "s" and it deletes the "s" character. How can I prevent this from happening?

uhm.....

don't send backspace immediately after sending s?

if ((Keys)keyCode== Keys.A)
            {
                Sendkeys.Send("{BS}"); // Deletes the "A" (already sent)
                SendKeys.Send("s"); // Sends the "S"
            }
tom.dietrich
but I get "sa" characters!
milot
A: 

Have you tried switching .Send({BS}) with .Send("s")? Otherwise, can you clarify?

Andrew Austin
I edited the post, you can re-read it.
milot
+2  A: 
AllenG
A: 

SendKeys.SendWait

It doesn't help, I tried :)
milot
+2  A: 

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
        }
rally25rs
Just a note: I posted this before the original question was changed to state that he is working with notepad.
rally25rs
I need this to be global, I am using Windows Hooks to get the global keys that are pressed.
milot
by switching key sends doesn't work.
milot
A: 

This may not be what you want/need, but if you just want to remap some keyboard keys, I would suggest looking into using another keyboard layout, or creating a custom layout. You can create new layouts with this:

http://msdn.microsoft.com/en-ca/goglobal/bb964665.aspx

Sean Nyman
Thank you very much for your suggestion, but I need to remap the keyboard layout programmaticaly.
milot
+1  A: 

Another option you have is to use Low-Level keyboard hooks to trap the old character and then send the new one. It will require some P/Invoke, but it gives you the ability to completely trap the original key so apps don't even see it.

Erich Mirabal
+1  A: 

I think that the SendKeys.Send("{BS}"); approach will never work (in either order). That's because the original key-pressed/key-released event gets processed after SendKeys.Send is posted. I think you should somehow cancel the key-down/key-up event for the character you want to remove before it is processed by the target window procedure (e.g. notepad's).

Utaal
+1  A: 

I solved this problem by registering global hot keys. Thank you. This was the resourse that I used to solve my problem.

milot