【物理白痴进化论,欢迎吐槽】键盘按键原理及应用

来源:互联网 发布:杭州seo外包服务 编辑:程序博客网 时间:2024/05/17 10:53

参考:

【good!】http://blog.csdn.net/hgf1011/article/details/4242076

【good】 i8042 键盘控制器-------详细介绍-      http://blog.chinaunix.net/uid-25099259-id-3409632.html

【good】 http://blog.csdn.net/yes_life/article/details/6840660

【good】 键盘的扫描码Scan Code,通码Make code,断码Break Code http://blog.chinaunix.net/uid-24709751-id-3515788.html

【good,wiki】 https://en.wikipedia.org/wiki/Scancode

【要看下来,wiki】http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html#ss1.1

【wiki强大】http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html#toc10

http://mouse.it168.com/a2010/0715/1078/000001078067_2.shtml

http://www.bowenwang.com.cn/keyboard5.htm

通过对i8042 键盘控制器编程控制鼠标       http://blog.csdn.net/snailhit/article/details/6684192

电脑键盘工作原理  http://www.elecfans.com/dianzichangshi/200801317611.html

没想到里面这么有学问惊讶


整个经过:
键盘是一种矩阵结构,每一个键都有一个行地址和列地址,

SP/2:
1.用户按下键以后,EC获得该按键的matrix address,
2.EC将该address转化为matrix value然后判断该键的类型是特殊功能键还是标准按键,然后采用不同的方法将matrix value转成Set2,
(CapsLock,Fn:特殊按键,触发单独的IRQ
Fn没有scan code但是它有matrix address 所以EC收到该键按下后置一个flag,后续检测到F1-F12 被按下后,EC发一个Q_EVENT(什么是Q_EVENT?后续会详细描述J)给Host,Host就可以和EC通信了。如此便可以定制出各种各样的功能了。)

3.再转成Set1 value

4.送给host,
5.host收到后转成ascii就可以送给其它需要的程序了。
    OS查看是否是系统命令?->是:执行
                                                  不是:应用程序是否接受键盘数据?->不:舍弃
                                                                                                                  是:确定是否是命令?->是:执行
                                                                                                                                                       ->不是:当做内容接收


USB键盘经过:

USB 的code set 转成set2,然后的和SP/2一样。



-------SP2键盘--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我们现在用的行列式键盘(非编码键盘),会有按键冲突问题:


假如我同时按QAWS,由于两行两列连通为低电平,扫描后将认为QAWS被按下。

假如我同时按QAW,由于两行两列连通为低电平,扫描后也将认为QAWS都被按下,这种情况就错了。


假如我同时按下QSF呢?扫描后的结果应该是QSF和另外三个键都被按下,这种情况也错了。

所以,按键冲突的原因是——三个(或以上)按键按下使行列线连成一个闭环。

这个时候,大多数键盘选择的方式,就是按照一个固定的规则输出固定的按键组合代码,对于其他可能的组合方式统统予以忽略,换句话说,在上面的键盘中,可能无论你怎么按这四个按键,它可能永远只会输出“Q”-“E”-“D”的组合,尽管可能你按下的实际是“Q”-“A”-“D”(这里只是一个假设,实际上为了避免误码,大多数键盘根本只输出两个按键而已)。于是,“键位冲突”就出现了。


键盘数据接收

无论是通过缆线连接还是无线连接,来自键盘的信号都由计算机的键盘控制器进行监视。这是一种集成电路(IC),不仅可以处理所有来自键盘的数据,并且还会将这些数据转发给操作系统。当操作系统(OS)获得通知键盘有数据输入时,它会查看这些键盘数据是否为系统命令。Windows计算机中的Ctrl-Alt-Delete组合键(用于重启系统)就是一个极好的系统命令的例子。然后,操作系统将键盘数据传送给当前的应用程序。

应用程序确定键盘数据是否为一条命令,例如Alt-f组合键将在Windows程序中打开“文件”菜单。如果数据不是一条命令,应用程序则将这些数据当作内容来接受,它们可以是从输入文件、输入URL到执行运算的任何操作的内容。如果当前的应用程序不接受键盘数据,它将直接忽略这些信息。从击键到将内容输入到应用程序的整个过程几乎可以在瞬间完成。

字符映射表

一旦处理器发现某处电路闭合,它就将该电路在键矩阵上的位置与其只读存储器(ROM)内的字符映射表进行对比。字符映射表的基本功能就是比较图或查询表。它会告诉处理器每个键在矩阵中的位置,以及每次击键或者击键组合所代表的含义。例如,字符映射表会告诉处理器单独按下a键对应于小写字母“a”,而同时按下Shift键和a键对应于大写字母“A”。

计算机也可以使用不同的字符映射表取代键盘中原来使用的映射表。当人们键入的语言使用的字母与键盘上的英文字母表示不同的含义时,这项技术就非常有用。即使人们使用的键盘实际上采用的是QWERTY布局,也可以将计算机设置为按照Dvorak布局来解释他们的键击。另外,操作系统和应用程序还可以进行键盘辅助功能设置,使人们可以更改键盘的特性来克服自身在使用键盘方面的某些障碍。

-------正餐----------------------------------------------------------------------------------------------------------------------------------------------------------------

【good】 i8042 键盘控制器-------详细介绍-      http://blog.chinaunix.net/uid-25099259-id-3409632.html

【good】 http://blog.csdn.net/yes_life/article/details/6840660

【good】 键盘的扫描码Scan Code,通码Make code,断码Break Code http://blog.chinaunix.net/uid-24709751-id-3515788.html


EC:Embedded Controller, for example  ITE EC 

http://wenku.baidu.com/link?url=HCf7VtNb16GDaQKGgR4Srbu6KEnXuj4M9lpuLXK6DGv6brkxgwkqIIVQmqeR0R4WENUGPJcE-BYQI7eQekJOe7BBQLWkcPsKKsTp29V_KVa


【简述:从按下键盘的字母到PC端显示出字母的常见流程】

(1)用户按了键盘上的某个键

(2)键盘的硬件(8048)产生Scan Code,其中目前常见的AT键盘中用的是Set2的Scan Code。

注:

A. 如果是按下则是make Code,如果是释放则是Break Code

(3)键盘的控制器(8042)将Set2的Scan Code转化为Set1的Scan Code

注:

A. 此处是为了兼容旧的Scan Code Set1才去将Set2转为Set1。当然你也去通过设置而禁止掉此处的转换。

B. 将Scan Code Set2转为Scan Code Set1,对于普通PC机,一般是通过BIOS做的这个转换。

(4)控制器再将Set1的Scan Code发送给PC主机Host

(5)主机Host端,会有对应的驱动和软件去处理,将Scan Code,转化为对应的字母而显示出来。当前其间还会涉及到当前系统的本地语言设置,Code Page等知识。



keyboard scan code 表 http://blog.csdn.net/wshjldaxiong/article/details/7531708


扩展scancode

由 e0e1 或e2 引导。占2Byte。符合这个规则:去掉引导字节后,和基本 scan code的值相同。


set 1.2.3

"XT" ("set 1") scancodes    based on the 83-key keyboard used by the IBM PC XT and earlier.

"set 3"  This set is supported by the PS/2 keyboard, but is rarely used.

"AT" ("set 2") scancodes


set2 translate to set1: 

For computers since the IBM PC AT, the keyboard controller on the motherboard translates AT (set 2) scancodes into XT (set 1) scancodes in so called translation mode.[2] This translation can be disabled in pass-through-mode, allowing the raw scancodes to be seen.[3] Therefore, whether a software developer will encounter AT scancodes or XT scancodes on a modern PC-compatible depends on how the keyboard is being accessed.

A multi-function keyboard can be told to send scancodes in set 1, 2 or 3. The most common mode of operation on a PC compatible computer is for the keyboard to send set 2, which is translated by the keyboard controller into set 1. On some PS/2 machines, translation mode is not supported, so that the keyboard driver will either have to cope with raw scancodes in code set 2 or the keyboard be set to code set 1.


http://linux.chinaunix.net/techdoc/net/2008/07/23/1020084.shtml

key event  http://blog.sina.com.cn/s/blog_670bfea20101cn4s.html

Google 了一下Modifier Key:

  • Each key that appears on the keyboard without requiring modifiers are sent as a keydown followed by a key up.
  • If the server does not support native events and must simulate key strokes with JavaScript, it must generatekeydown, keypress, and keyup events, in that order. The keypress event should only be fired when the corresponding key is for a printable character.
  • If a key requires a modifier key (e.g. "!" on a standard US keyboard), the sequence is:modifier down, key down, key up, modifier up, where key is the ideal unmodified key value (using the previous example, a "1").
  • Modifier keys (Ctrl, Shift, Alt, and Command/Meta) are assumed to be "sticky"; each modifier should be held down (e.g. only a keydown event) until either the modifier is encountered again in the sequence, or the NULL (U+E000) key is encountered.
  • Each key sequence is terminated with an implicit NULL key. Subsequently, all depressed modifier keys must be released (with corresponding keyup events) at the end of the sequence.

系统组合键的判定

  在使用键盘的时候,通常会使用到CTRL+SHIFT+ALT 类似的组合键功能。对于此,我们如何来判定?

通过KeyUp 事件能够来处理(这里说明一下为什么不用KeyDown,因为在判定KeyDown的时候,CTRL、SHIFT和ALT 属于一直按下状态,然后再加另外一个键是不能准确捕获组合键,所以使用KeyDown 是不能准确判断出的,要通过KeyUp 事件来判定 )ysmz4:为什么??


Q:主机BIOS向键盘控制器(i8042)设置什么?

A:见 IO PORT command 部分  http://blog.163.com/x_ares/blog/static/1015485620113731537928/


Q:中断间隔时间由谁控制?

A:BIOS set to 键盘控制器。

Typematic Rate (Chars/Sec)[连续击键率(字符/秒)]
  选项: 6, 8, 10, 12, 15, 20, 24, 30
  当你重复按同一个键时,这个选项允许你控制每个字符的重复率,单位是每个字符/秒,不过必须先要在Typematic Rate Setting中设为Enabled。 

Typematic Rate Delay (Msec)[ 连续击键延迟时间(毫秒)] 
  选项: 250, 500, 750, 1000
  当你重复按同一个键时,这个选项控制的是每个字符的停顿时间,单位是毫秒,即每个字符出现前停顿多少毫秒,当然罗,你要在Typematic Rate Setting中设为Enabled。



wiki:

http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html#ss1.1

1.1 Key release

Most keyboards will repeat the make code (key down code) when the key repeats. Some will also fake Shift down and Shift up events during the repeat.

。。。

There are many reports of laptops with badly debounced key-up events. Thus, unexpected key-up events should probably be regarded as not unusual, and be ignored. Another source of key-up events without preceding key-down can be the fake shift.

1.6 Fake shifts

The ten grey keysInsert, Home, PgUp, Delete, End, PgDn, Up, Left, Down, Right are supposed to function regardless of the state ofShift and NumLock keys. But for an old AT keyboard the keypad keys would produce digits when Numlock was on or Shift was down. Therefore, in order to fool old programs, fake scancodes are sent: when LShift is down, and Insert is pressed, e0 aa e0 52 is sent; upon release of Insert e0 d2 e0 2a is sent. In other words, a fake LShift-up and fake LShift-down are inserted.

这10个按键执行功能时应该不管Shift and NumLock是什么状态。但为了兼容AT keyboard,当LShift is down, and Insert is pressed, e0 aa e0 52 is sent; upon release of Insert e0 d2 e0 2a is sent. In other words, a fake LShift-up and fake LShift-down are inserted.整个键值串如下:e0 2a    e0 aa e0 52    e0 d2 e0 2a   (有或没有e0 aa) ,Insert就没有在LShift期间按下,而是在LShift抬起后(ysmz4:怎么解释shift+方向键和方向键 效果不一样?这句话意思是在shift_up+方向键结果是选定,而不是digit?)


---USB键盘--------------------------------------------------------------------------

USB键盘     http://blog.163.com/x_ares/blog/static/1015485620113731537928/

http://wenku.baidu.com/link?url=Zf6llLq-hrtu2Wrtn4WX1RdqxOTheqQoALUwVw4Q9u36ApoNxgkgwreV6dswJWWzxhXPVAJ8VWt2u2ZpQqQbCS-CG94utJP4_59kwI0mWeC

Usb keyboardLegacy mode需要将数据送给EC,由EC在送给Host

当usb keyboard有数据输入,BIOS将数据转化并通过D2 command将数据送给EC, EC通过IRQ1通知Host,Host再下来读取。


一 
小弟刚开始学习EC,用的是ITE的8511,收益匪浅,但是还有几个疑问, 
1. peter老师提到,按下Fn后,ec要设置flag,是在哪里设置flag呢?如何去查看这个flag?
2. 有了flag后,按下fn功能组合键,比如说Fn+F2,应该能够抓到scancode吧?这个scancode与单纯的F2键的set2 scancode一样吗? 

3. Fn+Fx功能键的调节功能是如何实现的呢?是Q_EVent吗? 
4. 为什么有些Fn组合键的功能可以不需要应用程序(进入OS的那种)就可以实现,而有些需要有驱动程序的支持呢?这两种组合功能键实现上有什么区别? 
 
二:

1.peter老师说的这个flag,应该是code里设置的,定义一个BYTE,一般存overlay,Fn,Numlock,ctrl,alt,shift这些flag.(ysmz4:Fn,Numlock,Caps Lock用flag能理解,因为当按下抬起后表示为一个状态。shift,ctrl用flag不能理解,因为只在按下过程中和别的按键结合表示一个状态。
 
2.单纯按F2,一定有ccancode,但是Fn+F2这个要不要发scancode,要看这个功能需不需要,假设我要做LCD/CRT Switch,EC要发query event,不用发scancode 
3.调节功能你指得是什么呢?Query event是需要通知上层就发,asl对应的method会作处理
4.对EC来说,就是判断按下了什么键,你要EC怎么处理完全随你!有EC本身可以实现的,发scancode os处理的,发Q_Event asl实现的,还有driver实现的 
 
三:今天看到坛子里一个关于键盘的帖子,
 
http://ufoit.com/bbs/thread-770-1-1.html 
 
结合Peter和Sunnyholiday两位老师的指导,重新理解一下Fn的工作原理,请各位老师指正。  
假设Fn+F2是某个功能的Hotkey,不管是什么功能,当按下Fn+F2时,EC会下Flag,同时给出F2的keycode,EC or OS接收keycode的同时,检查flag,然后由EC直接实现该功能,
或者EC发送keycode给OS,由driver实现对应的功能。
 
不知道我这样理解是否正确?


四:Peter是老师,我可不是。。。 
我的意思是按Fn,EC会有个flag,os可没有flag!你可以看看CORE_XLT.C,如果你的code有的话。 
只按F2,会发scancode, 但是按Fn+F2,要怎么样就是EC自己搞了。例子,假设我Fn+F2要做LCD/CRT Switch,EC发个query event就可以了,不用发scancode,asl的对应的method
会做处理。假设我Fn+F2要做增加减少声音,发scancode就行了。


按键时间和扫描周期----------------------------------------------------------------------------------------------------------------------------------------------------------------

1.一般人在单击按键时的低电平保持时间大概是多少?
2.键扫描最小频率(一定是可以容忍的最小频率)是多少  时,人按键不会感到迟钝

http://bbs.21ic.com/icview-40402-1-1.html


怎么判断按键的时间长短程序

http://home.eeworld.com.cn/my/space-uid-116926-blogid-15886.html


遗留问题----------------------------------------------------------------------------------------------------------------------------------------------------------------

为什么要保留scancode set2呢?既然set2的所有码都能转成set1
已在此处提问  http://bbs.csdn.net/topics/391544606





0 0