• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Code

Always Begin...
  • 博客园
  • 管理

View Post

Python修改windows键盘映射

新电脑环境的处理之一是修改键盘映射,主Ctrl键使用CapsLock

CapsLock改为LeftCtrl

RightCtrl改为CapsLock

LeftCtrl改为RightCtrl

原来使用KeybMap这个软件修改,由于自己修改的映射很少,改为通过python操作注册表实现

import winreg

mapVal = b'\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x1d\x00\x3A\x00\x3A\x00\x1D\xE0\x1D\xE0\x1D\x00\x00\x00\x00\x00'

rootKey_Machine = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\CurrentControlSet\Control", 0, access=winreg.KEY_ALL_ACCESS)
rootkey_Machine_Keyboard = winreg.CreateKey(rootKey_Machine, "Keyboard Layout")

#rootKey_User = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"System\CurrentControlSet\Control", 0, access=winreg.KEY_ALL_ACCESS)
#rootkey_User_Keyboard =  winreg.CreateKey(rootKey_User, "Keyboard Layout")
winreg.SetValueEx(rootkey_Machine_Keyboard,"Scancode Map", 0, winreg.REG_BINARY, mapVal)
print("Done") 

mapVal(二进制值定义)解释:

b'\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x1d\x00\x3A\x00\x3A\x00\x1D\xE0\x1D\xE0\x1D\x00\x00\x00\x00\x00'

红色部分:0-4:Header: Version Information与4-8:Header: Flags,都设置为0

绿色部分:Header: Number of Mappings,标记映射数量。比如这里做3个映射,总数就是03

蓝色部分:映射主体定义。AB写法,即当按下B键时,执行A动作,例如把CapsLock(3A)映射为LeftCtrl(1D),则是:1D 00 3A 00

  注:LeftCtrl的键扫描码为1D,值表示为1D 00,RightCtrl的键扫描码为E0 1D,值表示为1D E0

灰色部分:收尾,Null Terminator (0x00000000)

关于注册表中键盘映射的定义,参考其他园丁的文章,或者:

https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/keyboard-and-mouse-class-drivers#scan-code-mapper-for-keyboards

问题:键盘映射写入到currentuser注册表下,不生效(之前使用KeybMap软件,应用当前用户也不生效)

 

附:

windows 键盘映射表 scancode.doc (live.com)

posted on 2022-08-17 10:19  老十  阅读(621)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3