如何使用ruby在Windows上获取键盘布局? [英] How to get the keyboard layout on windows with ruby?

查看:81
本文介绍了如何使用ruby在Windows上获取键盘布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取键盘布局名称,例如对于美式英语键盘为"kbdus",对于美式国际键盘则为"kbdusx".我已经尝试过从Win32API获得"GetKeyboardLayoutName",但是我刚得到一个数字(0x20409).我知道0x0409表示英语","0x2"可能表示英语键盘变体之一.如何获取用户键盘布局的确切名称?我在Windows上使用Ruby 1.8.7.

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"

这篇关于如何使用ruby在Windows上获取键盘布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆