键盘的自定义和替代(特别献给使用IBM笔记本的同学)
用过IBM早期笔记本(非Lenovo)的同学应该知道IBM的笔记本键盘是没有Win键的,虽然IBM的键盘用起来很舒服,但是没有Win键的确很不爽!用起来总是不那么顺手(我以前猜想Lenovo收购IBM PC后,第一件事就是给IBM键盘加上Win键,果然是新的笔记本键盘都有Win键了!IBM作为一个大公司,有点官僚了)。
我很少使用键盘右边的Alt和Ctrl键,我想,这两个用来代替Win键和App键多好啊?没有想到IBM还真的有这种程序:Keyboard Custom Shortcut,不过可惜的是由两个缺点:一个是这个程序是应用程序级别的,模拟Win键和App键不彻底,所以有BUG:按Win + L后,Win键会被当作一直按下去,所以在重新登陆后,键盘会疯掉,另外没有办法支持远程桌面!第二个缺点是要占用内存资源,对于一些优化狂来说,不可忍受的,如果机器内存只有256M,那么就太可怕了。
Google是大家的好帮手。:-D, 最终找到了一个完美的解决方法:零资源零内存占用的驱动级的remap方法。其原理是在键盘硬件驱动中,把键盘的扫描码代替掉,所以对应用程序来说完全透明,并且不占用任何资源,没有冲突,缺点是原来被替换掉的键就无法使用了。
键盘的输入处理是这样的:键盘上面有一个小的芯片,负责处理按键,某个键按下,产生一个代码,这个代码称之为Scan Code,这个是物理上的,然后产生中断,送入OS,OS收到中断后,处理Scan Code,转换成Virtual Code,也就是大家编码经常用到的VK_xxxx之类的代码了,到这个时候已经是应用程序级别了,在这个转换过程中,我们就可以偷梁换柱,直接替换掉Scan Code,那么效果就和直接按下了Scan Code代表的键一样,就可以做到真正的物理级的替代了!
幸运的是,Windows支持替换Scan Code,Windows默认的键盘驱动允许用户自定义Scan Code的替代和转换,我们只要修改注册表
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout,在下面新建Scancode Map(二进制),例如下面的代码定义IBM笔记本的Back 和 Forward两个键:
其中第一行的8個00是固定的header
第二行的03 00 00 00代表後面有三個mapping﹕
5b e0 6a e0,5b e0是windows鍵的scancode,6a e0是backward
5d e0 69 e0,5d e0是application, 69 e0是forward
00 00 00 00 (這是一個null terminator mapping)
修改后必须重新启动机器!
如果用上面的方法,太笨了,光那个Scan Code和Virutal Code转换之类就要搞死人了,因此我们可以使用Windows的Resource Kits中的一个工具
remapkey来实现,这个工具非常好用,就是以图形化的键盘来允许你自定义上面ScanCode Map数据!
補充一些常用代碼好了
Code -- Key
38,00 Left {Alt}
38,E0 Right {Alt}
1D,00 Left {Ctrl}
1D,E0 Right {Ctrl}
2A,00 Left {Shift}
46,00 Right {Shift}
3A,00 {Caps Lock}
5B,E0 Left {Windows}
5C,E0 Right {Windows}
3B,00 {F1}
3C,00 {F2}
3D,00 {F3}
3E,00 {F4}
3F,00 {F5}
40,00 {F6}
41,00 {F7}
42,00 {F8}
43,00 {F9}
44,00 {F10}
57,00 {F11}
58,00 {F12}
如果要自定义微软键盘上的一些按键的动作,可以修改注册表:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AppKey
在下面建立一些数字的对应的键,然后创建名字为ShellExecute的字符串,值为对应的命令行即可
例如Back对应的数字为1:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AppKey\<NUM>]
"ShellExecute"="C:\\"
其中的<NUM>为一些数字,对应的含义如下:
1: Browse back
2: Browse Forward
7: Browser
15: Mail
16: Media Player
17: My Computer
18: Caculator
更多关于这些的信息,请参考:
http://www.microsoft.com/taiwan/whdc/device/input/w2kbd.mspx
下面给出一些Scan Code的含义:
App: Calculator (E0_21)
App: E-Mail (E0_6C)
App: Media Select (E0_6D)
App: Messenger (E0_11)
App: My Computer (E0_6B)
Arrow: Down (E0_50)
Arrow: Left (E0_4B)
Arrow: Right (E0_4D)
Arrow: Up (E0_48)
F-Lock: Close (E0_40)
F-Lock: Fwd (E0_42)
FF-Lock: New (E0_3E)
F-Lock: Office Home (E0_3C)
F-Lock: Open (E0_3F)
F-Lock: Print (E0_58)
F-Lock: Redo (E0_07)
F-Lock: Reply (E0_41)
F-Lock: Save (E0_57)
F-Lock: Send (E0_43)
F-Lock: Spell (E0_23)
F-Lock: Task Pane (E0_3D)
F-Lock: Undo (E0_08)
Function: F1 (00_3B)
Function: F10 (00_44)
Function: F11 (00_57)
Function: F12 (00_58)
Function: F13 (00_64)
Function: F14 (00_65)
Function: F15 (00_66)
Function: F2 (00_3C)
Function: F3 (00_3D)
Function: F4 (00_3E)
Function: F5 (00_3F)
Function: F6 (00_40)
Function: F7 (00_41)
Function: F8 (00_42)
Function: F9 (00_43)
Key: ' " (00_28)
Key: - _ (00_0C)
Key: , < (00_33)
Key: . > (00_34)
Key: / ? (00_35)
Key: ; : (00_27)
Key: [ { (00_1A)
Key: \ | (00_2B)
Key: ] } (00_1B)
Key: ` ~ (00_29)
Key: = + (00_0D)
Key: 0 ) (00_0B)
Key: 1 ! (00_02)
Key: 2 @ (00_03)
Key: 3 # (00_04)
Key: 4 $ (00_05)
Key: 5 % (00_06)
Key: 6 ^ (00_07)
Key: 7 & (00_08)
Key: 8 * (00_09)
Key: 9 ( (00_0A)
Key: A (00_1E)
Key: B (00_30)
Key: C (00_2E)
Key: D (00_20)
Key: E (00_12)
Key: F (00_21)
Key: G (00_22)
Key: H (00_23)
Key: I (00_17)
Key: J (00_24)
Key: K (00_25)
Key: L (00_26)
Key: M (00_32)
Key: N (00_31)
Key: O (00_18)
Key: P (00_19)
Key: Q (00_10)
Key: R (00_13)
Key: S (00_1F)
Key: T (00_14)
Key: U (00_16)
Key: V (00_2F)
Key: W (00_11)
Key: X (00_2D)
Key: Y (00_15)
Key: Z (00_2C)
Logitech: iTouch (E0_13)
Logitech: Shopping (E0_14)
Logitech: Webcam (E0_12)
Media: Mute (E0_20)
Media: Next Track (E0_19)
Media: Play/Pause (E0_22)
Media: Prev Track (E0_10)
Media: Stop (E0_24)
Media: Volume Down (E0_2E)
Media: Volume Up (E0_30)
Num: - (00_4A)
Num: * (00_37)
Num: . (00_53)
Num: / (E0_35)
Num: + (00_4E)
Num: 0 (00_52)
Num: 1 (00_4F)
Num: 2 (00_50)
Num: 3 (00_51)
Num: 4 (00_4B)
Num: 5 (00_4C)
Num: 6 (00_4D)
Num: 7 (00_47)
Num: 8 (00_48)
Num: 9 (00_49)
Num: Enter (E0_1C)
Special: ¥ - (00_7D)
Special:
€ (E0_45)
Special: Application (E0_5D)
Special: Backspace (00_0E)
Special: Caps Lock (00_3A)
Special: Delete (E0_53)
Special: End (E0_4F)
Special: Enter (00_1C)
Special: Escape (00_01)
Special: Home (E0_47)
Special: Insert (E0_52)
Special: Left Alt (00_38)
Special: Left Ctrl (00_1D)
Special: Left Shift (00_2A)
Special: Left Windows (E0_5B)
Special: Num Lock (00_45)
Special: Page Down (E0_51)
Special: Page Up (E0_49)
Special: Power (E0_5E)
Special: PrtSc (E0_37)
Special: Right Alt (E0_38)
Special: Right Ctrl (E0_1D)
Special: Right Shift (00_36)
Special: Right Windows (E0_5C)
Special: Scroll Lock (00_46)
Special: Sleep (E0_5F)
Special: Space (00_39)
Special: Tab (00_0F)
Special: Wake (or Fn) (E0_63)
Unknown: 0x0054 (00_54)
Unknown: 0x0055 (00_55)
Unknown: 0x0056 (00_56)
Unknown: 0x0059 (00_59)
Unknown: 0x005A (00_5A)
Unknown: 0x005B (00_5B)
Unknown: 0x005C (00_5C)
Unknown: 0x005D (00_5D)
Unknown: 0x005E (00_5E)
Unknown: 0x005F (00_5F)
Unknown: 0x0060 (00_60)
Unknown: 0x0061 (00_61)
Unknown: 0x0062 (00_62)
Unknown: 0x0063 (00_63)
Unknown: 0x0067 (00_67)
Unknown: 0x0068 (00_68)
Unknown: 0x0069 (00_69)
Unknown: 0x006A (00_6A)
Unknown: 0x006B (00_6B)
Unknown: 0x006C (00_6C)
Unknown: 0x006D (00_6D)
Unknown: 0x006E (00_6E)
Unknown: 0x006F (00_6F)
Unknown: 0x0070 (00_70)
Unknown: 0x0071 (00_71)
Unknown: 0x0072 (00_72)
Unknown: 0x0073 (00_73)
Unknown: 0x0074 (00_74)
Unknown: 0x0075 (00_75)
Unknown: 0x0076 (00_76)
Unknown: 0x0077 (00_77)
Unknown: 0x0078 (00_78)
Unknown: 0x0079 (00_79)
Unknown: 0x007A (00_7A)
Unknown: 0x007B (00_7B)
Unknown: 0x007C (00_7C)
Unknown: 0x007E (00_7E)
Unknown: 0x007F (00_7F)
Unknown: 0xE001 (E0_01)
Unknown: 0xE002 (E0_02)
Unknown: 0xE003 (E0_03)
Unknown: 0xE004 (E0_04)
Unknown: 0xE005 (E0_05)
Unknown: 0xE006 (E0_06)
Unknown: 0xE009 (E0_09)
Unknown: 0xE00A (E0_0A)
Unknown: 0xE00B (E0_0B)
Unknown: 0xE00C (E0_0C)
Unknown: 0xE00D (E0_0D)
Unknown: 0xE00E (E0_0E)
Unknown: 0xE00F (E0_0F)
Unknown: 0xE015 (E0_15)
Unknown: 0xE016 (E0_16)
Unknown: 0xE017 (E0_17)
Unknown: 0xE018 (E0_18)
Unknown: 0xE01A (E0_1A)
Unknown: 0xE01B (E0_1B)
Unknown: 0xE01E (E0_1E)
Unknown: 0xE01F (E0_1F)
Unknown: 0xE025 (E0_25)
Unknown: 0xE026 (E0_26)
Unknown: 0xE027 (E0_27)
Unknown: 0xE028 (E0_28)
Unknown: 0xE029 (E0_29)
Unknown: 0xE02B (E0_2B)
Unknown: 0xE02C (E0_2C)
Unknown: 0xE02D (E0_2D)
Unknown: 0xE02F (E0_2F)
Unknown: 0xE031 (E0_31)
Unknown: 0xE033 (E0_33)
Unknown: 0xE034 (E0_34)
Unknown: 0xE036 (E0_36)
Unknown: 0xE039 (E0_39)
Unknown: 0xE03A (E0_3A)
Unknown: 0xE044 (E0_44)
Unknown: 0xE046 (E0_46)
Unknown: 0xE04A (E0_4A)
Unknown: 0xE04C (E0_4C)
Unknown: 0xE04E (E0_4E)
Unknown: 0xE054 (E0_54)
Unknown: 0xE055 (E0_55)
Unknown: 0xE056 (E0_56)
Unknown: 0xE059 (E0_59)
Unknown: 0xE05A (E0_5A)
Unknown: 0xE061 (E0_61)
Unknown: 0xE062 (E0_62)
Unknown: 0xE064 (E0_64)
Unknown: 0xE06E (E0_6E)
Unknown: 0xE06F (E0_6F)
Web: Back (E0_6A)
Web: Favorites (E0_66)
Web: Forward (E0_69)
Web: Home (E0_32)
Web: Refresh (E0_67)
Web: Search (E0_65)
Web: Stop (E0_68)