1

In my application i am using SendKeys.SendWait to send text to screen:

SendKeys.SendWait("password");

The text is on English but when the keyboard set to other language the text that SendKeys.SendWait type is set in other language and not in English

Any suggestions how to make sure that the text will set only in English ?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
falukky
  • 1,099
  • 2
  • 14
  • 34
  • 1
    Are you sure `SendKeys` is the right approach here instead of using the more *semantically* oriented UI Automation features of the OS? I also have *no idea* of why keyboard language matters when you're sending a stream of plain text... – Damien_The_Unbeliever Mar 19 '19 at 18:41
  • SendKeys sends **keystrokes**, not text. If the keyboard locale is set to something other than English, then those characters will be sent - as [documented](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=netframework-4.7.2) - _"Use SendKeys to send keystrokes and keystroke combinations to the active application...."_ – stuartd Mar 19 '19 at 18:41
  • So any other way ? – falukky Mar 19 '19 at 18:41

1 Answers1

0

I did a quick test using SendKeys.Send to send text to a couple of input fields. It sends the same text regardless of whether I have the keyboard in English or another language, so I'm not sure why you see a different result. Example:

SendKeys.Send("username");
SendKeys.Send("{TAB}");
SendKeys.Send("påsswørd");
SendKeys.SendWait("{ENTER}");

One possibility is that you could change the keyboard to English temporarily before calling SendKeys, then set it back to whatever it was before. There is an excellent example of the technique in this answer.

Another option is to use Win32 API functions to send messages to the window. The problem will be how to find the right windows to send the text to though. I'm not sure it could be done reliably. Here's an example (not tested):

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, string lParam);

// Windows message constants
const int WM_SETTEXT = 0x000C;

public void DoLogin(string username, string password)
{
    // Get handle for current active window
    IntPtr hWndMain = GetForegroundWindow();

    if (!hWndMain.Equals(IntPtr.Zero))
    {
        IntPtr hWnd;

        // Here you would need to find the username text input window
        if ((hWnd = FindWindowEx(hWndMain, IntPtr.Zero, "UserName", "")) != IntPtr.Zero)
            // Send the username text to the active window
            SendMessage(hWnd, WM_SETTEXT, 0, username);

        // Here you would need to find the password text input window
        if ((hWnd = FindWindowEx(hWndMain, IntPtr.Zero, "Password", "")) != IntPtr.Zero)
            // Send the password text
            SendMessage(hWnd, WM_SETTEXT, 0, password);

        // Send ENTER key to invoke login
        SendKeys.SendWait("{ENTER}");
    }
}
Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
  • I am using this SendKeys to send to the focus field user, tab, password and enter (my credentials) so i can use your way only with user and password ? – falukky Mar 20 '19 at 04:53
  • @falukky Added some information about keystroke options. – Chris Olsen Mar 20 '19 at 15:33
  • What is this WM_SETTEXT ? i want to send all the text to the focus window – falukky Mar 20 '19 at 17:36
  • That is strange, i cannot see the text that your code type, for example what i am test it on notepad windows the text is type on the window title and not inside the file, i am want to use it on websites when i need to enter my credentials – falukky Mar 21 '19 at 07:07
  • @falukky I did a quick test and you're right, it was setting the text of the main window. You would need to somehow find the actual text input window and send WM_SETTEXT to them. Not sure it's feasible. I also did a quick test of `SendKeys` and couldn't replicate the problem you describe. I've edited this answer again now with the new information. – Chris Olsen Mar 21 '19 at 14:22
  • This 2 SendMessage inside the if statements never executed – falukky Mar 21 '19 at 14:57
  • Right, that's just an example of how to call the functions. For it to actually work, you would need to know in advance the class name or title of the user/password input windows. Not sure that method is appropriate if you want it to work with different web sites though. – Chris Olsen Mar 21 '19 at 15:04