Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# SendKeys.Send

Tags:

c#

sendkeys

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?

like image 371
milot Avatar asked Jun 18 '09 13:06

milot


2 Answers

I would reverse my calls:

SendKeys.Send("{BS}");

SendKeys.Send("S");

EDIT (After Question Updated):

If you're working with the string (for your special characters), can you not just capture the string generated by the key press ("a") and modify it by setting the string to the unicode value of the character you're attempting to represent? If the other solutions people have been mentioning aren't working, that's where I'd try next...

like image 59
AllenG Avatar answered Oct 02 '22 18:10

AllenG


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.

like image 42
Erich Mirabal Avatar answered Oct 02 '22 18:10

Erich Mirabal