locked
sendkeys types wrong character sometimes RRS feed

  • Question

  • On occasion sendkeys will type the wrong character. What other methods could I use to type keys to another process? I need to also send function keys, key modifiers (alt,shift,ctrl) and also support unicode. I was thinking about using pinvoke of sendinput but I have sendkeys configured to use sendinput so I'm not sure what if anything would be different if I went that route. Other options?

    sent: BLAH1a
    typed: BLAH!a


    sent: +^%][~)(
    typed: +^%][~0(

    Thread.Sleep(250);
    SendKeys.SendWait(temp);
    SendKeys.Flush();

    https://stackoverflow.com/questions/52667332/sendkeys-types-wrong-character-sometimes


    Friday, October 12, 2018 7:43 PM

All replies

  • Hello wallst23,

    1. It is surprising that when you sent the "BLAH1a" string, it gets translated into "BLAH!a" instead. This should not have happened.

    2. However, it does imply that on your keyboard, the key that represents the '1' character is also used for the '!' character.

    3. Try the following string instead : "BLAH+1a".

    - The '+' will send the '1' character with the SHIFT key pressed.

    4. See SendKeys Class for more information on how to send reserved characters.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    Saturday, October 13, 2018 6:02 AM
  • Why would I want to press the shift key, I don't want the '!' I want '1'. Seems more like a bug with the sendkeys API that is does not consistently type what I tell it to. This all takes place on a VM where there is no user interaction at all, so interference is not an issue here.  I don't know why it would be activating the shift key in certain circumstances when I dont instruct it to

    • Edited by wallst23 Monday, October 15, 2018 1:37 PM
    Monday, October 15, 2018 12:31 PM
  • Hi wallst23,

    There is a stackoverflow link on your post, which provide a answer by using user32.dll. And here is the document about all of virtual-key-codes for your reference.

    https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 16, 2018 5:38 AM
  • Stumbled across this post googling solution to the same issue 4 years later. Nothing changed and SendInput still works unpredictable. I found easy solution in other place but returned back here to leave comment because it might help somebody. Just get rid of this SendInput and use InputSimulator nuget package (https://www.nuget.org/packages/InputSimulator). It works like a charm. No problems at all.
    Friday, September 6, 2019 3:52 PM
  • I did wind up using the inputsimulator lib, one thing you should be aware of is the different methods of keypress vs TextEntry. They don't exactly send the same commands, even though both use sendinput under the covers just with different arguments sometimes which results in not getting the right keys pressed sometimes.
    Friday, September 6, 2019 3:58 PM