1

I would like to automatically change the keyboard layout and I create a simple console application in Visual Basic adding the following:

InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(New CultureInfo("ru"))

But when I compile this code it doesn't change the keyboard layout, so it remains what it was before compiling. What am I doing wrong?

MGMKLML
  • 39
  • 6

1 Answers1

0

There three ways to change keyboard language:

  1. Using property .CurrentInputLanguage (only if input language installed)

    InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(New CultureInfo("ru-RU"))


  1. Using property .CurrentCulture (only if input language installed)

    Dim culture = System.Globalization.CultureInfo.GetCultureInfo("ru-RU")
    Dim lang = InputLanguage.FromCulture(culture)
    
    If InputLanguage.InstalledInputLanguages.IndexOf(lang) >= 0 Then
        InputLanguage.CurrentInputLanguage = InputLanguage.InstalledInputLanguages(InputLanguage.InstalledInputLanguages.IndexOf(lang))
        System.Threading.Thread.CurrentThread.CurrentCulture = culture
    End If
    

  1. Using winapi function .LoadKeyboardLayout (slowly, works even if input language not installed)

    <DllImport("user32.dll")>
    Private Shared Function LoadKeyboardLayout(ByVal pwszKLID As String, ByVal Flags As UInteger) As IntPtr
    End Function
    
    LoadKeyboardLayout("00000419", 1)
    

Additional

For check current culture:

InputLanguage.CurrentInputLanguage.Culture.Name

Check is input language installed:

InputLanguage.InstalledInputLanguages.IndexOf(InputLanguage.FromCulture(New CultureInfo("ru-RU"))

Switch to next locale identifier (keyboard layout):

Declare Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As Integer, ByVal flags As Integer) As Integer 

'switch keyboard layout to next
Sub SwitchKeyboardLayout()
    Dim HKL_NEXT As Integer = 1
    Dim dl As Integer = ActivateKeyboardLayout(HKL_NEXT, 0)
    If dl = 0 Then MsgBox("Unsuccessful!")
End Sub

Additional materials

abberdeen
  • 323
  • 7
  • 32
  • I used "ru-RU" but it didn't help. The general problem is that you seem to have to be attached to the specific application as there are no 'general' keyboard layout in windows. – MGMKLML Jul 10 '18 at 08:21
  • Check this: MsgBox(InputLanguage.CurrentInputLanguage.Culture.EnglishName); before and after: InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(New CultureInfo("ru-RU")) – abberdeen Jul 10 '18 at 09:15
  • Is lang "ru" exists on user lang list (language panel) – abberdeen Jul 10 '18 at 09:44
  • 1
    Technically it changes the culture but the keyboard layout itself isn't changed. Yes, 'ru' keyboard does exist in the list – MGMKLML Jul 10 '18 at 09:53
  • Please check above^ – abberdeen Jul 10 '18 at 12:07
  • Thank you a lot, I'll take a look at this a bit later – MGMKLML Jul 10 '18 at 12:51