简体   繁体   中英

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 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.

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