How to get the keyboard layout on windows with ruby?

旧城冷巷雨未停 提交于 2020-01-17 05:59:11

问题


I want to get the keyboard layout name like "kbdus" for US-English keyboard or "kbdusx" for US-International. I have tried "GetKeyboardLayoutName" from Win32API, but I just got a number (0x20409). I know 0x0409 means "English" and "0x2" probably means one of the english keyboard variations. How to get the exact name of the keyboard layout of the user? I'm using Ruby 1.8.7 on Windows.


回答1:


I found a answer to my question:

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:


This discussion about Language Identifiers will probably get you going in the right direction. This listing of constants is probably even better.



来源:https://stackoverflow.com/questions/4857320/how-to-get-the-keyboard-layout-on-windows-with-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!