InputLanguage无法正常工作

时间:2018-07-09 07:22:44

标签: vb.net visual-studio input keyboard input-language

我想自动更改键盘布局,并在Visual Basic中创建一个简单的控制台应用程序,添加以下内容:

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

但是当我编译这段代码时,它不会更改键盘布局,因此它保持了编译前的状态。我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

有三种更改键盘语言的方法:

  1. 使用属性.CurrentInputLanguage (仅在安装了输入语言的情况下)

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


  1. 使用属性.CurrentCulture (仅在安装了输入语言的情况下)

    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. 使用winapi函数.LoadKeyboardLayout (缓慢,即使未安装输入语言也可以使用)

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

其他

查看当前的文化:

InputLanguage.CurrentInputLanguage.Culture.Name

检查是否安装了输入语言:

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

切换到下一个语言环境标识符(键盘布局):

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

其他材料