2011-01-31 65 views
0

我想獲得鍵盤佈局名稱,例如US-English鍵盤的「kbdus」或US-International的「kbdusx」。我已經嘗試從Win32API獲得「GetKeyboardLayoutName」,但我只是得到一個數字(0x20409)。我知道0x0409意味着「英文」和「0x2」可能意味着英文鍵盤變化之一。如何獲得用戶的鍵盤佈局的確切名稱?我在Windows上使用Ruby 1.8.7。如何使用紅寶石獲取windows上的鍵盤佈局?

回答

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"