开发者

How to get the keyboard layout on windows with ruby?

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 开发者_C百科keyboard layout of the user? I'm using Ruby 1.8.7 on Windows.


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"


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜