【问题标题】:How to get the keyboard layout on windows with ruby?如何使用 ruby​​ 在 Windows 上获取键盘布局?
【发布时间】:2011-02-02 00:11:34
【问题描述】:

我想获得键盘布局名称,例如美国英语键盘的“kbdus”或美国国际键盘的“kbdusx”。我尝试过 Win32API 中的“GetKeyboardLayoutName”,但我只得到了一个数字(0x20409)。我知道 0x0409 表示“英语”,而“0x2”可能表示英语键盘变体之一。如何获取用户键盘布局的确切名称?我在 Windows 上使用 Ruby 1.8.7。

【问题讨论】:

    标签: ruby winapi keyboard


    【解决方案1】:

    我找到了我的问题的答案:

    require 'win32/registry'
    require 'win32API'
    
    Win32API.new('user32', 'GetKeyboardLayoutName', 'p', '').call(layoutid = "\0"*8)
    
    reg_path = 'SYSTEM\CurrentControlSet\Control\Keyboard Layouts\\' << layoutid
    reg = Win32::Registry::HKEY_LOCAL_MACHINE.open(reg_path)
    
    layoutcode = reg.read('Layout File')[1].split('.', 2)[0]
    layoutname = reg.read('Layout Text')[1]
    
    layoutcode # => "KDBUSX"
    layoutname # => "United States-International"
    

    【讨论】:

      【解决方案2】:

      关于Language Identifiers 的讨论可能会让你朝着正确的方向前进。 This listing of constants 可能更好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-19
        • 2013-09-12
        • 2020-08-02
        • 2019-12-09
        • 2016-05-24
        • 1970-01-01
        • 2019-05-28
        相关资源
        最近更新 更多