简体   繁体   中英

InputLanguage doesn't work properly

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM